简体   繁体   中英

several threads from client side connecting to same serversocket, all on localhost

I have a server with a serversocket, and a client which is also a thread. in the main client, i creates 80 instances of the client thread, and each one of those opens a connection (using sockets) with the server. Both the client and the server are on the same computer, all using same localhost and port. The serversocket in the server side accepts only the first thread that connects to it, and throws an exception for the rest of the threads, because they all come from the same IP to the same port. How do I solve this? I need for the 80 threads to connect to the same server on the same computer (using same IP and port). PS. I'm also using multithreading, so after the serversocket accepts, it creates and starts a new thread to handle the client's request. I need to do this only in Java. I'll be very grateful for a quick response. Thanks in advance.

If you cant uniquely identify a client by its IP and PORT, you need to think of something else. Adding certain generated unique ID's when sending a packet helps. When the server receives the clients initial request it can then set that unique id when making a thread for the client.

To get something unique every time you could use:

UUID.randomUUID()

from the java.util.UUID; package .

And cause every client has the same IP and PORT I assume every client gets every response from the server. So the logic on seeing if the package is for the correct client needs to be on the client side.

If this is not what youre looking for please update your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM