Using Datagram Sockets

UDP datagram sockets are easy to use, after you understand how they work. A datagram socket has a specific port number assigned to it. You can specify the port number when you create the socket, or use port number 0 to allocate a random free port number. When you send a datagram packet, you can send it as a broadcast, meaning it goes to every host on the network, or you can send it to a specific host. The important thing to keep in mind is that you always send it to a port number, and not necessarily the same port number as your datagram socket.


When you send a datagram packet, you must always specify a host address. To broadcast a packet, set the host portion of the IP address to all 1s. For instance, if your network ...

Get Special Edition Using Java™ 2 Enterprise Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.