Java Reference
In-Depth Information
5.3
Performance Implications
The TCP implementation's need to copy user data into SendQ for potential retransmission
also has implications for performance. In particular, the sizes of the SendQ and RecvQ buffers
affect the throughput achievable over a TCP connection. Throughput refers to the rate at which
bytes of user data from the sender are made available to the receiving program; in programs
that transfer a large amount of data, we want to maximize this rate. In the absence of network
capacity or other limitations, bigger buffers generally result in higher throughput.
The reason for this has to do with the cost of transferring data into and out of the buffers
in the underlying implementation. If you want to transfer
n
n
bytes of data (where
is large),
it is generally much more ecient to call write() once with a buffer of size
n
than it is to
times with a single byte. 1 However, if you call write() with a size parameter that is
much larger than SQS , the system has to transfer the data from the user address space in SQS -
sized chunks. That is, the socket implementation fills up the SendQ buffer, waits for data to
be transferred out of it by the TCP protocol, refills SendQ , waits some more, and so on. Each
time the socket implementation has to wait for data to be removed from SendQ , some time
is wasted in the form of overhead (a context switch occurs). This overhead is comparable to
that incurred by a completely new call to write() . Thus, the effective size of a call to write()
is limited by the actual SQS . For reading from the InputStream , the same principle applies:
however large the buffer we give to read() , it will be copied out in chunks no larger than RQS ,
with overhead incurred between chunks.
If you are writing a program for which throughput is an important performance metric,
you will want to change the send and receive buffer sizes using the setSendBufferSize()
and setReceiveBufferSize() methods of Socket . Although there is always a system-imposed
maximum size for each buffer, it is typically significantly larger than the default on modern
systems. Remember that these considerations apply only if your program needs to send an
amount of data significantly larger than the buffer size, all at once. Note also that these factors
may make little difference if the program deals with some higher-level stream derived from
the Socket 's basic input stream (say, by using it to create an instance of FilterOutputStream or
PrintWriter ), which may perform its own internal buffering or add other overhead.
call it
n
5.4
TCP Socket Life Cycle
When a new instance of the Socket class is created—either via one of the public constructors or
by calling the accept() method of a ServerSocket —it can immediately be used for sending and
receiving data. That is, when the instance is returned, it is already connected to a remote
peer and the opening TCP message exchange, or handshake, has been completed by the
implementation.
1 The same thing generally applies to reading data from the Socket 's InputStream , although calling read()
with a larger buffer does not guarantee that more data will be returned.
 
Search WWH ::




Custom Search