Basic Tutorial: TCP connection establishment and release (1)

Source: Internet
Author: User

Basic Tutorial: TCP connection establishment and release (1)

TCP Header Format

First look at the format of the TCP packet segment, as shown below;

The first 20 bytes of the TCP segment header are fixed, and the next 4 bytes are added as needed. Therefore, the minimum length of TCP packets is 20 bytes.

The meanings of fields in the fixed part of the header are as follows:

1. Source Port and destination port: add the source IP address and destination IP address of the IP header to determine a unique TCP connection. In addition, the target port is used to determine the application that TCP delivers the datagram, so as to achieve the TCP sharing function.

2. Serial number: 4 bytes. The serial number ranges from 0 to. Because TCP is oriented to byte streams, every byte in the byte stream transmitted in a TCP connection is numbered sequentially, the sequence number field in the first part refers to the sequence number of the first byte of the data sent in this section. In addition, the sequence number is used cyclically. When the sequence number is increased to the maximum value, the next sequence number returns to 0.

3. Confirmation Number: valid when the ACK flag is 1, indicating the sequence number of the First Data byte of the next packet segment to be received. If the validation number is N, it indicates that all data bytes until the serial number N-1 have been correctly received.

4. Header Length: the length of the header of the TCP packet segment. It indicates the distance between the start position of the Data Segment of the TCP packet segment and the start position of the TCP packet segment. The Header Length occupies 4 bytes, but its unit is 32 characters, that is, 4 bytes. Therefore, the maximum length of the header is 15*4 = 60 bytes, this means that the length of the option cannot exceed 40 bytes.

5. Reserved bits: Must be 0.

6. The following six control bits describe the nature of the packet segment:

1) URG: used with the emergency pointer field in the header. When URG is 1, it indicates that the emergency pointer field is valid. The sending application process tells the sender that TCP has urgent data to be transmitted, as a result, the sender's TCP inserts the emergency data to the beginning of the data in this section, and the end is still normal data.

2) ACK: The ACK is valid only when ACK = 1. When ACK = 0, the ACK is invalid. TCP specifies that ACK must be set to 1 for all transmission packets after the connection is established.

3) PSH: If the psh in the sent packet segment is 1, the receiver will directly deliver it to the application process after receiving the packet segment, instead of waiting for the entire cache to be filled up before delivery.

4) RST: reset flag. When RST is set to 1, it indicates a serious error occurred in the TCP connection. You must release the connection and then re-establish the transport connection.

5) SYN: Synchronous sequence number, used to initiate a connection. When SYN = 1 and ACK = 0, it indicates that this is a connection request message segment. If the other party agrees to establish a connection, SYN = 1 and ACK = 1 should be made in the response packet segment.

6) FIN: Used to release a connection. When FIN = 1, it indicates that the data of the sender of the message segment has been sent and the connection must be released.

7. Window: the size of the sending window set by the receiver for the next message segment sent by the sender.

8. checksum: the range of fields for verification includes the header and data.

9. Emergency pointer: The emergency pointer is valid only when URG = 1. It indicates the number of bytes of emergency data in this section. It is worth noting that emergency data can be sent even if the window is 0.

10. Options and fill: the option should be an integer multiple of 4 bytes; otherwise, it should be filled 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 packets that can be received by the local end. 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, the standard MTU (minimum) on the internet is 576B.


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.