We can write a client program that receives all the strings sent from server. Let us follow these steps to do this:
// 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>
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