Network--tcp

Source: Internet
Author: User
Tags ack

Reference Document: Https://www.cnblogs.com/feng9exe/p/8058891.html

65664309

29557837

Https://www.cnblogs.com/zmlctt/p/3690998.html

Https://www.cnblogs.com/thrillerz/p/6464203.html

51146489

52241541

60468161

TCP Message Format:

1. Port number: A different application process used to identify the same computer.

1) Source port: The purpose of the source port and IP address is to identify the return address of the message.

2) Destination port: Port indicates the application interface on the receiver's computer.

The source port number and destination port number in the TCP header determine a TCP connection with the source IP in the IP datagram and the destination IP uniquely.

  2, serial number and confirmation number: is a key part of TCP reliable transmission. The ordinal number is the ordinal of the first byte of the data group sent by this section. In a stream that is transmitted by TCP, each byte is an ordinal number. e.g. the sequence number of a message segment is 300, the data portion of this segment has 100 bytes, and the next segment is numbered 400. So the sequence number ensures the order of the TCP transmission. The confirmation number, which is the ACK, indicates the next byte ordinal to be received, indicating that all data before the ordinal is received correctly. The confirmation number is valid only if the ACK flag is 1 o'clock. For example, when establishing a connection, the ACK flag bit for the SYN message is 0.

3, data offset/header length: 4bits. Since the header may contain optional content, the length of the TCP header is indeterminate, the header does not contain any of the optional fields, the length is 20 bytes, the 4-bit header length field can represent a maximum value of 1111, and the conversion to 10 is 15,15*32/8 = 60, so the maximum length of the header is 60 bytes. The first ministerial degree is also called data offset, because the header length actually indicates the starting offset value of the data area in the message segment.

4, Reservations: For the future definition of new uses to retain, now generally set 0.

5, control bit: URG ACK PSH RST SYN FIN, a total of 6, each flag bit represents a control function.

1) URG: Emergency pointer flag, 1 indicates that the emergency pointer is valid, and 0 ignores the emergency pointer.

    2) ACK: Confirm the serial number flag, 1 indicates that the confirmation number is valid, 0 indicates that the message does not contain confirmation information, ignore the confirmation number field.

3) The Psh:push flag, 1 indicates that the data with the push flag indicates that the receiving party should give the message segment to the application as soon as possible after receiving the segment, instead of queuing in the buffer.

4) RST: Resets the connection flag to reset the connection that was faulty due to host crash or other reasons. Or it is used to reject illegal message segments and deny connection requests.

    5) SYN: Synchronous sequence number, used to establish the connection process, in the connection request, Syn=1 and ack=0 indicates that the data segment does not use the Piggyback confirmation domain, and the connection answer is a confirmation, that is, Syn=1 and ack=1.

    6) Fin:finish flag, used to release the connection, 1 indicates that the sender has no data sent, that is, to close the local traffic.

6, window: Sliding window size, used to inform the sender of the cache size at the end of the receiver, in order to control the sending side of the data transmission rate, so as to achieve traffic control. The window is large for a 16bit field, thus the maximum window size is 65535.

7, Checksum: Parity, this checksum is the entire TCP message segment, including TCP headers and TCP data, calculated in 16-bit words. is computed and stored by the sender and validated by the receiving end.

8. Emergency pointer: only if the URG flag is placed 1 o'clock the emergency hand is valid. The emergency pointer is a positive offset, and the sum of the values in the Sequence Number 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.

9, options and fills: the most common optional field is the longest message size, also known as MSS (Maximum Segment size), each connecting party usually in the first message segment of the communication (set to establish a connection with the SYN flag 1 of the segment) to indicate this option, It represents the length of the maximum message segment that can be accepted on this side. The option length is not necessarily 32-bit integer multiples, so fill the bit, that is, add an additional 0 to this field to ensure that the TCP header is an integer multiple of 32.

10. Data part: The data part of TCP message segment is optional. When a connection is established and a connection is terminated, the message segments exchanged by both parties are only TCP headers. If a party has no data to send, it also uses the header without any data to confirm the received data. In many cases where the timeout is processed, a segment of the message without any data is also sent.

TCP connections:

  

TCP Three-time handshake:

The so-called three-time handshake (three-way handshake) means that when a TCP connection is established, the client and server are required to send a total of 3 packets.

The purpose of the three-time handshake is to connect the server to the specified port, establish a TCP connection, and synchronize the serial number and confirmation number of both parties and Exchange TCP window size information. In socket programming, the client executes connect (). Will trigger a three-time handshake.

  

  First handshake: The client sends a SYN packet (SYN=X) to the server and enters the Syn_send state, waiting for the server to confirm;

  

  Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=X+1), and also send itself a SYN packet (syn=y), that is, the Syn+ack packet, when the server enters the SYN_RECV state;

  

  Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=Y+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.

  

  A three-time handshake is used to prevent the failed connection request message segment from suddenly being transmitted to host B, resulting in an error. Invalid connection request message segment refers to: Host a connection request did not receive the confirmation of Host B, so after a period of time, host A and then re-send the connection request to host B, and establish a successful, sequential completion of data transmission. Consider such a special case, host a first send the connection request is not lost, but because the network node caused the delay to host B, Host B thought it was host A and the new connection initiated, so Host B agreed to connect, and to host a back to confirm, but at this time, host A will not listen to, Host B has been waiting for host A to send data, causing host B to waste resources.

  TCP Connection Third handshake packet loss processing mode:

    When the client side receives the server's syn+ack response, its status changes to established, and the ACK packet is sent to the server;

If the ACK is lost in the network at this time, the server side of the TCP connection has a status of Syn_recv, and then waits 3 seconds, 6 seconds, and 12 seconds to resend the Syn+ack packet so that the client can resend the ACK packet. the number of times that the server re-syn+ack the package can be modified by setting/proc/sys/net/ipv4/tcp_synack_retries, which defaults to 5. If the ACK response is still not received after the specified number of times, the server automatically shuts down the connection after a period of time. However, the client considers that the connection has been established, and if the client writes data to the server, the server side will respond with the RST packet in order to perceive the server's error.

TCP four times wave:

Similar to the "three-time handshake" that establishes a connection, disconnecting a TCP connection requires "four waves".

  First wave: The active shutdown sends a FIN, which is used to close the active side to the passive closed side of the data transfer, that is, the active shutdown to tell the passive shut-off party: I will no longer send you the data (of course, the data sent before the FIN packet, if not received the corresponding ACK acknowledgement message, The active shutdown will still re-send the data), but the active shut-off party can also accept the data at this time.

Second wave: The passive closing party receives the fin packet, sends an ACK to the other, confirms that the serial number is received ordinal +1 (same as SYN, one fin occupies a serial number).
Third wave: The passive shut-off side sends a fin, used to close the passive shut-off side to the active shutdown of the data transfer, that is, to tell the active shut-off party, my data is sent out, will not send you data.
Wave for the fourth time: the active closing party receives fin, sends an ACK to the passive closing party, confirms the serial number to receive the serial number +1, to this point, completes four times the wave.

Why four handshake required:

When the connection is closed, when the server side receives the fin message, it is likely that the socket will not be closed immediately, so you can only reply to an ACK message to the client side, "You sent the FIN message I received." I can't send fin messages until all the messages on my server end are sent, so I can't send them together. Therefore, four-step handshake is required.

when the client enters the time-wait state (that is, the fourth wave time), must go through the time counter set time 2MSL (the longest message segment life), to enter the closed state, then why???

  

    This is mainly because of two reasons:

1, in order to ensure that the client sends the last ACK message segment to reach the server. Because this ACK is likely to be lost, the server in the Last-ack state cannot receive a confirmation message for the fin-ack. The server will time out to retransmit the Fin-ack, and then the client re-passes the acknowledgment again, restarting the time waiting timer. Finally, the client and server can shut down gracefully. Assume that the client does not wait for 2MSL, but is released immediately after the ACK has been sent off, but if this ACK is lost, the server will not be able to enter the shutdown state properly.

2, he can also prevent the broken message segment. After the client sends the last ACK, it passes through 2MSL, so that all segments of the message generated during the duration of the link will disappear from the network. From the guarantee after the connection is closed there will not be stranded in the network message segment to harass the server.

Note: The time-out retransmission timer is started immediately after the server has sent Fin-ack. The client starts the time wait timer immediately after the last ACK is sent.

Network--tcp

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.