title: Can two different socket instances listen to same TCP port ( Port already in use )
body: <p>I have a TcpServer class that is responsible to, well, act like a tcp server. You can find the class below :</p>
  
  <pre><code>public class TcpServer {
      private ServerSocket serverSocket;
      private Socket socket;
      private int locallyBoundPort;
  
      public TcpServer() {
  
  
      }
  
      public TcpServer(int locallyBoundPort) {
          try {
              this.serverSocket = new ServerSocket(locallyBoundPort);
              serverSocket.setReuseAddress(true);
          } catch (IOException e) {
              System.out.println("Error at binding to port TCP : " + locallyBoundPort + "...cause : " + e.getMessage());
          }
          socket = null;
  
      }
  
      public void accept() {
          try {
              socket = serverSocket.accept();
              socket.setReuseAddress(true);
          } catch (IOException e) {
              System.out.println("Error at accept : " + locallyBoundPort);
          }
      }
  
  
  
      public void send(Data data) throws IOException {
          if(socket != null) {
  
              ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
              out.writeObject(data);
  
          }
      }
  
      public Data receive() throws ClassNotFoundException, IOException {
          if(socket != null) {
  
                  ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                  return (Data) in.readObject();
  
          } else {
              return null;
          }
      }
  
      public boolean bind(int port) throws IOException {
          try {
              this.serverSocket = new ServerSocket(port);
              this.locallyBoundPort = port;
          } catch(IOException e) {
              return false;
          }
          return true;
  
      }
  
      public void close() {
          try {
              serverSocket.close();
              socket.close();
          } catch (IOException e) {
              OzumUtils.print("IOException in close, TcpServer");
          }
  
      }
  
      public int getLocallyBoundPort() {
          return locallyBoundPort;
      }
  
      public Socket getSocket() {
          return socket;
      }
  
      public ServerSocket getServerSocket() {
          return serverSocket;
      }
  }
  </code></pre>
  
  <p>And I have a code piece that does this :</p>
  
  <pre><code>TcpServer tcpServer = new TcpServer(LocalPort);
  while(1)
  {
      tcpServer.accept();
      Thread thread = new Thread(new runnable(tcpServer));
      thread.start();
      tcpServer = new TcpServer(LocalPort);
  }
  </code></pre>
  
  <p>However I am getting a port already in use error. I thought two different socket instances could listen to the same port as multiplexing allows two connections through the same port when the connector has different ip or port ?
  What am I missing?</p>