Sunday 23 August 2015

Client Server program in Java using Socket (One Server and Multiple Clients)

                 In this post, we will see how to write a sample client-server program in Java using Socket programming (One Server and one/more Clients). 
                 Go through the following programs (Server.java and Client.java). Run Server.java on a machine which you want to make server and run Client.java on any number of machines which you want to make clients. In Server.java, I have used multi-threading to handle requests from multiple clients.
                 I have run following programs on same machine. That's why I have used IP address 127.0.0.1. If you want to run on different machines, then instead of 127.0.0.1, use IP address of server machine. 


Program: (Client.java)


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

public class Client
{
   public static void main(String [] args) throws IOException
   {
      String serveraddress = "127.0.0.1";
      int port=4000;
     
         Socket client = new Socket(serveraddress, port);
         System.out.println("Connected to "+client.getRemoteSocketAddress());
         DataOutputStream out=new DataOutputStream(client.getOutputStream());

         out.writeUTF("Hello from Client");
         DataInputStream in=new DataInputStream(client.getInputStream());
         System.out.println("Message from Server: "+in.readUTF());
         client.close();
    }
}

Program: (Server.java)

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

public class Server extends Thread
{
   private ServerSocket serversocket;
  
   public Server(int port) throws IOException
   {
      serversocket = new ServerSocket(port);
      serversocket.setSoTimeout(15000);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            Socket server = serversocket.accept();
            System.out.println("Connected to "+server.getRemoteSocketAddress());
            DataInputStream in=new DataInputStream(server.getInputStream());
            System.out.println("Message from Client: "+in.readUTF());
            DataOutputStream out=new DataOutputStream(server.getOutputStream());
            out.writeUTF("Hello from Server");
            server.close();
         }
         catch(SocketTimeoutException e)
         {
            System.out.println("Socket Timed Out!!");
            break;
         }
         catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args) throws IOException
   {
         int port=4000;
         Thread t = new Server(port);
         t.start();
   }
}

How To Run: 
(Note:  First run Server.java program on server machine and then run Client.java on all client machines.)

For Server.java
To Compile:
javac Server.java
To Run: 
java Server

For Client.java 
To Compile:
javac Client.java 
To Run:
java Client

Output:
                       I have tested output for one Server and two Clients on same machine. That's why, I mentioned server's address as 127.0.0.1(localhost's address). If you want to run these programs on distinct machines, then instead of 127.0.0.1, mention server machine's IP address.  


Server


Client 1


Client 2

Next: How to find the execution time of a C program

Previous: How to Handle Session in JSP (Java) with Example

2 comments: