title: Java multi-thread sockets receive data only once
body: <p>I have to do a project in Java for an exam which has to have multi-client sockets.</p>
  <p>We have some example code for sockets from our teacher - and I modified it a bit. The problem is that it only receives data once, and then it stops. I found that out by printing the received data into the console.</p>
  <p>So here's my the code:</p>
  <p>Server.java</p>
  <pre><code>package socket;
  
  import database.models.PersoanaEntity;
  
  import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server {
      public static final int PORT = 27015;
  
      public void start() {
          new Thread(() -&gt; {
              try {
                  ServerSocket serverSocket = new ServerSocket(PORT);
                  Socket clientSocket = null;
                  boolean isClose = false;
  
                  System.out.println(&quot;Server is running&quot;);
                  while (!isClose) {
                      clientSocket = serverSocket.accept();
                      new Thread(new ServerThread(clientSocket)).start();
                  }
                  serverSocket.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }).start();
      }
  }
  </code></pre>
  <p>ServerThread.java</p>
  <pre><code>package socket;
  
  import database.models.PersoanaEntity;
  import org.json.JSONObject;
  
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.net.Socket;
  
  public class ServerThread extends Thread{
      private Socket socket = null;
      private ObjectInputStream in = null;
      private ObjectOutputStream out = null;
      PersoanaEntity loggedInPerson = new PersoanaEntity();
  
      public ServerThread(Socket socket) {
          this.socket = socket;
          try {
              //For receiving and sending data
              this.in = new ObjectInputStream(socket.getInputStream());
              this.out = new ObjectOutputStream(socket.getOutputStream());
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void run() {
          try {
              String received = this.in.readObject().toString();
              System.out.println(received);
              JSONObject jsonObject = new JSONObject(received);
              execute(received);
          } catch (IOException e) {
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }
      }
  
      private void execute(String message) {
          JSONObject jo = new JSONObject(message);
          ProcessInput pi = new ProcessInput();
          pi.processInput(jo, this.out);
      }
  }
  </code></pre>
  <p>ProcessInput.java</p>
  <pre><code>package socket;
  
  import database.daos.PersoanaDao;
  import database.models.PersoanaEntity;
  import jakarta.persistence.NoResultException;
  import org.json.JSONObject;
  
  import java.io.IOException;
  import java.io.ObjectOutputStream;
  import java.util.Optional;
  
  public class ProcessInput {
      void processInput(JSONObject data, ObjectOutputStream out) {
          switch (data.get(&quot;type&quot;).toString())
          {
              case &quot;LOGIN_USER&quot;:
                  PersoanaDao persoanaDao = new PersoanaDao();
                  String username = data.get(&quot;username&quot;).toString();
                  JSONObject json = new JSONObject();
                  try {
                      PersoanaEntity persoanaEntity = persoanaDao.get(username);
                      json.put(&quot;status_code&quot;, &quot;200&quot;);
                  } catch(Exception e) {
                      json.put(&quot;status_code&quot;, &quot;404&quot;);
                  }
                  try {
                      out.writeObject(json.toString());
                  } catch (IOException e)
                  {
                      System.out.println(e.toString());
                  }
                  break;
          }
      }
  }
  </code></pre>
  <p>I won't add the code from the client side because I'm quite sure the problem is from the server after doing some tests. I think that it happens because the run method is only called once, AFAIK, and that is when .start() is called from the Thread.</p>
  <p>Then it won't ever be called again so there will never be data in the received string.</p>
  <p>Am I right?</p>
  <p>If so, what's the fix?</p>
  <p>If not, why am I getting data only once and how can I fix it?</p>
  <p>//edit: I will add the client just in case:</p>
  <pre><code>package socket;
  
  import org.json.JSONObject;
  
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.net.Socket;
  import java.util.Scanner;
  
  public enum Client implements Runnable {
      INSTANCE;
  
      public static final int PORT = 27015;
  
      public Socket getSocket() {
          return socket;
      }
  
      public ObjectInputStream getInputStream() {
          return inputStream;
      }
  
      public ObjectOutputStream getOutputStream() {
          return outputStream;
      }
  
      private Socket socket = null;
      private ObjectInputStream inputStream = null;
      private ObjectOutputStream outputStream = null;
      private String name;
  
      Client() {
          try {
              socket = new Socket(&quot;localhost&quot;, PORT);
              this.outputStream = new ObjectOutputStream(socket.getOutputStream());
              this.inputStream = new ObjectInputStream(socket.getInputStream());
          } catch(Exception e) {
              System.out.println(&quot;Connection error&quot;);
          }
      }
  
      public void run() {
          System.out.println(&quot;Client socket running&quot;);
  
          //For receiving data
          boolean isClose = false;
          while (!isClose) {
  
              }
          try {
              socket.close();
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
      }
  }
  </code></pre>