❮ Previous Next ❯

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:

Socket s=new Socket(“IPAddress”, port number);
Here the IPAddress represents the IPAddress of the server machine where Server1.java 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 Server1.java and Client1.java programs on two different computers connected in a network. But, at Client1.java, we should pass the server machine’s IPAddress. Then the port number at Client1.java 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.

InputStreamobj=s.getInputStream(); BufferedReaderbr=new BufferedReader(new InputStreamReader(obj)); str=br.readLine(); 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.
import java.io.*;
import java.net.*;
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
D:\rnr>javac Client1.java

❮ Previous Next ❯


Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Email: info@silantechnology.com
      Phone: 0674-2361252
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved