Java Socket communication (client server two way)


Create two new projects, 1 client and 1 server. Start the server and then start the client

The read-write thread classes for both projects are essentially identical

Server:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

  public static final int PORT = 8000;// The port number to listen to

  public static void main(String[] args) {
    Server server = new Server();
    server.init();
  }

  public void init() {
    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket(PORT);
      while (true) {
        Socket client = serverSocket.accept();
        //1 Two client connections on account opening handle reads and writes
        new Thread(new ReadHandlerThread(client)).start();
        new Thread(new WriteHandlerThread(client)).start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally{
      try {
        if(serverSocket != null){
          serverSocket.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

/*
 * The thread handling the read operation
 */
class ReadHandlerThread implements Runnable{
  private Socket client;

  public ReadHandlerThread(Socket client) {
    this.client = client;
  }

  @Override
  public void run() {
    DataInputStream dis = null;
    try{
      while(true){
        // Read client data
        dis = new DataInputStream(client.getInputStream());
        String reciver = dis.readUTF();
        System.out.println(" The content sent by the client :" + reciver);
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try {
        if(dis != null){
          dis.close();
        }
        if(client != null){
          client = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

/*
 *  The thread that handles write operations
 */
class WriteHandlerThread implements Runnable{
  private Socket client;

  public WriteHandlerThread(Socket client) {
    this.client = client;
  }

  @Override
  public void run() {
    DataOutputStream dos = null;
    BufferedReader br = null;
    try{
      while(true){
        // Reply to the client
        dos = new DataOutputStream(client.getOutputStream());
        System.out.print(" Please enter the :\t");
        //  Keyboard entry
        br = new BufferedReader(new InputStreamReader(System.in));
        String send = br.readLine();
        // To send data
        dos.writeUTF(send);
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try {
        if(dos != null){
          dos.close();
        }
        if(br != null){
          br.close();
        }
        if(client != null){
          client = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

Client:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Client {

  public static final String IP = "localhost";// Server address
  public static final int PORT = 8000;// Server port number

  public static void main(String[] args) {
    handler();
  }

  private static void handler(){
    try {
      // instantiation 1 a Socket , and specify the server address and port
      Socket client = new Socket(IP, PORT);
      // Start two threads, 1 The individual is responsible for reading, 1 Responsible for writing
      new Thread(new ReadHandlerThread(client)).start();
      new Thread(new WriteHandlerThread(client)).start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

/*
 * The thread handling the read operation
 */
class ReadHandlerThread implements Runnable{
  private Socket client;

  public ReadHandlerThread(Socket client) {
    this.client = client;
  }

  @Override
  public void run() {
    DataInputStream dis = null;
    try {
      while(true){
        // Read the server-side data
        dis = new DataInputStream(client.getInputStream());
        String receive = dis.readUTF();
        System.out.println(" The server side is coming back with : " + receive);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
      try {
        if(dis != null){
          dis.close();
        }
        if(client != null){
          client = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

/*
 *  The thread that handles write operations
 */
class WriteHandlerThread implements Runnable{
  private Socket client;

  public WriteHandlerThread(Socket client) {
    this.client = client;
  }

  @Override
  public void run() {
    DataOutputStream dos = null;
    BufferedReader br = null;
    try {
      while(true){
        // Get the output stream
        dos = new DataOutputStream(client.getOutputStream());
        System.out.print(" Please enter the : \t");
        // Keyboard entry
        br = new BufferedReader(new InputStreamReader(System.in));
        String send = br.readLine();
        // To send data
        dos.writeUTF(send);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
      try{
        if(dos != null){
          dos.close();
        }
        if(br != null){
          br.close();
        }
        if(client != null){
          client = null;
        }
      }catch(Exception e){
        e.printStackTrace();
      }
    }
  }
}