title: Java socket will send but then not receive response
body: <p>I am currently learning <code>Java</code> and am playing around with socket programming.</p>
  
  <p>My goal is to implement request-response communication like <code>HTTP</code>. I can get one way communication from the client to the server. But when I program the client to listen for a response it causes the server to not print out the request. </p>
  
  <p>I've googled the issue and most posts mention using the <code>flush()</code> method, which I have tried. Anyone have any thoughts?</p>
  
  <p>Here is the client code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.Socket;
  
  class Client {
      public static void main(String args[]) throws IOException {
  
          // Get the port number from the first command line argument
  
          int port_number;
  
          if(args.length == 0) {
              port_number = 80;
          } else {
              port_number = Integer.parseInt(args[0]);
          }
  
          try (
  
              // Create a socket
              Socket socket = new Socket("localhost", port_number);
  
              // Input reader
              BufferedReader in = new BufferedReader(
                  new InputStreamReader(socket.getInputStream())
              );
  
              // Output writer
              PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
  
          ) {
  
              System.out.println("Sending request");
              out.print("Foo");
              out.flush();
  
              System.out.println("Sending Complete");
  
              // If this next section is commented out then the server prints the message
  
              StringBuilder message = new StringBuilder();
              int data;
  
              while((data = in.read()) != -1) {
                  message.append((char) data);
              }
  
              System.out.print(message);
  
          }
      }
  }
  </code></pre>
  
  <p>and here is my server code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server {
  
      public static void main(String args[]) throws IOException {
  
          // Get the port number from the first command line argument
          int portNumber = Integer.parseInt(args[0]);
  
          System.out.println("Listening on port " + portNumber);
          ServerSocket serverSocket = new ServerSocket(portNumber);
  
          while (true) {
  
              try (
                  Socket socket = serverSocket.accept();
                  PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                  BufferedReader in = new BufferedReader(
                      new InputStreamReader(socket.getInputStream())
                  );
              ){  
  
              StringBuilder message = new StringBuilder();
              int data;
  
              while((data = in.read()) != -1) {
                  message.append((char) data);
              }
  
              System.out.println(message);
  
              out.print("Bar");
  
              }
          }
      }
  }
  </code></pre>