5.3. DatagramSocket Class

The DatagramSocket class provides access to a UDP socket, which allows UDP packets to be sent and received. A DatagramPacket is used to represent a UDP packet, and must be created prior to receiving any packets. The same DatagramSocket can be used to receive packets as well as to send them. However, read operations are blocking, meaning that the application will continue to wait until a packet arrives. Since UDP packets do not guarantee delivery, this can cause an application to stall if the sender does not resubmit packets. You can use multiple threads of execution, discussed in Chapter 7, or as of JDK1.1, you can use nonblocking I/O to avoid this problem, as shown in the EchoClient example discussed in Section 5.7.2 ...

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.