In this chapter, we examined the nature of the UDP protocol and how Java supports it. We contrasted TCP and UDP to provide some guidance in deciding which protocol was best for a given problem.
We started with a simple UDP client/server to demonstrate how the DatagramPacket
and DatagramSocket
classes are used. We saw how the InetAddress
class was used to obtain addresses used by sockets and packets.
The DatagramChannel
class supports using NIO techniques in a UDP environment, which can be more efficient than using the DatagramPacket
and DatagramSocket
approach. The approach used a byte buffer to hold messages that were sent between a server and a client. This example illustrated many of the techniques that were developed in Chapter 3, NIO Support for Networking.
This was followed by a discussion of how UDP multicasting works. This provides a simple technique to broadcast a message to members of a group. The use of the MulticastSocket
, DatagramChannel
, and MembershipKey
classes were...