Search code examples
javasocketsnio

Java NIO Sockets clients request processing


I am trying to understand the NIO Sockets model and I have a few questions that I want to clear up.

  1. Is it possible that you can register more than one Server channels with selector i.e.

    serverChannel_1.register(selector, SelectionKey.OP_ACCEPT);
    serverChannel_2.register(selector, SelectionKey.OP_ACCEPT);
    // and so on ....? 
    

    So, by registering two ServerSocketChannel are you having two servers on the same machine on different ports?

  2. What are the "keys" in this following nio socket model:

enter image description here

From what I can tell is that the long client I/O requests are divided into small chunks and handled one after another. i.e. say 10% of request from client 1, 2, 3 is processed at a time and loop starts again?


Solution

    1. Yes.

    2. A SelectionKey Is the result of registering a channel with a selector. It represents the registration, if you like. It has as properties the channel that was registered, the event(s) it was registered for, and the event(s) if any that are currently ready.

      From what I can tell is that the long client I/O requests are divided into small chunks and handled one after another. i.e. say 10% of request from client 1, 2, 3 is processed at a time and loop starts again?

    No. How you handle client I/O is entirely up to you. All the Selector does is tell you which events are ready on which channels.