Hello everyone,
I am trying to make a java application in which there are two classes -Server and Client. But these are not connecting.Here are the codes :
and SimpleClient.java :
It is printing only "hello" but I want a chat type application. I want that when client says anything the server should respond it.
Can anyone make suggestions about this.
Thanks in advance.
I am trying to make a java application in which there are two classes -Server and Client. But these are not connecting.Here are the codes :
package javaapplication5;
import java.io.*;
import java.net.*;
public class SimpleServer
{
public static void main(String args[])
{
ServerSocket s=null;
try
{
s=new ServerSocket (8989);
}
catch(IOException e)
{
e.printStackTrace();
}
while(true)
{
try
{
Socket s1=s.accept();
OutputStream s1out=s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
dos.writeUTF("hello");
dos.flush(); // dont forget to call this method
dos.close();
s1.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
and SimpleClient.java :
package javaapplication5;
import java.net.*;
import java.io.*;
public class SimpleClient
{
public static void main(String args[])
{
try
{
Socket s1=new Socket("localhost",8989);
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
System.out.println(dis.readUTF());
// br.close();
s1.close();
}
catch(ConnectException connExc)
{
System.err.println("could not connect");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
It is printing only "hello" but I want a chat type application. I want that when client says anything the server should respond it.
Can anyone make suggestions about this.
Thanks in advance.