Search code examples
javaniodatagram

DatagramChannel receive may not return SocketAddress


According to the Javadoc for DatagramChannel the receive method:

public abstract SocketAddress receive(ByteBuffer dst)
                           throws IOException

may return null if the DatagramChannel is in non-blocking mode. Other than changing to blocking mode is there a way to ensure I get the SocketAddress for the data I read?


Solution

  • may return null if the DatagramChannel is in non-blocking mode.

    Read on.

    Returns ... null if this channel is in non-blocking mode and no datagram was immediately available.

    If it returned null there was no data read. The situation corresponds to SocketChannel.read() returning zero.