title: Java Socket - Inverse Communication
body: <p>I have many clients that are waiting for server messages. So the client make accept() and wait for server. When server have messages, open a connection to the client and send messages, after that, close the communication and the cycle restart.</p>
  
  <p>I've seen usually the inverse approach, where the server do accept() and client connect to it. I've wrote this code but the client (that do accept() ) is blocked on point <strong>3</strong> and the server (that create the connection to the client) is blocked on point <strong>2</strong>.</p>
  
  <p>Sure i have some problems in my code (dont know where), but... this is the correct way ?</p>
  
  <p><strong>The client (that do accept() and wait for new messages)</strong></p>
  
  <pre><code>try {
                  System.out.println("Waiting..");
                  receiver = serverSocket.accept();
  
                  System.out.println("1");
                  ObjectInput fromServerReader = new ObjectInputStream(receiver.getInputStream());
                  ObjectOutputStream toServerWriter = new ObjectOutputStream(receiver.getOutputStream());
                  System.out.println("2");
                  toServerWriter.writeObject("dummy");
                  toServerWriter.flush();
                  System.out.println("3");
  
                  ScheduledEvent scheduledEvent = (ScheduledEvent) fromServerReader.readObject();
  
                  System.out.println("4");
  
                  receiver.close();
                  System.out.println("5");
  
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  // Should never happen
              }
  </code></pre>
  
  <p><strong>The server (that when have new message to send to client, create the
   connection)</strong></p>
  
  <pre><code>try {
              InetAddress address = InetAddress.getByName(sendToUser
                      .getMachineName());
              socket = new Socket(address, port);
  
              log.debug("1");
              ObjectOutputStream toClientWriter = new ObjectOutputStream(
                      socket.getOutputStream());
              ObjectInputStream fromClientReader = new ObjectInputStream(socket.getInputStream());
              log.debug("2");
  
              String read = (String)fromClientReader.readObject();
              log.debug("3");
  
              // Compose the message
              ScheduledEvent scheduledEvent = new ScheduledEvent();
              scheduledEvent.setSubject(event.getSubject());
              scheduledEvent.setMessage(event.getText());
  
              log.debug("4");
              toClientWriter.writeObject(scheduledEvent);
              toClientWriter.flush();
              log.debug("5");
  
              socket.close();
              log.debug("6");
          } catch (UnknownHostException e) {
              // TODO handle
              e.printStackTrace();
          } catch (IOException | ClassNotFoundException e) {
              // TODO handle
              e.printStackTrace();
          }
  </code></pre>