title: Socket - server does not receive client messages
body: <p>I'm new in client/server programming so I tried to write a simple chat program peer-to-peer so each program has it's own client and server and the programs are directly connected something like this.</p>
  
  <pre><code>  Program1                   Program2
  |----------|               |----------|
  |--Client--|--------------&gt;|--Server--|
  |----------|  connections  |----------|
  |--Server--|&lt;--------------|--Client--|
  |----------|               |----------|
  </code></pre>
  
  <p>The problem is that the servers and clients are connected but the messages simply don't reach the servers.</p>
  
  <p>I followed line by line the code with the debugger and noticed that after the (correct) initialization of the server, when it reaches the second <code>writer.readLine()</code>, the debugger just stop following the code like the thread is waiting for something to happen and the loop that should get and print the messages simply doesn't run.
  Server:</p>
  
  <pre><code>@Override
  public void run() {
      try {
          // the following code is executed
          server = new ServerSocket(port);
          port = server.getLocalPort();
          logMessage("[Server]Server started.");
          logMessage("[Server]Server is waiting on port: " + this.port + ".");
          while(true) {
              logMessage("[Server]Waiting for connection...");
              client = server.accept();
              logMessage("[Server]Client connecting on port: " + port + ".");
              logMessage("[Server]Client ip: " + client.getInetAddress());
              requestConnection(); // Here I ask the user if he want to accept the client or disconnect
              try {
                  synchronized(this) {
                      this.wait(); // Waiting for the user responce...
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
  
              // When the user write the answer the thread wake up!
              PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
              if(!connectionAccepted.toUpperCase().equals("Y")) {
                  writer.println(Connection.REFUSED.ordinal());
                  writer.flush();
                  writer.close();
                  client.close();
                  System.out.println("Connection refused.");
                  continue;
              }
  
              writer.println(Connection.ACCEPTED.ordinal());
              writer.flush();
              System.out.println("Connection accepted.");
  
              BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
              message = reader.readLine(); // This code get the port where the client of this program has to connect to
              GUI.connect(client.getInetAddress().getHostName(), Integer.parseInt(message));
              message = reader.readLine(); // Here is where the debug stop
  
              while(!message.equals("/disconnect")) {
                  if(message != null)
                      logMessage(message);
                  message = reader.readLine();
                  System.out.println("Waiting for message..."); // This method should be called several times but in the console there is no message
              }
              client.close();
          }
      } catch (IOException e) {
          logStackTrace(e);
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>@Override
  public void run() {
      try {
          client = new Socket(host, port);
  
          BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
          String message = reader.readLine();
          logMessage("[Client]Message recieved: " + message);
          try {
              if(Integer.parseInt(message) == Connection.ACCEPTED.ordinal()) {
                  logMessage("[Client]Connection accepted.");
              } else {
                  logMessage("[Client]Connection refused.");
              }
          } catch(NumberFormatException e) {
              logStackTrace(e);
              reader.close();
          }
          writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
          writer.println(GUI.getServer().getPort());
          writer.flush();
  
      } catch (IOException e) {
          logStackTrace(e);
      }
  }
  </code></pre>
  
  <p>"Send" method of the client:</p>
  
  <pre><code>public void send(String message) {
      writer.println(message);
      writer.flush();
  }
  </code></pre>