title: Java: Simple Client Server message exchange not working
body: <p>I am new to Java just started yesterday. I wrote a very simple client server java code. Client sends a message to server. The Server should display that message. And the Server should send a message to client after receiving the message. The client should display the message sent by server.</p>
  
  <p>Server Code,</p>
  
  <pre><code>import java.io.InputStreamReader;
  import java.io.BufferedReader;
  import java.io.OutputStreamWriter;
  import java.io.BufferedWriter;
  import java.net.Socket;
  import java.net.ServerSocket;
  
  public class CustomServer{
      public static void main(String[] args){
         final int SERVER_PORT_NUMBER = 8081;
  
         try{
           ServerSocket serverObj = new ServerSocket(SERVER_PORT_NUMBER);
           Socket clientSocketObj = serverObj.accept();
           BufferedReader clientInputStream = new BufferedReader(new InputStreamReader(clientSocketObj.getInputStream()));
           BufferedWriter clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocketObj.getOutputStream()));
  
          if(clientSocketObj != null){
              System.out.println("Client Connected to Server!");
  
              // Recieve Message from Client
              System.out.println("MESSAGE FROM CLIENT");
              System.out.println(clientInputStream.readLine());
  
              // Send Message to Client
              clientOutputStream.write("SERVER: Hello Client!");
  
              // Close Streams
              clientOutputStream.close();
              clientInputStream.close();
          }
          serverObj.close();
      }
      catch(Exception e){
          System.out.println(e);
      }
  }
  }
  </code></pre>
  
  <p>Client,</p>
  
  <pre><code>import java.io.BufferedWriter;
  import java.io.OutputStreamWriter;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.Socket;
  
  public class CustomClient{
      public static void main(String[] args){
          final String HOST_NAME = "127.0.0.1";
          final int SERVER_PORT_NUMBER = 8081;
  
          try{
             Socket clientSocket = new Socket(HOST_NAME, SERVER_PORT_NUMBER);
             BufferedWriter clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
             BufferedReader clientInputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
             System.out.println("Connecting....");
  
          if(clientSocket != null){
              System.out.println("Connected to Server!");
  
              // Send message to Server
              clientOutputStream.write("CLIENT: HELLO SERVER");
  
              // Recieve message from Server
              System.out.println("MESSAGE FROM SERVER");
              System.out.println(clientInputStream.readLine());
  
              // Close Streams
              clientInputStream.close();
              clientOutputStream.close();
          }
          clientSocket.close();
      }
      catch(Exception e){
          System.out.println(e);
      }
  }
  }
  </code></pre>
  
  <p>Neither the Server or Client receive the message. Stuck in some loop. Anyone know why?</p>