Java TCP Chat – Multiple Client


INFO : These programs work as following

  • server must be running first
  • any number of clients can be run then
  • server randomly take request from any client and respond to it
  • to respond client server maintains that many number of threads
  • each thread share the same object, which object’s method will respond to client, thus synchronization is achieved
  • client program runs until client enters ‘EXIT’

SERVER PROGRAM

public class TCPMultiServer {

public static void main(String argv[]) throws Exception {

ServerSocket welcomeSocket = new ServerSocket(6789);

Responder h = new Responder();
 // server runs for infinite time and
 // wait for clients to connect
 while (true) {
 // waiting..
 Socket connectionSocket = welcomeSocket.accept();

// on connection establishment start a new thread for each client
 // each thread shares a common responder object
 // which will be used to respond every client request
 // need to synchronize method of common object not to have unexpected behaviour
 Thread t = new Thread(new MyServer(h, connectionSocket));

// start thread
 t.start();

}
 }
 }

class MyServer implements Runnable {

Responder h;
 Socket connectionSocket;

public MyServer(Responder h, Socket connectionSocket) {
 this.h = h;
 this.connectionSocket = connectionSocket;
 }

@Override
 public void run() {

while (h.responderMethod(connectionSocket)) {
 try {
 // once an conversation with one client done,
 // give chance to other threads
 // so make this thread sleep
 Thread.sleep(5000);
 } catch (InterruptedException ex) {
 ex.printStackTrace();
 }
 }

try {
 connectionSocket.close();
 } catch (IOException ex) {
 Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex);
 }

}

}

class Responder {

String serverSentence;
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// on client process termination or
 // client sends EXIT then to return false to close connection
 // else return true to keep connection alive
 // and continue conversation
 synchronized public boolean responderMethod(Socket connectionSocket) {
 try {

BufferedReader inFromClient =
 new BufferedReader(
 new InputStreamReader(
 connectionSocket.getInputStream()));

DataOutputStream outToClient =
 new DataOutputStream(
 connectionSocket.getOutputStream());

String clientSentence = inFromClient.readLine();

// if client process terminates it get null, so close connection
 if (clientSentence == null || clientSentence.equals("EXIT")) {
 return false;
 }

if (clientSentence != null) {
 System.out.println("client : " + clientSentence);
 }
 serverSentence = br.readLine() + "\n";

outToClient.writeBytes(serverSentence);

return true;

} catch (SocketException e) {
 System.out.println("Disconnected");
 return false;
 } catch (Exception e) {
 e.printStackTrace();
 return false;
 }
 }
 }

CLIENT PROGRAM

public class TCPMultiClient {

public static void main(String argv[]) throws Exception {
 String sentence;
 String modifiedSentence;

BufferedReader inFromUser =
 new BufferedReader(
 new InputStreamReader(System.in));

Socket clientSocket = new Socket("localhost", 6789);

while (true) {
 DataOutputStream outToServer =
 new DataOutputStream(
 clientSocket.getOutputStream());

BufferedReader inFromServer =
 new BufferedReader(
 new InputStreamReader(
 clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

if (sentence.equals("EXIT")) {
 break;
 }

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

}
 clientSocket.close();
 }
 }

Advertisements

One thought on “Java TCP Chat – Multiple Client

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s