sockets - How can I avoid blocking with Java ServerSocket? -


I am working on a socket listener who has to listen to 2 types of data (port 80 and port 81) on 2 ports. I

These data are very similar to the type of operation that is done on the data and are just different because they reach different ports. I went ahead and coded an implementation using Java's ServerSet class, only to understand later that the acceptance of the ServerSocket class () method and can not handle my implementation, I now implement it using Java NIO But after going through some tutorials, I feel more confused than how I started. It would be great if a person can walk me through the whole process, even in the phony code or the texts, "what to do next" is what this is what I am planning to achieve.

Listen, calling on two similar threads as usual for 2 ports. (Non-blocked) connects a remote device from some network location, sends data and then disconnects.

I think that if the NIO can be used to set up a server to listen to the port, port 80 can be achieved on the porthost; it's easy.

Cheers

It is said to NIO when you have several thousands of connections is needed.

Otherwise, I suggest using several threads. For each port (and its related ServerSocket ), create a thread that accept the call () in a loop. These calls will be blocked, but this is fine because other threads are running, taking care of any available tasks.

When a new socket is accepted, then create another thread that is dedicated, the connection depends on this application, but generally this thread socket (one block) Operation), and execute the requested work, the result will be written back to the socket.

This architecture is simple enough for connections and programming models on many hundreds of desktop platforms, unless each connection is independent and independent of others (it avoids concurrent issues). Submission of the NIO will provide more scalability, but requires very complexity.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -