Summary

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,

Get Learning Network Programming with Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.