Java Reference
In-Depth Information
Both methods throw a SocketException if the underlying socket implementation does
not recognize the SO_RCVBUF option. This might happen on a non-POSIX operating
system. The setReceiveBufferSize() method will throw an IllegalArgumentExcep
tion if its argument is less than or equal to zero.
SO_SNDBUF
DatagramSocket has methods to get and set the suggested send buffer size used for
network output:
public void setSendBufferSize ( int size ) throws SocketException
public int getSendBufferSize () throws SocketException
The setSendBufferSize() method suggests a number of bytes to use for buffering
output on this socket. Once again, however, the operating system is free to ignore this
suggestion. Consequently, you'll want to check the result of setSendBufferSize() by
immediately following it with a call to getSendBufferSize() to find out the real buffer
size.
Both methods throw a SocketException if the underlying native network software
doesn't understand the SO_SNDBUF option. The setSendBufferSize() method also
throws an IllegalArgumentException if its argument is less than or equal to zero.
SO_REUSEADDR
The SO_REUSEADDR option does not mean the same thing for UDP sockets as it does
for TCP sockets. For UDP, SO_REUSEADDR controls whether multiple datagram
sockets can bind to the same port and address at the same time . If multiple sockets are
bound to the same port, received packets will be copied to all bound sockets. This option
is controlled by these two methods:
public void setReuseAddress ( boolean on ) throws SocketException
public boolean getReuseAddress () throws SocketException
For this to work reliably, setReuseAddress() must be called before the new socket binds
to the port. This means the socket must be created in an unconnected state using the
protected constructor that takes a DatagramImpl as an argument. In other words, it
won't work with a plain vanilla DatagramSocket . Reusable ports are most commonly
used for multicast sockets, which will be discussed in the next chapter. Datagram chan‐
nels also create unconnected datagram sockets that can be configured to reuse ports, as
you'll see later in this chapter.
SO_BROADCAST
The SO_BROADCAST option controls whether a socket is allowed to send packets to
and receive packets from broadcast addresses such as 192.168.254.255, the local network
Search WWH ::




Custom Search