Java sockets: multiple client threads on same port on same machine?

44,287

Solution 1

Yes, however only one client will be able to connect per thread execution as written.

You can just put your server run() inside a while true loop to let multiple clients connect. Depending on the executor, they will execute either in series or parallel.

   public class Server extends Thread  
   {  
       ...  
       void run()  
       {  
           while(true){
              // serverSocket on "localhost", 1234  
              Socket clientSock = serverSocket.accept();  
              executor.execute(new ClientWorker(clientSock));  
           }
       }  
   } 

Solution 2

As long as you only have one object trying to bind the port for listening, then there's no problem with multiple clients connecting.

Solution 3

In this example, your Server accepts and handles one client connection at a time. You can have as many Clients as you want attempting to connect, but only one at a time will be handled.

It is not apparent whether your executor logic is multithreaded, since you didn't provide the implementation. If the executor delegates to a threadpool or something like that, you would need to make sure that your ClientWorker is thread-safe, as you will have multiple instances executing in parallel.

I am of course assuming that your Client is thread-safe as well, since your question is only concerning the Server.

Share:
44,287
espcorrupt
Author by

espcorrupt

Updated on October 05, 2020

Comments

  • espcorrupt
    espcorrupt over 3 years

    I am new to Socket programming in Java and was trying to understand if the below code is not a wrong thing to do. My question is:

    Can I have multiple clients on each thread trying to connect to a server instance in the same program and expect the server to read and write data with isolation between clients"

    public class Client extends Thread
    {
        ...
        void run()
        {
            Socket socket = new Socket("localhost", 1234);
            doIO(socket);  
        }
    }
    
    public class Server extends Thread
    {
        ...
        void run()
        {
            // serverSocket on "localhost", 1234
            Socket clientSock = serverSocket.accept();
            executor.execute(new ClientWorker(clientSock));
        }
    }
    

    Now can I have multiple Client instances on different threads trying to connect on the same port of the current machine?

    For example,

       Server s = new Server("localhost", 1234);
       s.start();
       Client[] c = new Client[10];
       for (int i = 0; i < c.length; ++i)
       {
            c.start();
       }
    
  • espcorrupt
    espcorrupt almost 14 years
    Thanks guys, I thought of the port as a single physical entity (like a wire) since it has a single number. So my thinking was it can be used by only one client socket, otherwise multiple client sockets could write into the same wire at a time. But from your answers, I think the port itself is made of multiple resources (say, like memory blocks) but the socket will be bound to one of those blocks probably indexed by some binding key.
  • user207421
    user207421 almost 14 years
    The port is just a number. It doesn't correspond to anything physical. A connection is defined by the tuple {protocol, source address, source port, target address, target port}. The client-side OS will take care of ensuring different outbound port numbers for each outgoing connection. So there is no problem in having multiple inbound connections to the same target host/port, even if they are all from the same client source host.