Hello friends, today I am going to explain the client-server communication using Java programming language. I will develop a simple program just so that you can understand the basics of it.We are going to use threads as well because we want multiple clients to connect with the server at the same time. And we can handle this by using multi-threading.

How it works?

We create the server class and open a connection using the ServerSocket java Class to communicate with the client.Then we create the client class using Socket class.Keep in mind that we should use the same port for both client and server. That enables a successful communication and the server will listen and accept the client.

The server class :

import java.net.*;
import java.io.*;

public class server {

  public static void main(String[] args) throws IOException {
    
    ServerSocket ss = new ServerSocket(5555);
    System.out.println("Server is now up and running...");
    
    while(true)
    {
      Socket s = ss.accept();
      new thread(s).start();
    }

  }

}

 

Basically here we create a ServerSocket class and give 5555 as a parameter for the port number.As I said earlier, the server and client should have the same port number so they can communicate when the client connects to the server. While the server is up and running, we create the Socket class. This is for the client which means that the server will accept him. To do this we call the accept() method of the ServerSocket class.Next, we call the thread class and give the client object as a parameter.By doing so, we start the communication.

The client class :

import java.net.*;
import java.io.*;

public class client {

public static void main(String[] args) throws UnknownHostException, IOException {
    Socket s = new Socket("localhost",5555);
    
  PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String msg;
  while((msg=br.readLine())!=null)
  {
      System.out.println("Sending "+msg);
      pw.println(msg);
  }
  }

}

 

Notice that in this class, we create a Socket object which is the client of course.It takes 2 parameters

1 – the server which is localhost

2 – the port number that we mentioned earlier

The thread class :

import java.net.*;
import java.io.*;

public class thread extends Thread{

  public Socket s;
  
  public thread(Socket s)
  {
    this.s=s;
  }
  
  public void run()
  {
    BufferedReader br;
    try {
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    
    String client_msg = null;
    while((client_msg=br.readLine())!=null)
    {
      System.out.println("The client sent : "+client_msg);
    }
    
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 

I am not going to enter into details because if you are seeing this example most likely you know about PrintStream or BufferedReader Java classes. So go ahead, compile the example and I then suggest to write it yourself.

The compiled program

client-server

You can comment below for any suggestions or questions. Stay tuned!

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here