Channel support for UDP
The DatagramChannel class provides additional support for UDP. It can support nonblocking interchanges. The DatagramChannel class is derived from the SelectableChannel class that makes multithreaded application easier. We will examine its use in Chapter 7, Network Scalability.
The DatagramSocket class binds a channel to a port. After this class is used, it is no longer used directly. Using the DatagramChannel class means, we do not have to use datagram packets directly. Instead, data is transferred using an instance of the
ByteBuffer class. This class provides several convenient methods to access its data.
To demonstrate the use of the DatagramChannel class, we will develop an echo server and client application. The server will wait for a message from a client, and then send it back to the client.
The UDP echo server application
The UDP echo server application declaration follows and uses port 9000. In the main method a try-with-resources block opens the channel and...