Transport layer: TCP protocol One, overview
TCP and UDP are on the same tier-transport layer, but they are a lot different. TCP is the most complex part of the TCP/IP family protocol, which has the following characteristics:
(1) TCP provides reliable data transfer services, TCP is connection-oriented . Before the application uses TCP communication, the connection is established, which is a "call" process, and "hang up" after the end of the communication.
(2) A TCP connection is a point-to-point , and a TCP connection can only connect two endpoints.
(3) TCP provides reliable transmission, error-free, not lost, not duplicated, in order.
(4) TCP provides full-duplex communication, allowing both parties to send data at any time because both sides of the TCP connection have send cache and receive cache.
(5) TCP-oriented byte stream . TCP does not know the meaning of the transmitted data, only the data as a series of byte sequence, it does not guarantee that the receiver receives the data block and the sender of the data block has a size correspondence.
II. Structure of TCP message segment
TCP is a byte stream oriented, while TCP transmits data to a unit that is a message segment . A TCP message segment can be divided into two parts: a header and a data section. The data section is the data that is delivered by the upper application, and the header is the key to TCP functionality.
The header of the TCP message segment has a fixed portion of the first 20 bytes, and the following 4n bytes are fields that are added as needed. is the TCP message segment structure:
20-byte fixed part, each field function description:
1. Source and Destination ports : 2 bytes each, write the source port number and destination port number, respectively. This is similar to the UDP header because it is a transport layer protocol.
2. Serial number : 4 byte sequence, the serial number range [0,2^32-1], the serial number is increased to 2^32-1, the next sequence number goes back to 0. TCP is byte-stream-oriented, and each of the bytes in a stream transmitted by TCP is numbered sequentially, while the ordinal field value in the header refers to the ordinal of the first byte of the data in this section.
3. confirm the serial number : 4 bytes, expect to receive the other side of the next message segment of the first data byte ordinal.
4. Data offset : 4 bits, which refers to the length of the header of the TCP segment, including fixed 20 bytes and option fields.
5. Reservations : accounted for 6, reserved for future use, currently 0.
6. control bit : A total of 6 control bits, indicating the nature of this paper, meaning as follows:
**URG 紧急**:当 URG=1 时,它告诉系统此报文中有紧急数据,应优先传送(比如紧急关闭),这要与**紧急指针**字段配合使用。**ACK 确认**:仅当 ACK=1 时**确认号**字段才有效。建立 TCP 连接后,所有报文段都必须把 ACK 字段置为 1。**PSH 推送**:若 TCP 连接的一端希望另一端立即响应,PSH 字段便可以“催促”对方,不再等到缓存区填满才发送。**RET 复位**:若 TCP 连接出现严重差错,RST 置为 1,断开 TCP 连接,再重新建立连接。**SYN 同步**:用于建立和释放连接,稍后会详细介绍。**FIN 终止**:用于释放连接,当 FIN=1,表明发送方已经发送完毕,要求释放 TCP 连接。
7. window : occupies 2 bytes. The window value refers to the sender's own receive window size because there is a limited amount of space to receive the cache .
8. Inspection and : 2 bytes. Like a UDP message, there is a test and check that the message is not in the process of transmission error.
9. Emergency pointer : 2 bytes. When Urg=1 is valid, it indicates the number of bytes of emergency data in this section.
options : variable length, up to 40 bytes. Specific option fields, and then do the introduction when needed.
Third, the establishment and release of the connection
As mentioned earlier, TCP is connection-oriented, to create a connection before transmitting the TCP segment, the party that initiates the connection is called the client, and the party that responds to the connection request is called the server, and the process of creating the connection is called a three-time handshake :
(1) The client makes a request connection message segment, where the header control bit syn=1, the initial sequence number seq=x. The client enters the Syn-sent (synchronous sent) state.
(2) After the service side receives the request message section, sends the acknowledgment message segment to the client. Confirm that the header of the message section is syn=1,ack=1, the confirmation number is ack=x+1, and you select an initial ordinal seq=y for yourself. The server enters the SYN-RCVD (synchronously received) state.
(3) After the client receives the acknowledgment message section of the service side, it also sends a confirmation message section to the server. This message section ack=1, confirmation number ack=y+1, and its own serial number seq=x+1. This message segment can already carry data, if not carry the data without consuming serial number, then the next segment sequence number is still seq=x+1.
At this point the TCP connection has been established, the client enters the established (established connection) state, when the service side receives the confirmation, also enters the established state, between them can formally transmit the data.
When the transfer data ends, both sides of the communication can release the connection, the release connection process is called the release connection :
(1) Both ends of the TCP connection are still in the established state, the client stops sending data and emits a FIN segment. The first fin=1, the Ordinal seq=u (U equals the last byte of the client transfer data plus 1). The client enters the fin-wait-1 (terminating wait 1) state.
(2) The service side reply acknowledgment message segment, confirmation number ack=u+1, the serial number seq=v (v equals the service end transmits the data last byte serial number plus 1), the service side enters the close-wait (closes waits) the state. The TCP connection is now semi- open and the client still receives the server if it continues to send data.
(3) The client receives the acknowledgement message, enters the fin-wait-2 state, after the service end sends the data, issues the FIN message section, Fin=1, confirms the number ack=u+1, then enters Last-ack (final confirmation) status.
(4) Client reply confirmation message segment, Ack=1, Confirmation number ack=w+1 (W is half open half closed state, received the last byte data number), sequence number seq=u+1, and then enter the time-wait (time Wait) state.
Note that the connection is not released at this point, and it takes time to wait until the status ends (4 minutes) before the connection is CLOSED. Set the time to wait because it is possible that the last acknowledgment message is missing and needs to be re-transmitted.
Iv. implementation of TCP reliable transmission
(1) The length of the TCP message segment is variable, and adjusts according to the cache state and network state of the sending and receiving parties.
(2) When TCP receives data from the other end of the TCP connection, it sends an acknowledgment.
(3) When TCP sends out a segment, it initiates a timer, waits for the destination to acknowledge receipt of the message segment, and if it cannot receive a confirmation in time, the segment will be re-sent. This is the time- out retransmission that is described later.
(4) TCP will keep its header and data checked and. If there is a mistake in the detection and discovery of the message segment, the segment will be discarded and wait for the timeout to retransmit.
(5) TCP sorts the data in bytes, with a sequence number in the message segment to ensure the correctness of the order.
(6) TCP can also provide flow control. Each side of a TCP connection has a send and receive cache. The receiving side of TCP only allows the other end to send the data that the receiving buffer can accept. This prevents faster hosts from causing buffer overruns for slower hosts.
The visible time-out retransmission mechanism is the key to TCP reliability, so long as the acknowledgement message is not received, the datagram is resent until the confirmation is received.
Five, time-out retransmission
TCP provides that, after receiving the data packet, the receiver should reply to a confirmation message section to inform the sender that the data has been received. If the sender does not receive a confirmation message segment for a period of time ( timeout timer ), it is sent repeatedly:
In order to achieve super-time retransmission, it is important to note:
1. After sending a message segment, the sender temporarily saves a copy of the message segment , which is used when a time-out retransmission occurs, and the message segment is deleted after the acknowledgement message is received.
2. Confirm that the message segment also requires a serial number in order to be clear that the data that was sent is confirmed.
3. The timeout timer is slightly longer than the transfer round-trip time, but the specific value is indeterminate and varies depending on the network condition.
Six, Continuous ARQ protocol
Perhaps you also found, according to the above introduction, time-out retransmission mechanism is time-consuming, each send a datagram to wait for confirmation.
In practice, this is not true, the real situation is that the use of pipeline transmission : The sender can continuously send a plurality of message segments (continuous transmission of the length of the data is called a window), without having to stop every time to wait for confirmation.
In practice, the receiving party does not have to reply to every message received, but instead uses the cumulative acknowledgement method: After receiving multiple successive segments, the receiver only replies to the last segment of the message, indicating that the data before it has been received.
In this way, the transmission efficiency has been greatly improved.
Vii. flow control and congestion control
Due to the receiver cache limit, the Send window cannot be larger than the receiver receive window. There is a field in the header of the message section called the window (Rwnd), which is used to tell the other side of the receiving window, the size of the visible window can be changed.
So how does the size of the window change? TCP's control of congestion is summarized as "slow start, additive increase, multiplicative subtraction":
(1) slow start : The initial window value is small, but gradually increases exponentially, until the slow start threshold (Ssthresh)is reached.
(2) additive Increase: The window value reaches the slow start threshold, each sending a message segment, the window value increases a unit amount.
(3) multiplicative subtraction : No matter what stage, whenever a timeout occurs, the window value is reduced by half.
Eight, tcpdump crawl TCP message segment
In the previous experiment, we crawled and read the UDP message with tcpdump, so this time we tried to crawl the TCP segment. Of course, first install the Tcpdump (installation method refer to the previous section of the experiment).
For this experiment, download code from GitHub, which is a TCP-based chat applet, is divided into server (service side) and client (clients):
cd Desktopgit clone http://git.shiyanlou.com/shiyanlou/tcp_ip_6cd tcp_ip_6gcc -o server server.cgcc -o client client.c
Do not run after compilation, first open tcpdump, install and run tcpdump with command:
updatesudo apt-get install tcpdumpsudo tcpdump -vvv -X -i lo tcp port 7777
Minimize the terminal running tcpdump, and then open a different terminal to run the server program:
cd Desktop/tcp_ip_6./server 127.0.0.1
Then open a third terminal and run the client program:
cd Desktop/tcp_ip_6./client 127.0.0.1
Now, using client and server chat, alternately sending a few short messages (such as Hello, Hi, Wei, and so on) can shut down the client and server and go back to the terminal running tcpdump to view the contents of the captured message segment:
Through the captured messages, you can also clearly see the establishment of a three-time handshake and disconnect four handshake process.
Transport Layer: TCP protocol