need Advice / Help
HI.. i have a program here that works as a simple chat program...
i have Strider.java as the server and Hiryu.java as the client...
my question is, althoug it is already running, the server and client
can't meet each other.
the server can't detect if there's a connection or the client can't connect
i can't see if there's really a connection happen..
could they please help me solve my problem. i have here
the codes of my program..
//Strider.java
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Strider extends JFrame implements ActionListener
{
ObjectOutputStream output;
ObjectInputStream input;
String msg;
JTextField txtfld;
JTextArea txtarea;
public Strider()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
txtfld = new JTextField(10);
txtarea = new JTextArea(10,20);
txtfld.addActionListener(this);
c.add(txtfld);
c.add(new JScrollPane(txtarea));
setSize(230,250);
show();
}
public void actionPerformed(ActionEvent e)
{
sendData(e.getActionCommand());
}
public void runStrider()
{
ServerSocket server;
Socket connection;
int counter = 1;
try {
server = new ServerSocket(5000, 100);
while(true){
txtarea.setText("Waiting for someone to connect");
connection = server.accept();
txtarea.append("Connection" + counter + "received from:" + connection.getInetAddress().getHostName());
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
txtarea.append("\n Got I/O Streams \n");
msg = "Server>> Connection Successful";
output.writeObject(msg);
output.flush();
do{
try{
msg = (String) input.readObject();
txtarea.append("\n" + msg);
txtarea.setCaretPosition(txtarea.getText().length());
}
catch(ClassNotFoundException cnfex){
txtarea.append("\nUnknown Object type received");
}
}while(!msg.equals("Client>> Terminate"));
txtarea.append("\nUser Terminated connection");
output.close();
input.close();
connection.close();
++counter;
}
}
catch(EOFException eof){
System.out.println("Client terminated connection");
}
catch(IOException io){
io.printStackTrace();
}
}
public void sendData(String s)
{
try{
output.writeObject("Server>>" + s);
output.flush();
txtarea.append("\nServer>>" + s);
}
catch(IOException cnfex){
txtarea.append("\nError writing Object");
}
}
public static void main(String[] args)
{
Strider app = new Strider();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
app.runStrider();
}
}
//Hiryu.java
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Hiryu extends JFrame implements ActionListener
{
ObjectOutputStream output;
ObjectInputStream input;
String msg;
JTextField txtfld;
JTextArea txtarea;
public Hiryu()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
txtfld = new JTextField(10);
txtarea = new JTextArea(10,20);
txtfld.addActionListener(this);
c.add(txtfld);
c.add(new JScrollPane(txtarea));
setSize(230,250);
show();
}
public void actionPerformed(ActionEvent e)
{
sendData(e.getActionCommand());
}
public void runClient()
{
Socket client;
try {
txtarea.setText("Attempting Connection\n");
client = new Socket(InetAddress.getByName("127.168.2.109"), 5000);
txtarea.append("Connected to:" + client.getInetAddress().getHostName());
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
txtarea.append("\n Got I/O Streams \n");
msg = "Server>> Connection Successful";
output.writeObject(msg);
output.flush();
do{
try{
msg = (String) input.readObject();
txtarea.append("\n" + msg);
txtarea.setCaretPosition(txtarea.getText().length());
}
catch(ClassNotFoundException cnfex){
txtarea.append("\nUnknown Object type received");
}
}while(!msg.equals("Server>> Terminate"));
txtarea.append("\nUser Terminated connection");
output.close();
input.close();
client.close();
//++counter;
}
catch(EOFException eof){
System.out.println("Client terminated connection");
}
catch(IOException io){
io.printStackTrace();
}
}
public void sendData(String s)
{
try{
output.writeObject("Client>>" + s);
output.flush();
txtarea.append("\nClient>>" + s);
}
catch(IOException cnfex){
txtarea.append("\nError writing Object");
}
}
public static void main(String[] args)
{
Hiryu app = new Hiryu();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
app.runClient();
}
}

