Search code examples
javasocketstcpudpnio

Nio Selector.select IO readiness


In java NIO, does Selector.select() guarantee that at least one entire UDP datagram content is available at the Socket Channel, or in theory Selector could wake when there is less then a datagram, say couple of bytes ?

What happens if transport protocol is TCP, with regards to Selector.select(), is there difference to UDP ?

From the API: Selects a set of keys whose corresponding channels are ready for I/O operations.

It doesn't however specify what ready means.

So my questions:

  • how incoming datagrams/streams go from hardware to Java application Socket (Channels).

  • when using UDP or TCP client, should one assume that at least one datagram is received or Selector could wake when there is only a part of datagram available ?


Solution

  • It doesn't however specify what ready means.

    So my questions:

    • how incoming packages/streams go from hardware to Java application Socket (Channels).

    They arrive at the NIC where they are buffered and then passed to the network protocol stack and from there to the socket receive buffer. From there they are retrieved when you call read().

    • when using UDP or TCP client, should one assume that at least one package is received

    You mean packet. Actually in the case of UDP you mean datagram. You can assume that an entire datagram has been received in the case of UDP.

    or Selector could wake when there is only a part of [packet] available?

    In the case of TCP you can assume that either at least one byte or end of stream is available. There is no such thing as a 'package' or 'packet' or 'message' at the TCP level.