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’


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


class MyServer implements Runnable {

Responder h;
 Socket connectionSocket;

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

 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
 } catch (InterruptedException ex) {

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



class Responder {

String serverSentence;
 BufferedReader br = new BufferedReader(new InputStreamReader(;

// 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(

DataOutputStream outToClient =
 new DataOutputStream(

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";


return true;

} catch (SocketException e) {
 return false;
 } catch (Exception e) {
 return false;


public class TCPMultiClient {

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

BufferedReader inFromUser =
 new BufferedReader(
 new InputStreamReader(;

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

while (true) {
 DataOutputStream outToServer =
 new DataOutputStream(

BufferedReader inFromServer =
 new BufferedReader(
 new InputStreamReader(

sentence = inFromUser.readLine();

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

if (sentence.equals("EXIT")) {

modifiedSentence = inFromServer.readLine();

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



One thought on “Java TCP Chat – Multiple Client

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s