TCP connection--the megaphone of loveThe most important characteristics of TCP communication are: ordered (ordering) and reliable (reliable). Order is achieved by segmenting and numbering the text stream. Reliability is achieved through ACK replies and repeated sends (retransmission). This article introduces the concept of TCP connection (connection).
TCP connections
The network layer logically provides the concept of a port. An IP address can have more than one port. A specific port requires the combination of an IP address and a port number (which we remember as the Ip:port form). A connection establishes a TCP communication between two ip:port. (A common analogy is: A TCP connection is like two people call, IP is the switchboard number, port is the extension number)
If there are two computers participating in the connection, then the TCP module of the two computer operating systems is responsible for establishing the connection. Each connection has four parameters (two IPs, two ports) to indicate "who is talking to". Each computer will be logged with these four parameters to determine which connection it is. If the four parameters are identical, the same connection is the same, and if the four parameters have a different one, the connection is different. This means that there can be multiple connections on the same port. After the TCP module in the kernel generates a connection, the connection is allocated to the process for use.
There can be multiple connections on one port
The TCP connection is bidirectional (duplex). In the TCP protocol and "flow" communication, we show that the TCP transmission is unidirectional. A two-way connection is essentially a TCP transmission in two directions, so the concept is not complicated. At this point, each side of the connection needs two sliding windows to handle the text stream sent and the text stream received separately. Due to the bidirectional nature of the connection, we also have to number the text stream in two directions. The numbers of the two text streams are independent of each other. Segmentation and numbering of text streams are handled by the sender, and the reply ACK is performed by the receiving party.
Header format for TCP fragments
Before we dive into the TCP connection, we need to have some understanding of the header format of the TCP fragment. We know that TCP fragments are divided into headers and data. The data section is the text stream data that TCP actually transmits. The following is the header format for the TCP fragment:
From Wikipedia
Focus on the following points first:
1. A TCP header needs to contain the originating port (source port) and destination ports (destination port). These together with two IP addresses in the IP header determine the connection.
2. Each TCP fragment has an ordinal (sequence number). These numbers eventually organize the text fragments of the data portion into text streams.
3. Ack is a (bit). The reply number (acknowledgement) is valid only if the ACK bit is set. The ACK reply number describes the next fragment that the receiver expects to receive, so the ACK reply number is the last received fragment number plus 1.
Many times, the ACK reply "attaches" in the sent data fragment. The TCP protocol is bidirectional. such as A and b two computers. ACK reply is the receiver reply to the sender (such as a sent to B, b reply a). But at the same time, B can also be the sender, b there may be data sent to a, so B will be the ACK reply attached to it to send to a the head of the data fragment. This reduces the amount of traffic that is consumed by the ACK. A fragment can contain only ACK replies. A pure ACK reply fragment does not transmit a text stream, so the serial number is not consumed. If there is a normal data fragment, it will have the same ordinal number as the pure ACK reply fragment.
(Ack replies can also "attach" to SYN fragments and fin fragments)
4. The ACK is followed by a SYN and FIN, which also occupies one (bit). I'll show you the two in the back.
Establishment of a connection
The TCP transport discussed in the TCP protocol and the "flow" communication requires a precondition: The TCP connection is already established. However, TCP connections need a process of establishing a connection from scratch. The most important thing to establish a connection is to let the two sides of the connection exchange the initial serial number (ISN, Initial Sequence numbers). According to the TCP protocol, the ordinal of the first fragment of a text stream cannot be a definite number (say 1). Both sides of the connection randomly generate their own isn, and then use a certain way to get to know each other. This is because of TCP connection security considerations: If you use a certain number as the initial TCP sequence number, it is easy for others to guess the next sequence number and send a "spoofed" TCP fragment in the correct sequence number to insert into the text stream.
The ISN interchange is implemented by SYN fragments. The SYN fragment is indicated by the SYN bit of the head, and its ordinal number is the sender's isn. The fragment is sent first to the other party by the connecting party, and we will send the SYN party as the client (client), and the party receiving the SYN is called the server. We use ISN (c) to denote the client side of isn, using isn (s) to represent the server side of the isn. The server receiving the SYN will then need to reply to the ACK and send out a SYN fragment containing the server's isn. The process of establishing a connection is the classic TCP three-time handshake (Three-way handshaking). Two vertical lines are the time axes of the client and server, respectively. Each arrow represents a one-way transmission of a TCP fragment.
Cyan is purely an ACK fragment. The essence of the process is that each side sends a SYN fragment containing its own isn. Depending on the rules of the TCP transmission, the party receiving the ISN needs to reply to the ACK, so a total of four pieces of information is transmitted during the establishment of the connection. The reason is three handshakes (not four) because the server merges the Send SYN and reply ack into a TCP fragment. Let's take the client side as an example. The client knows its own isn (aka (c)). After establishing the connection, it also knows the other's isn (s). Thereafter, if you need to send a text stream fragment, the number is isn (c) + 1, isn (c) + 2 .... If the text stream fragment is received, then expect to receive isn (s) + 1, isn (s) + 2 ....
After the connection is established, the two sides of the connection can send each other a text stream in the same way as the TCP transmission.
Normal termination of the connection
After a connection is established, processes on both sides of the connection can use the connection to communicate. When the connected party feels "I'm done," it can end the communication sent to the other side in the connection. The connection ends with a four-time handshake (Four-way handshaking), and the connection end uses a special fragment of fin (a fragment of fin 1).
We can see that during the connection termination process, the connecting parties also exchanged four pieces of information (two fin and two ack). In the process of terminating the connection, TCP does not merge fin and ACK fragments. The reason is that the TCP connection allows one-way shutdown (half-close). That is, the TCP connection turns off the transmission in one direction and becomes a one-way connection (Half-duplex). The second and third arrows must be passed apart in order for the gap to continue to be transmitted in the open direction. If the second arrow and the third arrow are merged together, the other side is forced to close as one side closes.
Between the second and third handshake, the server can continue a one-way send fragment to the client, but the client cannot send the data fragment to the server.
(The end of the above is initiated from the client, and the TCP connection termination can be initiated from the server first.) )
The client sends a final ACK reply, but the ACK may be lost. If the server does not receive an ACK, the fin fragment is repeatedly sent. So the client cannot shut down immediately, it must confirm that the server received the ACK. The client enters the TIME_WAIT state after sending an ACK. The client sets a timer that waits 2MSL of time. If you receive fin again within that time, then the client will re-send the ACK and wait for 2MSL again. The so-called 2MSL is twice times the MSL (Maximum Segment Lifetime). The MSL is the maximum time that a fragment will survive on the network, and 2MSL is the maximum length required for a send and a reply. If no fin is received until 2msl,client, then the client infers that the ACK has been successfully received, ending the TCP connection.
Time_wait State
Summarize
TCP is a connection-oriented protocol that corresponds to a non-connection-oriented protocol such as UDP. Connections can lead to better transmission control, but also require additional work, such as connection creation and termination.
We also have a preliminary understanding of the TCP header format. It should be noted that many times we "attach" ACK fragments to other fragments. We do this to save the amount of ACK required for the pure ACK fragment. In fact, the cost of attaching an ACK fragment is essentially equal to 0 because the ACK bits and acknowledge number areas required by the ACK fragment always exist on the head of TCP.
TCP connection--the megaphone of love