It is possible to send data from the server and receive the response from the client. Similarly, the client can also send and receive the data to and fro. For this purpose, we need additional streams both at server and client. For example, to receive the data into the server, it is a better idea to use BufferedReader as:
InputStreamobj=s.getInputStream();
BufferedReaderbr=new BufferedReader(new InputStreamReader(obj);
Then read() or readLine() methods of BufferedReader class can be used to read data. To send data from the client, we can take the help of DataOutputStream as:
OutputStreamobj=s.getOutputStream();
DataOutPutStream dos=new DataOutputStream(obj);
Then writeBytes() method of DataOutputStream can be used to send strings in the form of group of bytes.
//Server2 – A server that receives data and sends data
import java.io.*; import java.net.*; class Himalaya { public static void main(String args[])throws exception { //Create server socket ServerSocketss=new ServerSocket(888); //Connect it to Client socket Socket s=ss.accept(); System.out.println(“Connection established”); //to send data to the client PrintStreamps=new PrintStream(s.getOutputStream()); //to read data coming from the client BufferedReaderbr=new BufferedReader(new InputStreamReader(s.getInputStream())); //to read data from the keyboard BufferedReader kb=new BufferedReader(new inputStreamReader(System.in)); while(true) //server executes continuously { String str,str1; //repeat as long as client does not send null string while((str=br.readLine()) !=null) //read from client { System.out.println(str); str1=kb.readLine(); ps.println(str1); //send to client } // close connection ps.close(); br.close(); kb.close(); ss.close(); s.close(); System.exit(0); //terminate appliacation } //end of while } }
Output:
D:\rnr>javac Himalaya.java
D:\rnr>
Write a program to create a client which first connects to a server, then starts the communication by sending a string to the server. The server sends response to the client. When ‘exit’ is typed at client side, the program terminates.
//Client2 – a client that sends data and receives also
import java.io.*; import java.net.*; class Client2 { public static void main(String args[])throws Exception { // create client socket Socket s=new Socket(“localhost”,888); // to send data to the server DataOutputStream dos=new DataOutputStream(s.getOutputStream()); // to read data coming from the server BufferedReaderbr=new BufferedReader( new InputStreamReader(s.getInputStream))); // to read data from the keyboard BufferedReader kb= new BufferedReader( new InputStreamReader(System.in)); String str,str1; // repeat as long as exit is not typed at client while(!(str=kb.readLine()).equals(“exit”)) { dos.writeBytes(str+”\n”); // send to server str1=br.readLine(); // receive from server System.out.println(str1); } //close connection dos.close(); br.close(); kb.close(); s.close(); } }
Output:
D:\rnr>javac Client2.java
D:\rnr>
Run the server2.java and client2.java in two dos windows.
See the output while running these programs.
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