This article is excerpted from "UNIX Network programming Volume 1".
1. TCP Features
Compared to unreliable, non-connected User datagram protocols (user Datagram Protocol, UDP), Transmission Control Protocol (PROTOCOL, TCP) is a reliable, connection-oriented protocol (transmission). In addition to this, TCP also provides the following features:
1) TCP contains a dynamic estimate of the round-trip time between the customer and the server (round-trip, RTT) so that it knows how long it will take to wait for a confirmation.
2) TCP Sorts the data sent by associating a sequence number to each of these bytes (sequencing).
3) TCP provides flow control. TCP always tells the peer how many bytes of data it can accept at any one time from the peer, which is called the Notification window (advertised Windows). At any point in time, the window indicates the amount of space currently available in the buffer, thus ensuring that the data sent by the sending side does not overflow the receive buffer. The window changes dynamically: When data is received from the sender, the window size decreases, but the window size increases when the receive side application reads from the buffer. It is possible to reduce the size of the notification window to 0: When TCP corresponds to a socket's receive buffer full, it must wait for the app to read from the buffer before it can receive data from the peer.
4) The TCP connection is full-duplex (Full-duplex).
2. Establishment and termination of TCP connections2.1 Three-way handshake
The following scenario occurs when a TCP connection is established:
1) The server must be ready to accept external links. This is usually done by invoking the 3 functions of socket, bind, and listen, which we call passive-opening (passive open).
2) The client initiates the active open by invoking Connect (active Open). This causes the client TCP to send a SYN (synchronization, synchronous) subsection, which tells the server that the client will send the initial sequence number of the data in the (pending) connection. Typically, Syn bytes do not carry data, and the IP datagram contains only one IP header, one TCP header, and possibly TCP options.
3) The server must confirm (acknowlegement, ACK) The SYN of the client and also send a SYN section, which contains the initial sequence number of the data that the server will send in the same connection. The server sends the SYN and the ACK (acknowledgment) of the client SYN in a single subsection.
4) The client must confirm the SYN of the server.
Shows the TCP three-way handshake:
Figure 2-2 shows the client's initial sequence number is J, and the server's initial sequence number is K. The confirmation number in the ACK is the next sequence number that is expected at the end of the Send ACK. Because the SYN occupies a byte of the serial number space, the ACK number in each of the SYN's acknowledgments is the initial sequence number of the SYN plus 1. Similarly, the confirmation number in the ACK for each fin (finish, which means the end) is the Fin's serial number plus 1.
2.2 TCP connection termination
TCP establishes a connection requiring 3 sub-sections, and terminating a connection requires 4 sub-sections.
1) An application process first calls close, which we call an active shutdown (active close). TCP on that side then sends a FIN section indicating that the data has been sent.
2) receive this fin to the side to perform a passive shutdown (passive clsose). This fin has TCP confirmation. Its reception is also passed as a file terminator (End-of-file) to the receiving end of the application process (after any other data that has been queued for the application process), because the receiving of fin means that the receiving application process has no additional data to receive on the corresponding connection.
3) After a period of time, the application process that receives this file terminator will call close to close its socket. This causes its TCP to also send a fin.
4) Acknowledge this fin by receiving the original sender TCP of the final fin (that is, the end of the active shutdown).
Since each direction requires a fin and an ACK, it usually requires 4 sub-sections. We use the qualifier "usually" because, in some cases, the fin of step 1 is sent with the data, and the sections sent by step 2 and step 3 are from the end of the passive shutdown, and may be merged into a subsection.
Shows the 4 sections of the TCP connection termination:
Like Syn, a fin also occupies 1 bytes of serial number space. Therefore, the ACK confirmation number for each fin is the fin's serial number plus 1.
Between step 2 and step 3, it is possible to move data from one end of the passive shutdown to the execution of the active shutdown. This is called semi-close (half-close).
When the socket is closed, its TCP sends a fin on its own side. As we noted in the diagram, this is what happens when the application process calls close, but it needs to be realized that all open descriptors are closed when a UNIX process terminates either voluntarily (calling exit or returning from the main function) or involuntarily (receiving a signal to terminate the process). This also causes any TCP connections that are still open to emit a fin.
Figure 2-3 shows a scenario where the client is actively shutting down, but we point out that both the client and the server can perform an active shutdown at either end. Typically, the client performs an active shutdown, but some protocols, such as http/1.0, are actively shut down by the server.
2.3 Observation Groupings
Figure 2-5 shows the actual packet switching situation that occurs with a complete TCP connection, including 3 stages of connection establishment, data transfer, and connection termination.
Transmission Control Protocol (TCP)--connection establishment and termination process