O'Reilly logo

Special Edition Using Java™ 2 Enterprise Edition by Mark Wutka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Tip

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required