title: Multi client chat application In java
body: <p>I just got started with socket programming so in order to improve my understandings of it I wanna build a multi-client chat application.</p>
  
  <p>They way I intend  to do it is the following:
  Once the application starts you have two choices: create server or join server.</p>
  
  <p>If you chose to create a server a new thread will start and host the server, then another thread will start which is gonna create a new client and automatically connect to the server just build.</p>
  
  <p>And here I've encountered the following problem.
  Each client can send messages to the server, but in order to keep them synchronized for all clients I was thinking to redirect the retrieved messages from the server to all clients as shown in the following diagram.
  <a href="https://i.stack.imgur.com/A15iQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A15iQ.jpg" alt="enter image description here"></a></p>
  
  <p>The thing is that when I try to listen and send on both client and server I get this errors.</p>
  
  <pre><code>java.net.SocketException: Connection reset
      at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
      at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
      at java.base/java.net.SocketInputStream.read(SocketInputStream.java:200)
      at java.base/java.io.DataInputStream.readUnsignedShort(DataInputStream.java:342)
      at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:594)
      at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:569)
      at parctice.Server.lambda$main$0(Server.java:32)
      at java.base/java.lang.Thread.run(Thread.java:835)
  </code></pre>
  
  <p>This is my server:</p>
  
  <pre><code>int port = 4444;
  
          try {
              ServerSocket serverSocket = new ServerSocket(port);
              System.out.println("server starts port = " + serverSocket.getLocalSocketAddress());
  
  
              while(true){
                  Socket socket = serverSocket.accept();
                  System.out.println("accepts : " + socket.getRemoteSocketAddress());
  
                  DataInputStream in = new DataInputStream(socket.getInputStream());
                  DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  
  
                  String[] message = {""};
  
                  new Thread(() -&gt; {
                      try {
                          while(in.available() &gt; 0){
                              System.out.println("SERVER &gt; " + in.readUTF());
                              message[0] = in.readUTF();
                          }
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }).start();
  
                  System.err.println(message[0]);
                  try {
                      out.writeUTF(message[0] + "REDIRECTED MESSAGE");
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>and my client:</p>
  
  <pre><code>int portNumber = 4444;
  System.out.println("CLIENT &gt;  Trying to connect to the server...");
  try {
      Socket socket = new Socket("localhost", portNumber);
  
      DataInputStream in = new DataInputStream(socket.getInputStream());
      DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  
  
      new Thread(() -&gt; {
          try {
              while(in.available() &gt; 0){
                  System.out.println("SERVER &gt; " + in.readUTF());
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }).start();
  
  
      try {
          out.writeUTF("test");
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>In the end I would like to ask you guys if you think that the logic that I  try to use is the right one and if not please let me know what's wrong and also if possible could you explain me why I am not being able to listen and send on both, client and server? Because when I try to send data through client and retrieve it on the server it works just fine, but I would like to accomplish both ways communication. </p>