Client Server in java

Introduction 

 
Client-Server Connection using ObjectOutputStream & ObjectInputStream in JAVA.
  
In Client/Server applications the server normally listens to a specific port waiting for connection requests from a client.
 
When a connection request arrives, the client and the server establish a dedicated connection to communicate.
 
During the connection process, the client is assigned a local port number and binds a socket to it.
 
The client talks to the server by writing to the socket and gets information from the server by reading from it.
 
Similarly, the server gets a new local port number to communicate with the client.
 
The server also binds a socket to its local port and communicates with the client by reading from and writing to it.
 
The server uses a specific port dedicated only to listening for connection requests from other clients. It can not use this specific port for communication with the clients.
 
The client and the server must agree on a protocol. They must agree on the language of the information transferred back and forth through the socket.
 
The java.net package in the Java development environment provides the class “Socket” which implements the client-side and the class “ServerSocket” class which implements the server-side of the two-way link.
 

Server side

  1. import java.io.*;  
  2. import java.net.*;  
  3. public class Provider {  
  4.  ServerSocket providerSocket;  
  5.  Socket connection = null;  
  6.  ObjectOutputStream out;  
  7.  ObjectInputStream in ;  
  8.  String message;  
  9.  Provider() {}  
  10.  void run() {  
  11.   try {  
  12.    providerSocket = new ServerSocket(200410);  
  13.    System.out.println("Waiting for connection");  
  14.    connection = providerSocket.accept();  
  15.    System.out.println("Connection received from " + connection.getInetAddress().getHostName());  
  16.    out = new ObjectOutputStream(connection.getOutputStream());  
  17.    out.flush(); in = new ObjectInputStream(connection.getInputStream());  
  18.    sendMessage("Connection successful");  
  19.    do {  
  20.     try {  
  21.      message = (String) in .readObject();  
  22.      System.out.println("client>" + message);  
  23.      if (message.equals("bye"))  
  24.       sendMessage("bye");  
  25.     } catch (ClassNotFoundException classnot) {  
  26.      System.err.println("Data received in unknown format");  
  27.     }  
  28.    } while (!message.equals("bye"));  
  29.   } catch (IOException ioException) {  
  30.    ioException.printStackTrace();  
  31.   } finally {  
  32.    try {  
  33.     in .close();  
  34.     out.close();  
  35.     providerSocket.close();  
  36.    } catch (IOException ioException) {  
  37.     ioException.printStackTrace();  
  38.    }  
  39.   }  
  40.  }  
  41.  void sendMessage(String msg) {  
  42.   try {  
  43.    out.writeObject(msg);  
  44.    out.flush();  
  45.    System.out.println("server>" + msg);  
  46.   } catch (IOException ioException) {  
  47.    ioException.printStackTrace();  
  48.   }  
  49.  }  
  50.  public static void main(String args[]) {  
  51.   Provider server = new Provider();  
  52.   while (true) {  
  53.    server.run();  
  54.   }  
  55.  }  
  56. }  
  57. Client Side  
  58. import java.io.*;  
  59. import java.net.*;  
  60. public class Requester {  
  61.  Socket requestSocket;  
  62.  ObjectOutputStream out;  
  63.  ObjectInputStream in ;  
  64.  String message;  
  65.  Requester() {}  
  66.  void run() {  
  67.   try {  
  68.    requestSocket = new Socket("localhost"2004);  
  69.    System.out.println("Connected to localhost in port 2004");  
  70.    out = new ObjectOutputStream(requestSocket.getOutputStream());  
  71.    out.flush(); in = new ObjectInputStream(requestSocket.getInputStream());  
  72.    do {  
  73.     try {  
  74.      message = (String) in .readObject();  
  75.      System.out.println("server>" + message);  
  76.      sendMessage("Hi my server");  
  77.      message = "bye";  
  78.      sendMessage(message);  
  79.     } catch (ClassNotFoundException classNot) {  
  80.      System.err.println("data received in unknown format");  
  81.     }  
  82.    } while (!message.equals("bye"));  
  83.   } catch (UnknownHostException unknownHost) {  
  84.    System.err.println("You are trying to connect to an unknown host!");  
  85.   } catch (IOException ioException) {  
  86.    ioException.printStackTrace();  
  87.   } finally {  
  88.    try {  
  89.     in .close();  
  90.     out.close();  
  91.     requestSocket.close();  
  92.    } catch (IOException ioException) {  
  93.     ioException.printStackTrace();  
  94.    }  
  95.   }  
  96.  }  
  97.  void sendMessage(String msg) {  
  98.   try {  
  99.    out.writeObject(msg);  
  100.    out.flush();  
  101.    System.out.println("client>" + msg);  
  102.   } catch (IOException ioException) {  
  103.    ioException.printStackTrace();  
  104.   }  
  105.  }  
  106.  public static void main(String args[]) {  
  107.   Requester client = new Requester();  
  108.   client.run();  
  109.  }  
  110. }  
Output
 
Image-1.jpg
 
 
Image-2.jpg