Basic concept of TCP/IP-packet, three-way handshake, and four-time breakup

Source: Internet
Author: User
Document directory
  • TCP packet
  • TCP Reliability Assurance
TCP data packets

  • Source Port and destination port field -- each occupies 2 bytes. Identifies the application process of the sender and receiver.
  • Number Field-4 bytes. Each byte in the data stream transmitted in the TCP connection is a serial number. The value of the serial number field refers to the sequence number of the first byte of the data sent in this section.
  • The validation number field, which occupies 4 bytes. It is the sequence number of the first byte of the data expected to receive the next packet segment of the other party.
  • Data offset, which occupies 4 bits, indicates how far the starting point of the TCP packet segment is from the starting point of the TCP packet segment.
  • Reserved field -- takes 6 bits and is reserved for future use, but it should be set to 0 currently.
  • URG-When URG = 1, it indicates that the emergency pointer field is valid. It tells the system that there is urgent data in this packet segment and should be transmitted as soon as possible (equivalent to high-priority data ).
  • Confirm the bit ACK -- the validation number field is valid only when ACK = 1. When ACK = 0, the confirmation number is invalid.
  • PuSH bit PSH (PuSH): receives the packet segment of PuSH bit 1 received by TCP, and delivers it to the receiving application process as soon as possible, instead of waiting until the entire cache is filled up before delivery.
  • ReSeT bit RST (ReSeT) -- When RST is set to 1, it indicates a serious error in the TCP connection (for example, due to host crash or other reasons). You must release the connection and notify the other party.
  • Synchronous bit SYN-when the synchronous bit SYN is set to 1, it indicates that this is a connection request or connection to receive packets.
  • Terminate bit FIN (FINal) -- used to release a connection. When FIN = 1, it indicates that the data of the sending end of the message segment has been sent and the transport connection needs to be released.
  • Window field -- occupies 2 bytes. The window field is used to control the volume of data sent by the recipient, in bytes. The end of the TCP connection determines the size of its receiving window based on the size of the cache space, and then notifies the other end to determine the upper limit of the sending window of the other end.
  • Check and -- Take 2 bytes. The range of the test field includes the header and the data and the pseudo-field header (not the information in TCP, but the content calculated during the calculation checksum: IP address, TCP data segment length, protocol type ).
  • Emergency pointer field-16 bits. Emergency pointer specifies the location of emergency data in this section.
  • Option field -- variable length. TCP specifies two options: the maximum length of the message segment (MSS) (maximum segment size ). The MSS tells the peer TCP: "The maximum length of the data field of the packet segment that can be received by my cache is MSS bytes ." And window expansion factor, used to expand the receiver window.
  • Fill field -- this is to make the length of the entire header be an integer multiple of 4 bytes.
TCP packet

TCP data packets mainly include:

  • SYN Packet: The data packet requesting connection Establishment
  • ACK packet: Response packet, indicating that a packet from the other party is received
  • Psh package: normal data packets
  • Fin package: communication end package
  • RST package: reset the connection
  • URG package: Emergency pointer

Cause of RST packet sending through TCP: No receiving process is waiting at the destination port specified by 1.syn data segment.
2. TCP wants to discard an existing connection. 3. TCP receives a data segment, but the connection identified by this data segment does not exist.
The TCP protocol that receives the RST data segment immediately disconnects the connection abnormally and reports an error to the application.

TCP Reliability Assurance
  • Timeout retransmission: each time TCP sends a packet segment, a timer is started to confirm the timing of the confirmation information returned by the target end. After timeout, the request is re-transmitted.
  • Validation signal: when TCP receives a message segment from TCP, a confirmation signal is sent back.
  • Test: TCP will always maintain the test of the header and data. If the packet segment is checked and there is an error, it will be discarded and the sender wants to re-transmit the packet upon timeout.
  • Re-sorting: Because IP datagram may be out of order, TCP will re-sort the data and hand it to the application layer in the correct order.
  • Discard duplicate: Because IP datagram may be repeated, TCP will discard duplicate data.
  • Traffic control: both ends of the TCP connection have a fixed buffer space. The TCP receiver only allows the Peer to send data that the local buffer can accommodate. TCP provides traffic control. When both parties interact, they will notify each other of the data size that the current receiving buffer can receive at most (the notification window), so as to ensure that the data sent by the sender will not overflow the receiving buffer.
Establish a connection (three-way handshake)

TCP connection establishment: TCP uses the three-way handshake process to create a connection.

First handshake: when a connection is established, the client sends the SYN Packet (SYN = J) to the server and enters the syn_send status. Wait for the server to confirm;

The second handshake: when the server receives the SYN packet, it must confirm the customer's Syn (ACK = J + 1) and send a SYN Packet (SYN = K), that is, the SYN + ACK packet, the server enters the syn_recv status;

The third handshake: the client receives the server's SYN + ACK package and sends the ACK (ACK = k + 1) Confirmation package to the server. After the package is sent, the client and server enter the established status, complete three handshakes.

A complete three-way handshake is request-response-re-confirmation. The description is as follows:

1. The client creates an active open by sending a SYN to the server as part of the three-way handshake.
2. The server should send a SYN/ack to a valid syn.
3. The client sends another ack. This completes the three-way handshake and enters the connection establishment state.

Data Transmission

1. Send the PSH data packet
2. Receive ACK data packets from the data end

In the data transmission status of TCP, many important mechanisms ensure the reliability and robustness of TCP. They include:

  • Sequence numbers are used to sort received TCP packets and detect duplicate data;
  • Use checksum to detect packet segment errors
  • Use the validation and timer to detect and correct packets or latencies.
Close connection (four breaks up)

Three handshakes are required to establish a connection, and four handshakes are required to terminate a connection. This is caused by the semi-close of TCP,

Because the TCP connection is full-duplex, each direction must be closed separately. This principle is that when one party completes its data sending task, it can send a FIN to terminate the connection in this direction. Receiving a FIN only means that there is no data flow between the two parties. a tcp connection can still send data after receiving a FIN. First, the party that closes the service will take the initiative to close the service, and the other party will passively close the service.

(1) Client A sends a fin to disable data transmission from client A to server B.

(2) server B receives the FIN and sends back an ACK to confirm that the serial number is added to 1. Like SYN, a FIN occupies a sequence number.

(3) server B Closes the connection with client A and sends A FIN to Client.

(4) Client A sends back the ACK message for confirmation, and sets the confirmation sequence number to receive the serial number plus 1.

The description is as follows:

1. One end closes the connection. Send a FIN package to the other end.
2. receive an ACK packet from the other end of the FIN package.
3. Send a FIN package to the other end.
4. The original sender of the received FIN package sends ACK to confirm it.

Why is the three-way handshake while the four-way handshake when the connection is closed?

This is because the SOCKET in the LISTEN status of the server can respond to ACK and SYN after receiving the SYN Packet connection request) it is sent in a message. However, when the connection is closed, when the other party receives the FIN Message notification, it only indicates that the other party has no data to send to you; but not all your data may have been sent to the other party, therefore, you may not close the SOCKET immediately, that is, you may need to send some data to the other party, and then send the FIN message to the other party to indicate that you agree to close the connection now, therefore, the ACK messages and FIN messages are sent separately in most cases.

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.