分类: 系统运维
2012-06-18 16:15:56
TCP obviously handles both types of data, but different algorithms come into play for each.
Interactive Input
Let's
look at the flow of data when we type an interactive command on an
Rlogin connection. Many newcomers to TCP/IP are surprised to find that
each interactive keystroke normally generates a data packet. That is,
the keystrokes are sent from the client to the server 1 byte at a time
(not one line at a time). Furthermore, Rlogin has the remote system (the
server) echo the characters that we (the client) type. This could
generate four segments: (1) the interactive keystroke from the client,
(2) an acknowledgment of the keystroke from the server, (3) the echo of
the keystroke from the server, and (4) an acknowledgment of the echo
from the client.
Normally, however, segments 2 and 3 are combined-the acknowledgment of the keystroke is sent along with the echo.
Delayed Acknowledgments
Normally
TCP does not send an ACK the instant it receives data. Instead, it
delays the ACK, hoping to have data going in the same direction as the
ACK, so the ACK can be sent along with the data. (This is sometimes
called having the ACK piggyback with the data.) Most implementations use
a 200-ms delay-that is, TCP will delay an ACK up to 200 ms to see if
there is data to send with the ACK.
The Host Requirements RFC states that TCP should implement a delayed ACK but the delay must be less than 500 ms.
Nagle Algorithm
We
saw that 1 byte at a time normally flows from the client to the server
across an Rlogin connection. This generates 41-byte packets: 20 bytes
for the IP header, 20 bytes for the TCP header, and 1 byte of data.
These small packets (called tinygrams) are normally not a problem on
LANs, since most LANs are not congested, but these tinygrams can add to
congestion on wide area networks. A simple and elegant solution was
proposed in RFC 896 [Nagle 1984], called the Nagle algorithm.
This
algorithm says that a TCP connection can have only one outstanding
small segment that has not yet been acknowledged. No additional small
segments can be sent until the acknowledgment is received. Instead,
small amounts of data are collected by TCP and sent in a single segment
when the acknowledgment arrives. The beauty of this algorithm is that it
is self-clocking: the faster the ACKs come back, the faster the data is
sent. But on a slow WAN, where it is desired to reduce the number of
tinygrams, fewer segments are sent.
Disabling the Nagle Algorithm
There
are times when the Nagle algorithm needs to be turned off. The classic
example is the X Window System server: small messages (mouse movements)
must be delivered without delay to provide real-time feedback for
interactive users doing certain operations.
The sockets API uses the TCP_NODELAY socket option to disable the Nagle algorithm.
The Host Requirements RFC states that TCP should implement the
Nagle algorithm but there must be a way for an application to disable it
on an individual connection.