Creating a Client

We can write a client program that receives all the strings sent from server. Let us follow these steps to do this:


  • First, we should create a socket at client side using Socket class as:

Socket s=new Socket(“IPAddress”, port number);
Here the IPAddress represents the IPAddress of the server machine where program is running. To know the IPAddress, we can use dos command as:
This will display the IPAddress of the machine where the command is applied.
Or we can follow the commands:
Start->Setting->Control panel->Network connections->right click on this to see the ‘local area connection’ dialog box and there double click on Internet protocol. (TCP/IP).
It opens Internet protocol properties where we can see the IPAddress.
It is possible to run the and programs on two different computers connected in a network. But, at, we should pass the server machine’s IPAddress. Then the port number at should be same as the port number with which the server socket has been created. In case, you do  not have your computer in a network, you have to run both the server and client programs in the same system. In that case, you can use localhost in place of IPAddress. The word localhost represents that the server is also locally available in the same system.

  • We should add InputStreams to the socket so that the socket will be able to receive the data on the InputStream.


  • To read the data from the socket into the client, we can take the help of BufferedReader as:

BufferedReaderbr=new BufferedReader(new InputStreamReader(obj));


  • Now we can read the data from the BufferedReader object, using read() or readLine() methods. Read() method can read a single line character at a time, where asreadLine() can read a string.


  • Close the connection by closing all the streams and sockets.

br.close();  // close the BufferedReader
s.close();  // close the socket

// Write a program to create client,which accepts all the strings sent by the server.
class Client1
            public static void  main(String args[])throws Exception
                        // create client socket with same port number
                        Socket s=new Socket(“localhost”,777);
                        // to read data coming from the server, attach InputStream to the socket
                        // to read data from the socket into the client, use BufferedReader
                        BufferedReaderbr=new BufferedReader(new InputStreamReader(obj));
                        // receive strings
                        String str;
                        while((str=br.readLine())  !=null)
                        System.out.println(“From server:”+str);
                        // close connection by closing the streams and sockets

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252