I am developing an application that entails a single server and a large number of clients. I am using the Java Socket Programming API to accomplish this task. At the moment, I am considering the possibility of restructuring the entire project of my application, because I just do not think that it is structured in the most efficient way, and I would like to receive some recommendations on the optimal path.
Current implementation
I have one ServerSocket located on port 5000, and the stream containing the socket just works continuously and accepts any connection. Then it starts a new server thread (based on a synchronized table of available ports), which processes communication with this client, and then blocks again for ServerSocket.accept() .
Threads generated from this main thread also contain ServerSocket and are used as a means to handle multiple connections simultaneously.
Now the client thread simply connects to port 5000, receives the next available port as a response, then disconnects from port 5000 (by calling Socket.close() ) and reconnects to the port that the server said.
My question
Is this the most optimal way (or, even better, even reasonable?) To handle multiple clients on the same server? Or am I just opening ServerSocket on all available ports and just listening all the time? Perhaps something I haven't thought about yet?
Adding
I am trying to think in terms of very large client-server applications such as MMORPG or some kind of chat application in order to understand how much I can implement my implementation. For example, I try to ask myself: "Although this may work, would it be a good solution if there was a large user base in this application?" At the same time, it would be easier for me to understand the optimal nature of the solution if I could see how it will work on a large scale by, say, millions of users.
source share