Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

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 Server1.java program is running.
    To know the IPAddress, we can use dos command as:
    C:\>ipconfig
    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.
  • • We should add InputStreams to the socket so that the socket will be able to receive the data on the InputStream.
    InputStreamobj=s.getInputStream();
  • • 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.
    str=br.readLine();
  • • Close the connection by closing all the streams and sockets. br.close(); // close the BufferedReader
    s.close(); // close the socket
Program:

// 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
		InputStreamobj=s.getInputStream();
		// 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
		br.close();
		s.close();
	}
}

Output:
D:\rnr>javac Client1.java
D:\rnr>

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext