title: How to automatically update server and client side in java
body: <p>I'm learning distributed systems basics and currently I'm trying to do a simple yet realistic messenger between <strong>one</strong> server and <strong>one</strong> client. <strong>What I do intend is that on each endpoint socket side (Server and Client) text automatically updates (like a real "messaging app")</strong>. In other words, I want that the moment I write and "send" the message, it automatically appears on recipient side. What I have now follows this schema:</p>
  
  <ol>
  <li>I send a message (let's assume from client)</li>
  <li>To see that message on Server's side I need to reply first (because Server's BufferedReader / Client's PrintWriter is only read after asking for the answer)</li>
  </ol>
  
  <p>My code:</p>
  
  <pre><code>public class ClientSide {
      public static void main(String [] args){
          String host_name = args[0];
          int port_number = Integer.parseInt(args[1]);
  
          try {
              Socket s = new Socket(host_name, port_number);
  
              PrintWriter out =
                      new PrintWriter(s.getOutputStream(), true);
  
              BufferedReader in = 
                      new BufferedReader(
                          new InputStreamReader(s.getInputStream()));
  
              BufferedReader stdIn = 
                      new BufferedReader(
                          new InputStreamReader(System.in));
  
  
              String answer;
  
              while ((answer = stdIn.readLine()) != null) {
                  out.println(answer);
                  System.out.println("\nlocalhost said\n\t" + in.readLine());
              }
  
          } catch (IOException ex) {
              Logger.getLogger(ClientSide.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  
  
  public class ServerSide {
      public static void main(String [] args){
          int port_number = Integer.parseInt(args[0]);
  
          try {
              ServerSocket ss = new ServerSocket(port_number);
              Socket tcp = ss.accept();
  
              PrintWriter out = 
                      new PrintWriter(tcp.getOutputStream(), true);
  
              BufferedReader in =
                      new BufferedReader(
                          new InputStreamReader(tcp.getInputStream()));
  
              BufferedReader stdIn = 
                      new BufferedReader(
                          new InputStreamReader(System.in));
  
              String answer;
              while ((answer = stdIn.readLine()) != null){
                  out.println(answer);
                  System.out.println("\nClient said\n\t" + in.readLine());
              }
  
          } catch (IOException ex) {
              Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p>How can I do this? Does it involve advanced knowledge on the matter?
  Thanks in advance. </p>