Java Reference
In-Depth Information
• SO_BINDADDR
• SO_TIMEOUT
• SO_LINGER
• SO_SNDBUF
• SO_RCVBUF
• SO_KEEPALIVE
• OOBINLINE
• IP_TOS
The funny-looking names for these options are taken from the named constants in the
C header files used in Berkeley Unix where sockets were invented. Thus, they follow
classic Unix C naming conventions rather than the more legible Java naming conven‐
tions. For instance, SO_SNDBUF really means “Socket Option Send Buffer Size.”
TCP_NODELAY
public void setTcpNoDelay ( boolean on ) throws SocketException
public boolean getTcpNoDelay () throws SocketException
Setting TCP_NODELAY to true ensures that packets are sent as quickly as possible
regardless of their size. Normally, small (one-byte) packets are combined into larger
packets before being sent. Before sending another packet, the local host waits to receive
acknowledgment of the previous packet from the remote system. This is known as
Nagle's algorithm . The problem with Nagle's algorithm is that if the remote system
doesn't send acknowledgments back to the local system fast enough, applications that
depend on the steady transfer of small parcels of information may slow down. This issue
is especially problematic for GUI programs such as games or network computer appli‐
cations where the server needs to track client-side mouse movement in real time. On a
really slow network, even simple typing can be too slow because of the constant buffer‐
ing. Setting TCP_NODELAY to true defeats this buffering scheme, so that all packets
are sent as soon as they're ready.
setTcpNoDelay(true) turns off buffering for the socket. setTcpNoDelay(false) turns
it back on. getTcpNoDelay() returns true if buffering is off and false if buffering is
on. For example, the following fragment turns off buffering (that is, it turns on
TCP_NODELAY) for the socket s if it isn't already off:
if (! s . getTcpNoDelay ()) s . setTcpNoDelay ( true );
These two methods are each declared to throw a SocketException , which will happen
if the underlying socket implementation doesn't support the TCP_ NODELAY option.
Search WWH ::




Custom Search