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.
// Write a program to create client,which accepts all the strings sent by the server.
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
// close connection by closing the streams and sockets