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

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.