Communication between client and server

Q. Write a Java Socket program that runs on a server which accepts a message from client & server gives the response to the client.

Answer:

It is like a small chat application. In this example the server and client both send the messages to each other.

ClientMachine.java

import java.io.*;
import java.net.*;
public class ClientMachine
{
     public static void main(String[] args) throws Exception
     {
          Socket sock = new Socket("localhost", 3000);
          //reading from keyboard (keyRead object)
          BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
          //sending to client (pwrite object)
          OutputStream ostream = sock.getOutputStream();
          PrintWriter pwrite = new PrintWriter(ostream, true);

          // receiving from server ( receiveRead  object)
          InputStream istream = sock.getInputStream();
          BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

          System.out.println("Start the chatting, type and press Enter key");

          String receiveMessage, sendMessage;               
          while(true)
          {
               sendMessage = keyRead.readLine();  // keyboard reading
               pwrite.println(sendMessage);       // sending to server
               pwrite.flush();                    // flush the data
               if((receiveMessage = receiveRead.readLine()) != null) //receive from server
               {
                    System.out.println(receiveMessage); // displaying at DOS prompt
               }         
          }               
     }                    
}


ServerMachine.java

import java.io.*;
import java.net.*;
public class ServerMachine
{
     public static void main(String[] args) throws Exception
     {
          ServerSocket sersock = new ServerSocket(3000);
          System.out.println("Server  ready for sending message...");
          Socket sock = sersock.accept( );                          
          //reading from keyboard (keyRead object)
          BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
          //sending to client (pwrite object)
          OutputStream ostream = sock.getOutputStream();
          PrintWriter pwrite = new PrintWriter(ostream, true);

          //receiving from server ( receiveRead  object)
          InputStream istream = sock.getInputStream();
          BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

          String receiveMessage, sendMessage;               
          while(true)
          {
               if((receiveMessage = receiveRead.readLine()) != null)  
               {
                    System.out.println(receiveMessage);         
               }         
               sendMessage = keyRead.readLine();
               pwrite.println(sendMessage);             
               pwrite.flush();
          }               
     }                    
}


Output:

Client machine:

cilent msg

Server machine:  
       
server msg