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

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

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 May 13 '10 at 4:16 1 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 May 13 '10 at 10:16.

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)); } } }.

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).

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.

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 ci.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(" other side of the connection!"); And for reading use a BufferedReader.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions