Establishing the two-way Communication between Server and Client in Java
It is possible to send data from the server and receive a response from the client. Similarly, the client can also send and receive data to-and-from.
Below are the various steps to do so:
- We need additional streams both at server and client. For example, to receive data into the server, it is a better idea to use a BufferedReader object, as shown in the following code snippet:
InputStream obj = s.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(obj);
- Then read() or readLine() methods of the BufferedReader object can be used to read data. To send data from the client we can take the help of the DataOutputStream class as shown in the following code snippet:
OutputStream obj + s.getOutputStrean(); DataOutputStream dos = new DataOutputStream(obj);
- Then, the writeBytes() method of the DataOutputStream class can be used to send strings in the form of a group of bytes. To establish the two-way communication between a client and server perform the following steps:
- Creating the Server Program: Let’s create a class named Server2.java to create server such that the server receives data from the client using a BufferedReader object and then sends a reply to the client using a PrintStream object.
Server2.java
// Server2 class that// receives data and sends dataimportjava.io.*;importjava.net.*;classServer2 {publicstaticvoidmain(String args[])throwsException{// Create server SocketServerSocket ss =newServerSocket(888);// connect it to client socketSocket s = ss.accept();System.out.println("Connection established");// to send data to the clientPrintStream ps=newPrintStream(s.getOutputStream());// to read data coming from the clientBufferedReader br=newBufferedReader(newInputStreamReader(s.getInputStream()));// to read data from the keyboardBufferedReader kb=newBufferedReader(newInputStreamReader(System.in));// server executes continuouslywhile(true) {String str, str1;// repeat as long as the client// does not send a null string// read from clientwhile((str = br.readLine()) !=null) {System.out.println(str);str1 = kb.readLine();// send to clientps.println(str1);}// close connectionps.close();br.close();kb.close();ss.close();s.close();// terminate applicationSystem.exit(0);}// end of while}}Command to compile the Server2.Java file:
D:\Conversation Program>javac Sever2.java
- Creating the Client Program: Let’s create a client, named Client2.Java, which first connects to a server, then starts the communication by sending a string to the server. The server sends a response to the client. When ‘exit’ is typed at the client side, the program terminates.
Client2.java
// Client2 class that// sends data and receives alsoimportjava.io.*;importjava.net.*;classClient2 {publicstaticvoidmain(String args[])throwsException{// Create client socketSocket s =newSocket("localhost",888);// to send data to the serverDataOutputStream dos=newDataOutputStream(s.getOutputStream());// to read data coming from the serverBufferedReader br=newBufferedReader(newInputStreamReader(s.getInputStream()));// to read data from the keyboardBufferedReader kb=newBufferedReader(newInputStreamReader(System.in));String str, str1;// repeat as long as exit// is not typed at clientwhile(!(str = kb.readLine()).equals("exit")) {// send to the serverdos.writeBytes(str +"\n");// receive from the serverstr1 = br.readLine();System.out.println(str1);}// close connection.dos.close();br.close();kb.close();s.close();}}Command to compile the Client2.java file:
D:\Conversation Program>javac Client2.java
- Output:
To execute the Server2 and Client2 classes, run the Server2.java and Client2.java in two separate Command Prompt windows. Figure 1 shows the output of the Server2 and Client2 classes:
- Creating the Server Program: Let’s create a class named Server2.java to create server such that the server receives data from the client using a BufferedReader object and then sends a reply to the client using a PrintStream object.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.



