Transferred from: https://blog.csdn.net/jnu_simba/article/details/8966936
TCP segment Format
The segment format for TCP is shown in the following figure
Source port number and destination port number
The source port number and destination port number, plus the IP header's source IP address and destination IP address, uniquely determine a TCP connection.
Serial number
The sequence number represents the first byte ordinal of the data in this message segment.
Confirmation number
Valid only if the ACK flag is 1 o'clock. The confirmation number represents the ordinal of the next byte expected to be received.
head Length
4-bit, TCP header up to 60 bytes, minimum 20 bytes
Reserved Bits
6 bits, must be 0
6 flag Bits
urg-Emergency pointer Valid
ack-confirm the serial number is valid
psh-receiver should give this segment to the application layer as soon as possible
rst-Connection Reset
The syn-synchronization sequence number is used to initiate a connection
fin-indicates that a connection will be terminated
Window size
Flow control is achieved by window size.
Checksum
Verifies the TCP header with the data.
Emergency pointers
is a positive offset, and the sum of the values in the Ordinal field represents the ordinal of the last byte of the emergency data. The emergency mode of TCP is a way of sending an emergency data to the other end. In fact, emergency data is not the same as out-of-band data, and TCP does not have a separate logical connection to transmit data, except that emergency data is called out-of-band data in the socket API.
The confusion between TCP ' s urgent mode and Out-of-band data is also because the predominant programming interface, the Sockets API, Maps TCP ' s urgent mode into what sockets calls Out-of-band data.
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24
|
|
Ability to enable FD to receive out-of-band data void active_oobinline (int fd) { int ret; int oobinline = 1; ret = setsockopt (FD, Sol_socket, So_oobinline, &oobinline, sizeof (Oobinline)); if (ret = =-1) { Err_exit ("setsockopt"); } } Sigurg signal is generated when FD generates out-of-band data function to open the current process to receive Sigurg signals To receive out-of-band data in a signal processing function void Active_sigurg (int fd) { int ret; ret = Fcntl (FD, F_setown, Getpid ()); if (ret = =-1) { Err_exit ("Fcntl"); } } void Handle_sigurg (int sig) {char cmdline [max_command_line] = {0}; int ret = ReadLine (CTRL_FD, CmdLine, MAX _command_line); if (ret <= 0) {err_exit ("ReadLine");} ...} |
Options and Padding (option is 4-byte integer multiple, otherwise filled with 0)
The most common optional fields are the longest message size MSS (Maximum Segment size), each of which typically indicates this option in the first message segment of the communication. It indicates the maximum length of the message segment (payload) that can be received on this side. This option, if not set, defaults to 536 (20+20+536=576 bytes of IP datagram), where the IP header and TCP header are 20 bytes each, while the standard MTU (minimum) on the Internet is 576B.
The maximum segment size (MSS) is the largest segment that a TCP are willing to receive from its peer and, consequently, th E largest size its peer should ever use when sending. The MSS value counts only TCP data bytes and does does include the sizes of any associated TCP or IP header
Communication Timing (3-time handshake – transmits data-->4 Wave)
The following diagram is a time series diagram of a TCP communication:
In this example, the client initiates the connection, sends the request, and then the server responds to the request, and the client actively shuts down the connection. Two vertical lines indicate the two ends of the communication, from top to bottom to indicate the order of time, note that the data from one end to the other end of the network also takes time, so the arrows are oblique in the diagram. The paragraphs sent by the parties are numbered 1-10 in chronological order, and the main information in each paragraph is marked on the arrows, for example, the arrows on paragraph 2 are marked with SYN, 8000 (0), ACK 1001, <mss 1024>, the SYN position in the segment 1, the 32-bit sequence number is 8000, The segment does not carry a payload (data bytes 0), an ACK position of 1, a 32-bit confirmation sequence number is 1001, with an MSS option value of 1024.
the process of establishing a connection
1. The client emits a segment 1,syn bit representing the connection request. The sequence number is 1000, the serial number in the network communication as a temporary address, each data byte, this sequence number to add 1, so that the receiver can be discharged according to the sequence number of packets to the correct order, you can also find the situation of packet loss, in addition, the provisions of the SYN bit and fin also accounted for a serial number, this time, although However, due to the SYN bit, the next delivery should use the ordinal 1001. MSS represents the maximum segment size, if a segment is too large, package into a frame beyond the maximum frame length of the link layer, it must be fragmented in the IP layer, in order to avoid this situation, the client declares its own maximum segment size, it is recommended that the server side sent to not exceed the length of the segment.
2. The server emits paragraph 2, also with a SYN bit, and the ACK bit to confirm, confirm that the serial number is 1001, indicating "I received the sequence number 1000 and its previous segments, please send the next sequence number 1001", that is, to answer the client's connection request, but also to send a connection request to the client, Also declare a maximum size of 1024.
3. The client sends out segment 3, responds to the server's connection request, and confirms that the serial number is 8001.
In this process, the client and the server to each other to send a connection request, also answered the other side of the connection request, where the server's request and response in a paragraph issued, so a total of three segments for the establishment of a connection, called "three-party handshake (Three-way-handshake)". While establishing the connection, the two sides negotiated some information, such as the initial value of the serial number, the maximum segment size, etc.
In TCP communication, if one party receives a segment from the other, reads the destination port number, and finds that no process uses the port, it will answer a segment containing the RST bit to the other party. For example, the server does not have any process use 8080 port, we use Telnet client to connect it, the server receives the client sends the SYN segment to answer an RST segment, the client Telnet program receives the RST segment to report the error connection refused.
In general, a reset was sent by TCP whenever a segment arrives that does not appear to be correct for the referenced Connec tion. (We use the term referenced connection to mean the connection specified by the 4-tuple in the TCP and IP headers of the RE Set.) Resets ordinarily result in a fast teardown of a TCP connection.
the process of data transfer
1. The client emits segment 4, containing 20 bytes of data starting with ordinal 1001.
2. The server sends out segment 5, confirms that the serial number is 1021, the data of the serial number 1001-1020 is acknowledged to receive, and the request sends the data starting with the sequence number 1021, and the server also sends 8001 bytes of data starting from ordinal 10 to the client at the same time, which is called piggyback.
3. The client issues paragraph 6, the server sent a sequence number of 8001-8010 to confirm the receipt, the request to send the data starting with the ordinal 8011.
In the data transmission process, the ACK and confirm the serial number is very important, the application to the TCP protocol sent by the data will be temporarily present in the TCP layer send buffer, the TCP data segment sent to the other side, only to receive the ACK of the other side of the answer to know that the data segment is actually sent to the other side, Can be freed from the send buffer, if the data segment is lost due to network failure or the ACK segment sent back by the other party is lost, the TCP protocol automatically resend the data segment in the send buffer after a wait timeout.
This example only describes the simplest of a question and answer scenario, the actual TCP data transfer process can send and receive many data segments, although the typical scenario is the client actively request the server passive response, but it is not necessary, in fact, the TCP protocol for the application layer provides full duplex (Full-duplex) service, Both parties can actively and even send data to each other at the same time.
If the communication process can only be a one-way answer, receiving and sending two directions can not be transmitted simultaneously, at the same time only allow One direction of data transmission, is called "Half-duplex (Half-duplex)", assuming that some kind of connection-oriented protocol is half-duplex, then only need a set of serial number is enough, There is no need to maintain a serial number for each communication.
process of closing a connection
1. The client emits a segment 7,fin bit to indicate a request to close the connection.
2. The server issues segment 8, answering the client's shutdown connection request.
3. The server issues segment 9, which also contains the fin bits, which sends a close connection request to the client.
4. The client issues segment 10, answering the server's shutdown connection request.
The process of establishing a connection is a three-way handshake, and closing the connection typically requires 4 segments, and the server's answer and close connection requests are usually not the same and in one segment, because there is a case of a semi-closed connection (calling shutdown instead of close), in which case the client can no longer send data to the server after the connection is closed. However, the server can also send data to the client until the server closes the connection.
sliding window and flow control
If the sending side is sent at a faster rate, the receiver receives the data after the processing is slow, and the size of the receive buffer is fixed, the data is lost. The TCP protocol solves this problem through a "sliding window (Slidingwindow)" mechanism. Look at the communication process in the diagram below.
1. Send side initiates the connection, declares the maximum segment size is 1460, the initial sequence number is 0, the window size is 4K, indicates "My receive buffer also has 4 K bytes idle, you send the data not to exceed 4K". The Receive side answers the connection request, declaring that the maximum segment size is 1024, the initial sequence number is 8000, and the window size is 6K. Send-side answer, three-party handshake ends.
2. Send the end of paragraph 4-9, each segment with 1K of data, the sender of the window according to the size of the receiver to know the buffer is full, so stop sending data.
3. The receiving side of the application to lift the 2K data, the receiving buffer has 2K idle, the receiver sends out segment 10, the response has received 6K data while declaring the window size is 2K.
4. The receiving side of the application also lift the 2K data, the receive buffer has 4 K idle, the receiver sends out segment 11, re-declared the window size of 4K.
5. Send the end of paragraph 12-13, each segment with 1K data, Segment 13 also contains the fin bit.
6. The receiving side answers the received 2K data (6145-8192), plus the fin bit takes up a sequence number 8193, so the answer sequence number is 8194, and the receiving side declares the window size at 2K.
7. The receiving side of the application to lift the 2K data, the receiver to re-declare the window size of 4K.
8. The receiving side of the application to take away the remaining 2K of data, the receive buffer is empty, the receiver to re-declare the window size of 6K.
9. The receiving end of the application after the completion of the entire data, decided to close the connection, the issue of paragraph 17 contains the FIN bit, the sending side answer, the connection is completely closed.
The above figure in the receiving end with a small square to represent 1 K data, a solid small square represents the received data, the dashed box represents the receive buffer, so the dotted box in the hollow small square represents the window size, as can be seen from the image, as the application moves data, the dashed box is the right slide, so called sliding window.
As can be seen from this example, the sender is a K-K to send the data, and the receiving end of the application can be two K two k to take the data, of course, it is possible to take 3 K or 6K data at a time, or only a few bytes of data at a time, that is, the application sees the data is a whole, Or a stream, how many bytes of a message are not visible to the application, so the TCP protocol is a stream-oriented protocol, which is also the cause of sticky-packet problems. And UDP is a message-oriented protocol, each UDP segment is a message, the application must be in the message to extract data, not one time to extract arbitrary bytes of data, which is very different from TCP.
How TCP guarantees reliability
1, the application data is divided into TCP is considered to be the most suitable data block to send, called segment passed to the IP layer.
2. When TCP sends out a segment, it initiates a timer, waiting for the destination to acknowledge receipt of the message segment. If a confirmation cannot be received in time, the message segment will be re-sent.
3. When TCP receives a data segment from the other end of the TCP connection, it sends an acknowledgment. This acknowledgement is not sent immediately and will typically be deferred for a fraction of a second.
4, TCP will maintain its header and data checksum. This is an end-to-end checksum to detect any changes in the data during transmission. If a checksum error is received for a segment, TCP discards the segment and does not confirm (causing the other to time out retransmission)
5, TCP is hosted in the IP datagram to transmit, and the arrival of the IP datagram may be out of order, so the arrival of the TCP message segment may also be out of sequence. TCP will then re-order the received data segment and present the receive buffer to the application layer for extraction.
6, the IP datagram will be duplicated, TCP receiver must discard the duplicate data.
7, TCP can also provide flow control. Each side of a TCP connection has a buffer space of a certain size.
Reference:
"Linux C Programming One-stop learning"
"TCP/IP detailed Volume One"