title: Client-Server communication using sockets blocked at client-side
body: <p>I'm writing a simple <strong>local</strong> client-server program that should do the following:</p>
  
  <ol>
  <li>Client sends his name to server ("client1")</li>
  <li>Server returns a String saying "hello client1"</li>
  </ol>
  
  <p>The problem I'm having is that the "hello client1" message is never retrieved, and instead the program just keeps running without throwing any exceptions or errors. I'm guessing it has something to do with the readLine method that blocks the bufferedReader, but I haven't been able to find a proper solution to the problem yet. To run the program locally (see Main), I use a serverThread and a clientThread, maybe that could be part of the problem too.</p>
  
  <p><strong>NOTE: In the following code, I put a comment block on the lines that seem to cause the problem, the rest of the code runs fine:</strong></p>
  
  <p>Server:</p>
  
  <pre><code>public class Serverside implements Runnable{
      PrintStream out;
      int port;
  
      Serverside(int port){
          out = new PrintStream(System.out);
          this.port = port;
      }
  
      void start(){
          try{
              ServerSocket serverSocket = new ServerSocket(port);
              Socket connSocket = serverSocket.accept();
  
              BufferedReader clientInput = new BufferedReader(new InputStreamReader(connSocket.getInputStream()));
              DataOutputStream clientOutput = new DataOutputStream(connSocket.getOutputStream());
  
              String clientName = clientInput.readLine();
              String reply = "Hello " + clientName + "\n";
  
              //clientOutput.writeBytes(reply);
  
              clientOutput.close();
              clientInput.close();
              connSocket.close();
              serverSocket.close();
          }catch(Exception e){
              out.println("Server: An exception has been thrown: " + e.getMessage());
          }
      }
  
      @Override
      public void run() {
          out.println("Server running");
          start();
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>public class ClientSide implements Runnable {
  
      String hostname;
      String clientName;
      int port;
  
      public ClientSide(String name, int port) {
          hostname = "localhost";
          this.port = port;
          clientName = name;
      }
  
      void start(){
          try{
              Socket connSocket = new Socket(hostname, port);
  
              BufferedReader serverInput = new BufferedReader(new InputStreamReader(connSocket.getInputStream()));
              DataOutputStream serverOutput = new DataOutputStream(connSocket.getOutputStream());
  
              serverOutput.writeBytes(clientName);
  
              //String reply = serverInput.readLine();
              //System.out.println("Server says: " + reply);
  
              serverOutput.close();
              serverInput.close();
              connSocket.close();
          }catch(Exception e){
              System.out.println("Client: An exception has been thrown: "  + e.getStackTrace().toString());
          }
  
      }
  
      @Override
      public void run() {
          System.out.println("Client-side running");
          start();
      }
  }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>public class Main {
  
      static final int PORT = 20000;
  
      void start() {
          Serverside server = new Serverside(PORT);
          ClientSide client = new ClientSide("Elias",PORT);
  
          Thread serverThread = new Thread(server);
          Thread clientThread = new Thread(client);
  
          clientThread.start();
          serverThread.start();
      }
  
      public static void main(String[] args) {
          new Main().start();
      }
  
  }
  </code></pre>