i just started writing my first Server/Client code and its a simple chat program , but i dont know how should I run the Code !
there is a .class file named Server Side , and a .class file named clientSide , are they supposed to be in different projects ? how should I run it so they both have a connection together ? thanks already ,following is a part of the codes
public void runServer()
{
try {
server = new ServerSocket();
while(true)
{
try
{
connection = server.accept();
try{
output = new ObjectOutputStream(connection.getOutputStream()) ;
output.flush();
input = new ObjectInputStream(connection.getInputStream()) ;
sendData(message) ;
do
{
try{
message = (String) input.readObject() ;
System.out.println(message);
}catch(Exception e)
{
e.printStackTrace() ;
}
}while(!message.equals("end"));
}catch(EOFException e)
{
e.printStackTrace() ;
}
}catch(IOException e)
{
e.printStackTrace() ;
}
finally {
try{
output.close();
input.close();
connection.close() ;
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}catch(Exception e )
{
e.printStackTrace();
}
}
and here’s the clientSide :
public void runClient()
{
try{
connect() ;
}catch(Exception e)
{
e.printStackTrace();
}
try{
output = new ObjectOutputStream(client.getOutputStream()) ;
output.flush() ;
input = new ObjectInputStream(client.getInputStream()) ;
}catch(IOException e)
{
e.printStackTrace() ;
}
do
{
try{
message = (String) input.readObject() ;
System.out.println(message);
}catch(Exception e)
{e.printStackTrace();}
}while(!message.equals("end")) ;
}
public void connect() throws UnknownHostException, IOException
{
client = new Socket(InetAddress.getByName(chatServer),12345) ;
}
First run the server
Then run the client:
But I do not see your server bound to the port 12345. The client will try to connect to the server on the port 12345 because of the statement
If the ports do not match, the connection will not be established.
To bind the server to the port 12345 try this:
instead of the default constructor.