5.4. Listening for UDP Packets

Before an application can read UDP packets sent to it by remote machines, it must bind a socket to a local UDP port using DatagramSocket, and create a DatagramPacket that will act as a container for the UDP packet's data. Figure 5-3 shows the relationship between a UDP packet, the various Java classes used to process it, and the actual application.

Figure 5-3. UDP packets are received by a DatagramSocket and translated into a DatagramPacket object.

When an application wishes to read UDP packets, it calls the DatagramSocket.receive method, which copies a UDP packet into the specified DatagramPacket. The contents of ...

Get Java™ Network Programming and Distributed Computing 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.