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



JSF Features

JSF is rich featured that make it much popular than the existing frameworks. Some of the JSF features are :

I JSF is standard web user interface framework for Java. 
II Built on top of Servlet API.
III JSF is a component framework
IV UI components are stored on the server.
V Easy use of third party components.
VI Event driven programming model.
VII Events generated by user are handled on the server.
VIII Navigation handling.
IX It can automatically synchronize UI components.

X JSF supports multiple client devices.
XI JSF has extensible architecture.
XII International language support.
XIII Extensive tool support (Sun, Oracle, IBM etc.).
XIV Rapid application development approach.