一、TCP协议相关笔记
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
Nagle 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.
This shows that TCP can acknowledge
received data before the application has read and processed that data. The TCP
acknowledgment just means TCP has correctly received the data. We also have an
indication that the server process has not read these 3 bytes of data because the advertised
window in the final segment is 8189, not 8192.
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.
Here we'll show another example that's easier to demonstrate-typing one of the terminal's
special function keys during an interactive login. The function keys normally generate
multiple bytes of data, often beginning with the ASCII escape character. If TCP gets the
data 1 byte at a time, it's possible for it to send the first byte (the ASCII ESC) and then
hold the remaining bytes of the sequence waiting for the ACK of this byte. But when the
server receives this first byte it doesn't generate an echo until the remaining bytes are
received. This often triggers the
delayed ACK algorithm
on the server, meaning that the
remaining bytes aren't sent for up to 200 ms. This can lead to noticeable delays to the
interactive user.
**************************************************************************************
With TCP's sliding-window protocol the receiver does not have to acknowledge
every received packet. With TCP, the ACKs are cumulative-they acknowledge that the
receiver has correctly received all bytes up through the acknowledged sequence number
minus one.
The ordering of
the packets that we see on the wire depends on many factors, most of which we have no
control over: the sending TCP implementation, the receiving TCP implementation, the
reading of data by the receiving process (which depends on the process scheduling by the
operating system), and the dynamics of the network (i.e., Ethernet collisions and backoffs).
There is no single correct way for two TCPs to exchange a given amount of data.
The sockets API allows a process to set the sizes of the send buffer and the receive buffer. The size of the
receive buffer is the maximum size of the
advertised window (也就是Tcp头部的滑动窗口大小)
for that connection. Some applications change
the socket buffer sizes to increase performance.
*****************************************************************************************************
In all the examples we've seen so far in this chapter, the sender starts off by injecting
multiple segments into the network, up to the window size advertised by the receiver. While
this is OK when the two hosts are on the same LAN, if there are routers and slower links
between the sender and the receiver, problems can arise. Some intermediate router must
queue the packets, and it's possible for that router to run out of space.
TCP is now required to support an algorithm called slow start. It operates by observing that
the rate at which new packets should be injected into the network is the rate at which the
acknowledgments are returned by the other end.
Slow start adds another window to the sender's TCP: the congestion window, called cwnd.
When a new connection is established with a host on another network, the congestion
window is initialized to one segment (i.e., the segment size announced by the other end).
Each time an ACK is received, the congestion window is increased by one segment,
(cwnd
is maintained in bytes, but slow start always increments it by the segment size.) The sender
can transmit up to the
minimum
of the
congestion window
and the advertised window.