Java Reference
In-Depth Information
These two methods each throw a SocketException if the underlying socket imple‐
mentation does not support the SO_TIMEOUT option. The setSoTimeout() method
also throws an IllegalArgumentException if the specified timeout value is negative.
SO_RCVBUF and SO_SNDBUF
TCP uses buffers to improve network performance. Larger buffers tend to improve
performance for reasonably fast (say, 10Mbps and up) connections whereas slower, dial-
up connections do better with smaller buffers. Generally, transfers of large, continuous
blocks of data, which are common in file transfer protocols such as FTP and HTTP,
benefit from large buffers, whereas the smaller transfers of interactive sessions, such as
Telnet and many games, do not. Relatively old operating systems designed in the age of
small files and slow networks, such as BSD 4.2, use two-kilobyte buffers. Windows XP
used 17,520 byte buffers. These days, 128 kilobytes is a common default.
Maximum achievable bandwidth equals buffer size divided by latency. For example, on
Windows XP suppose the latency between two hosts is half a second (500 ms). Then
the bandwidth is 17520 bytes / 0.5 seconds = 35040 bytes / second = 273.75 kilobits /
second. That's the maximum speed of any socket, regardless of how fast the network is.
That's plenty fast for a dial-up connection, and not bad for ISDN, but not really adequate
for a DSL line or FIOS.
You can increase speed by decreasing latency. However, latency is a function of the
network hardware and other factors outside the control of your application. On the
other hand, you do control the buffer size. For example, if you increase the buffer size
from 17,520 bytes to 128 kilobytes, the maximum bandwidth increases to 2 megabits
per second. Double the buffer size again to 256 kilobytes, and the maximum bandwidth
doubles to 4 megabits per second. Of course, the network itself has limits on maximum
bandwidth. Set the buffer too high and your program will try to send and receive data
faster than the network can handle, leading to congestion, dropped packets, and slower
performance. Thus, when you want maximum bandwidth, you need to match the buffer
size to the latency of the connection so it's a little less than the bandwidth of the network.
You can use ping to check the latency to a particular host manually, or
you can time a call to InetAddress.isReachable() from inside your
program.
The SO_RCVBUF option controls the suggested send buffer size used for network input.
The SO_SNDBUF option controls the suggested send buffer size used for network out‐
put:
public void setReceiveBufferSize ( int size )
throws SocketException , IllegalArgumentException
Search WWH ::




Custom Search