TCP data Segment format +UDP data segment format

Source: Internet
Author: User
Tags ack
TCP Message Format




TCP (Transmission Control Protocol Transmission Control Protocol) is a connection-oriented, reliable, byte-throttling-based transport-layer communication protocol.


There are 10 required fields and an optional field in the header of the TCP message segment. The header is at least 20 bytes. The data behind the header is optional.


1) Source port (16-bit)

Identifies the computer port or process that sent the message. A TCP segment must include the source port number so that the destination host knows where to send a confirmation message.


2) Destination port (16-bit)

Identifies the port or process of the destination host that receives the message.


3) Serial number (also called serial number) (32-bit)

Used to identify each segment of the message so that the destination host can confirm that the data in the specified message segment has been received. When the source host is used to send a message to multiple message segments, even if the messages arrive in the order of the destination host, the serial number can also cause the destination host to arrange them sequentially.


When the SYN flag is not set, the field indicates the ordinal number of the first byte in the user data area, and when the SYN flag is set, the field indicates the sequence number that was originally sent.


In the first message segment that is sent when the connection is established, both sides provide an initial serial number. The TCP standard recommends using a counter value that increments 1 at 4ms intervals as the value of this initial serial number. Use a counter to prevent the same serial number from connecting when the connection is closed and reconnected.

For those segments that contain data, the number of bytes in the first byte of the packet is the initial sequence number, and the data bytes are numbered sequentially. If the source host uses the same connection to send another segment, the sequence number of the segment is equal to the sum of the number of data bytes in the previous message segment. For example, if the source host sends 3 message segments, each message segment has 100 bytes of data, and the first message segment of the serial number is 1000, then the second segment of the serial number is 1100 (1000 + 100), the third segment of the serial number is 1200 (1100 + 100).

If the serial number increases to the maximum value, it is reset to 0.


4) Confirmation Number (32 digits)

The destination host returns a confirmation number, which causes the source host to know that one or several message segments have been received. If the ACK control bit is set to 1, the field is valid. The acknowledgment number equals the ordinal of the last message segment received in order plus 1, which is also the ordinal value of the message segment that the destination host expects to receive next. When the confirmation number is returned, the computer considers that it has received all data that is less than the confirmation number.


For example, the serial number equals the sum of the serial number of the previous message segment and the number of bytes in the previous message segment. For example, suppose that the source host sends 3 message segments, each message segment has 100 bytes of data, and the first segment of the sequence number is 1000, then received the first message segment, the destination host returned with a confirmation number 1100 header. After receiving the second message segment (its ordinal number is 1100), the destination host returns the confirmation number 1200. After receiving the third message segment, the destination host returns the confirmation number 1300.


The destination host does not necessarily return a confirmation number each time it receives a message segment. In the above example, the destination host may wait until all 3 packets have been received, and then return a message with a confirmation number 1300, indicating that all 1200 bytes of data have been received. However, if the destination host sends back the confirmation number before the wait time is too long, the source host will think that the data did not reach the destination host, and automatically restart.


In the example above, if the destination host receives the first message segment with the message segment number 1000 and the last segment of the message segment number 1200, it can return the confirmation number 1100, but before returning to the confirmation number 1300, it should wait for the intermediate segment of the message section number 1100.


5 Data migration (first ministerial degree) (4-bit)

The data at the beginning of the TCP segment is how far from the beginning of the TCP message segment, that is, the first Minister degree. Because the length of the TCP header varies depending on the contents of the TCP Option field, the header contains a field that specifies the header field. The field is 32 bits, so the header length must be an integer multiple of 32 bits, sometimes requiring 0 at the end of the header. If the header does not have a TCP option field, the header length value is 5, indicating that the header has 160 bits, or 20 bytes.


6) reserved bit (6 bits)

Consists of 6 digits following the data offset field, all 0.


7) Control bit (6-bit)

Emergency URG: This position 1 indicates that the emergency pointer field is valid and it tells the system that there are urgent data in this segment and should be transmitted as soon as possible.

Confirm ACK: only if the ACK = 1 o'clock Confirmation Number field is valid, TCP rules, in connection with the establishment of all the message segments must be 1 ACK.

Push PSH: When two application processes communicate interactively, sometimes the application process at one end wants to be able to receive the response immediately after typing a command. In this case, TCP can use push action, at this point, the sender TCP put PSH 1, and immediately create a message segment sent out, the receiver received PSH = 1 of the message segment, as soon as possible (that is, "push" forward) delivery to receive the application process, and no longer wait until the entire cache is filled and then to On delivery.

Reset RST: Used to reset the corresponding TCP connection

Synchronous SYN: Valid only when a TCP connection is established with a three handshake. When SYN = 1 and ack = 0 o'clock, indicates that this is a connection request message segment, if the other party agrees to establish a connection, it should use SYN = 1 and ack = 1 in the corresponding message segment. Therefore, SYN 1 means that this is a connection request or a connection acceptance message.

End FIN: Used to free a connection. When the FIN = 1 o'clock indicates that the data for the sender of this segment has been sent and the transport connection is released.


8) window (16-bit)

This field is used for traffic control, which is the number of bytes that the computer expects to receive at a time, that is, the size of the window that sends the data. Tell the person how much data can be sent without waiting for confirmation. The maximum length represented here is 2^16-1 = 65535, and if you need to use a larger window size, you need to use the window enlargement factor option in the option.


Refers to the receiving window (not the sending window) of the party sending the newspaper paragraph.


9) Checksum (16-bit)

The source host and destination host compute the checksum based on the contents of the TCP message segment and the pseudo header. Information from the IP header and TCP message segment length is stored in the pseudo header. As with UDP, pseudo headers are not transmitted over the network, and the purpose of including pseudo headers in checksums is to prevent the destination host from incorrectly receiving error datagrams with routes.


Pseudo header, also known as pseudo-header (Pseudo header): In the TCP subsection or UDP datagram format, increase the source IP address, Destination IP address, protocol field of IP packet, TCP or UDP datagram total length 12 bytes before the datagram header, and the extension header Structure. This pseudo header is a temporary structure that does not pass up or down, just to ensure that the socket is validated.


10) Emergency pointer (16-bit)

It only makes sense at URG = 1 o'clock, which indicates the number of bytes of emergency data in this section (which is normal after the end of the emergency data), indicating the position of the end of the emergency data in the message, noting that emergency data can be sent even if the window is zero.


If the URG is 1, the emergency pointer marks the end of the emergency data. The value is the ordinal number of the last 1 bytes of the emergency data, representing the offset of the sequence number of the message segment. For example, if the sequence number of the message segment is 1000 and the first 8 bytes are emergency data, then the emergency pointer is 8. Emergency pointers The general purpose is to enable the user to abort the process.


11) Options, fill fields

Options such as window enlargement factor, timestamp, and so on may be included. The length is variable, up to 40 bytes long, and the TCP header length is 20 bytes when no option is used.

Padding is used to guarantee an integer multiple of 32bit for any option.


12) data (variable length)

The part after the end of the TCP header


UDP Message Format

UDP is the user Datagram Protocol abbreviation, the Chinese name is the Subscriber datagram Protocol, is a connectionless transport layer protocol, provides the transaction-oriented simple unreliable information transmission service.


1 Source port (2 bytes): The sender port number


2 Destination port (2 bytes): Receiver Port number


3 Message Length (2 bytes): The total length of the UDP User datagram, in bytes.


4 Checksum (2 bytes): Detection of UDP User datagram in the transmission is wrong, the wrong discard.


Used to verify the number of UDP datagrams and the "pseudo header" that contains the UDP datagram header.


Pseudo header, also known as pseudo-header (Pseudo header): In the TCP subsection or UDP datagram format, increase the source IP address, Destination IP address, protocol field of IP packet, TCP or UDP datagram total length 12 bytes before the datagram header, and the extension header Structure. This pseudo header is a temporary structure that does not pass up or down, just to ensure that the socket is validated.



5 Data: UDP Data part if not even need to fill with 0, that is, if the data length is odd, the data length plus "1".

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.