views:

361

answers:

5

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();
   }
+1  A: 

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

Jherico
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.
espcorrupt
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.
EJP
A: 

Yes, it doesn't matter whether your clients are local or remote. The important thing in your example is that ClientWorker is thread-safe, as your server will have multiple instances of that class (one for each client connection).

Greg Harman
A: 

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.

danben
+1  A: 

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));  
           }
       }  
   } 
patros
A: 

So. To begin:

You can accept more clients with one serversocket, because you accept only one in the run-method. You have just to call accept() a second time.

Then, you in your for loop: first you have to create each time a new Client object. Then you can call c[i].start(); and not c.start().

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

Yes you can. Just create new Threads and run them. This should work perfectly.

expect the server to read and write data with isolation between clients

You can use your experience of the basic IO techniques like with file-io:

OutputStream os = socket.getOutputStream();
PrintStream pw = new PrintStream(os, true); // Or PrintWriter, I don't know what the best one is.
pw.println("Hello, other side of the connection!");

And for reading use a BufferedReader.

Martijn Courteaux