TCP/IP basics for Linux Network Programming (4): TCP connection establishment and disconnection and sliding windows

Source: Internet
Author: User
Tags connection reset telnet program

I. TCP segment format:

Shows the TCP segment format.


Source Port Number and destination port number
The source port number and destination port number, and the source IP address of the IP header and the destination IP address uniquely determine a TCP connection.


Serial number
The sequence number indicates the First Data byte number in the packet segment.


Confirmation Number
Valid only when the ACK flag is 1. The confirmation number indicates the sequence number of the next byte to be received.


Header Length
4-bit, TCP Header up to 60 bytes, at least 20 bytes


Reserved Bit
6 digits, must be 0


Six flag Spaces
URG-Emergency pointer valid
Ack-confirm that the serial number is valid
Psh-the receiver should send the packet segment to the application layer as soon as possible
RST-Connection Reset
Syn-synchronous sequence number is used to initiate a connection
Fin-indicates that a connection is to be terminated.

Window Size
The window size is used to control the traffic.

Checksum
Verify the TCP header and data.


Emergency pointer
Is a positive offset, and the sum of values in the serial number field indicates the sequence number of the last byte of the emergency data. The TCP emergency mode is a method in which the sender sends emergency data (also known as out-of-band data) to the other end.

Option and fill (the option is 4-byte integer multiple, otherwise fill with 0)
The most common optional field is the maximum Message Size (MSS) (maximum segment size). Each connection usually specifies this option in the first packet segment of the communication. It specifies the maximum length of the message segment (payload) that the local end can receive ). If this option is not set, the default value is 536 (20 + 20 + 536 = 576 bytes of IP datagram). The IP header and TCP Header are each 20 bytes, while the Internet
The standard MTU (minimum) is 576b.


Ii. Communication sequence (three handshakes --> data transmission --> four waves)

Is the sequence of a TCP communication:



In this example, the client initiates a connection and sends a request, the server responds to the request, and the client closes the connection. The two vertical bars indicate the two ends of the communication and the time sequence from top to bottom. Note that it takes time to transmit data from one end to the other end of the network, so the arrows in the figure are skewed. The segments sent by both parties are numbered 1-10 in chronological order, and the main information in each segment is marked with arrows. For example, the arrows of Segment 2 are marked with SYN, 8000 (0), Ack 1001, <MSS 1024> indicates that the SYN position 1, 32 bits are 8000, the segment does not carry a valid load (the number of data bytes is 0), and The ack position 1, 32 bits are confirmed as 1001, the value with an MSS option is 1024.


Connection establishment process:
1. The client sends out segment 1, and the SYN bit indicates the connection request. The serial number is 1000. This serial number is used as a temporary address in network communication. For each Data byte sent, this serial number must be added to 1. In this way, the receiver can discharge the correct sequence of data packets according to the serial number, you can also find the packet loss. In addition, the SYN and fin bits must also occupy a serial number. Although no data is sent this time, the SYN bit is sent, therefore, the serial number 1001 should be used for next sending. MSS indicates the maximum segment size. If a segment is too large and is encapsulated into a frame that exceeds the maximum frame length of the link layer, it must be sliced at the IP layer. To avoid this situation, the client declares its maximum segment size. It is recommended that the length of the segment sent by the server not exceed this length.
2. the server sends out Segment 2 with SYN bits and sets the ACK bits to confirm. The confirmation serial number is 1001, indicating that "I have received the serial number 1000 and all its previous segments, please send the segment with the serial number 1001 next time, that is, to respond to the client connection request, send a connection request to the client, and declare the maximum size of 1024.
3. The client sends a segment 3 to respond to the server's connection request. Check that the serial number is 8001.


In this process, the client and the server send connection requests to the other party respectively, and also respond to the connection requests of the other party. The server requests and responses are sent in one segment, therefore, a total of three segments are used to establish a connection, known as ''' three-way-handshake )'''. When establishing the connection, both parties negotiate some information, such as the initial value of the serial number sent by both parties and the maximum segment size.


In TCP communication, if one party receives a segment from the other party and reads the destination port number, it finds that no process on the local machine uses this port, then a segment containing the RST bit is returned to the other party. For example, the server does not have any process using port 8080, but we use the Telnet client to connect to it. When the server receives the SYN segment from the client, it will respond to an rst segment, the client's telnet program reports an error connection timeout after receiving the RST segment:


Data transmission process:
1. The client sends segment 4, which contains 20 bytes of data starting with the serial number 1001.
2. the server sends the data segment 5, the confirmation serial number is 1021, and the data with the serial number 1001-1020 indicates that the data is received and the request starts with the serial number 1021, when the server responds, it also sends 10 bytes of data starting with serial number 8001 to the client, which is called piggyback.
3. When the client sends segment 6, the data sent from the server with the serial number 8001-8010 indicates that the request is received, and the data starting with the request sending serial number 8011 is confirmed.

During data transmission, ack and serial number are very important. The data sent by the application to the TCP protocol is saved in the sending buffer of the TCP layer. After the data packet is sent to the other party, only the ACK segment that receives the reply from the other party knows that the packet is indeed sent to the other party, and can be released from the sending buffer, if the packet is lost due to a network failure or the ACK segment sent back by the other party is lost, the TCP protocol will automatically resend the packet in the sending buffer after waiting for timeout.


This example only describes the simplest Q & A scenario. The actual TCP data transmission process can send and receive many data segments. Although the typical scenario is that the client actively requests the server to respond passively, however, this is not necessary. In fact, the TCP protocol provides full-duplex services for the application layer. Both parties can send data to the other party at the same time.


If the communication process can only be answered with one answer, the receiving and sending directions cannot be transmitted at the same time, and only one direction of data transmission is allowed at the same time, it is called ''' Half Duplex (half-duplex) '''. If a connection-oriented protocol is half-duplex, only one serial number is required, and each other does not need to maintain one serial number.


Closing the connection:
1. The client sends a segment 7, and the fin bit indicates the request to close the connection.
2. the server sends out Segment 8 to respond to the client's closed connection request.
3. When the server sends segment 9, which also contains the fin bit, it sends a disconnect request to the client.
4. The client sends out segment 10 to respond to the server's closed connection request.


The process of establishing a connection is a three-party handshake, and closing the connection usually requires four segments. The server's response and closing connection requests are not usually merged in one segment, the connection is semi-closed (shutdown is called instead of close). In this case, after the client closes the connection, it cannot send data to the server, but the server can also send data to the client, until the server closes the connection.

3. Sliding Window and Traffic Control

If the sending speed of the sending end is fast and the receiving end receives data after processing is slow, and the size of the receiving buffer is fixed, data will be lost. TCP uses the '''' Sliding Window ''' mechanism to solve this problem. See the communication process.


1. when the sender initiates a connection, it declares that the maximum segment size is 1460, the initial serial number is 0, and the window size is 4 K, indicating that "My receiving buffer is 4 K Bytes idle, the data you send cannot exceed 4 K ". The receiving end responds to the connection request and declares that the maximum segment size is 1024, the initial sequence number is 8000, and the window size is 6 K. The sender responds, and the three-way handshake ends.
2. The sending end sends data ranging from 4 to 9, each with 1 kb of data. The sending end knows that the receiving end's buffer zone is full Based on the window size, so it stops sending data.
3. the application at the receiving end extracts 2 k data, and the receiving buffer is 2 k idle. the receiving end sends 10 segments. When the response receives 6 K data, the declared window size is 2 K.

4. the application at the receiving end extracts 2 k data, the receiving buffer is 4 K idle, the receiving end sends 11 segments, and the window size is 4 K.
5. The sending end sends 12-13 segments, each of which contains 2 k Data. segment 13 also contains fin bits.
6. the receiving end receives 2 k Data (6145-8192), and the fin position occupies a sequence number of 8193. Therefore, the response sequence number is 8194, and the receiving end simultaneously declares that the window size is 2 K.
7. the application at the receiving end extracts 2 k data, and the receiving end re-declares that the window size is 4 K.
8. the application at the receiving end extracts the remaining 2 k data, the receiving buffer is empty, and the receiving end re-declares that the window size is 6 K.
9. After the application at the receiving end extracts all the data, it decides to close the connection. The sending segment 17 contains the fin bit, the sending end responds, and the connection is completely closed.


At the receiving end, use a small square to represent 1 k of data, a solid small square to represent the received data, and a dotted box to represent the receiving buffer. Therefore, the hollow small square in the dotted box represents the window size. We can see that, as the application extracts data, the dotted box slides to the right, so it is called a sliding window.


From this example, we can also see that the sender sends data in one K and one K, while the application at the receiver can extract data in two K and two K, of course, it is also possible to extract 3 K or 6 K data at a time, or extract only a few bytes of data at a time. That is to say, the data seen by the application is a whole, or a stream, the data may be split into many data packets for transmission in the underlying communication, but the number of bytes of a data packet is invisible to the application, therefore, the TCP protocol is a stream-oriented protocol, which is also the cause of the packet sticking problem. UDP is a message-oriented protocol. Each UDP segment is a message. The application must extract data in units of messages and cannot extract any bytes of data at a time, this is very different from TCP.


Iv. How to ensure the reliability of TCP

1. Application Data is divided into data blocks that TCP considers to be the most suitable for sending, which are called segments passed to the IP layer.
2. When TCP sends a segment, it starts a timer and waits for the destination to confirm receiving the segment. If a confirmation message cannot be received in time, the message segment will be resold.
3. When TCP receives data from the other end of the TCP connection, it sends a confirmation message. This confirmation is not sent immediately. It will usually be postponed by a few seconds.
4. TCP will keep its header and data checksum. This is an end-to-end checksum to detect any changes in data during transmission. If the verification and error of the received segment are received, TCP discards the segment and does not confirm it (resulting in timeout retransmission)
5. TCP is carried by IP datagram, and the arrival of IP datagram may be out of order, so the arrival of TCP packet segments may also be out of order. TCP sorts the received data again.
6. Duplicate IP datagram occurs. The TCP receiver must discard duplicate data.
7. TCP can also provide traffic control. Each side of a TCP connection has a buffer space of a certain size.

Refer:

Linux C Programming one-stop learning

Chapter 1 TCP/IP details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.