One. TCP protocol
TCP (transmission Control Protocol) transmission protocol, is one of the most important protocols in the TCP/IP protocol family, the main work in the transport layer, and UDP, TCP provides a flow-oriented service for the connection of reliable transmission services, although the byte-stream-oriented, But TCP transmission unit is a message segment, a message is divided into the first message and the effective data information, the first information is in the transmission process plays a vital role;
-------------------------------------------------------------------------------------------
Two. Header format for TCP messages
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/1B/wKioL1c4NeGixj1sAAAW153zunI355.png "title=" TCP datagram. png "alt=" Wkiol1c4negixj1saaaw153zuni355.png "/>
Source Port number : Identifies the sending end is the 16-bit size of the information received from which application port on the application layer of the previous layer;
Destination port number : Identifies which application process to be handed over to the destination host application layer, 16-bit; As with the Mac frame protocol, there is a frame type identifier indicating which side of the previous layer to be handed over, and the type of protocol in the IP datagram determines whether the object being handed in is UDP or TCP;
32-bit serial number: Each byte in a stream that is transmitted in a TCP connection is numbered sequentially, a byte occupies an ordinal number, so the ordinal number in the header information indicates the ordinal of the first byte of the data sent in this article;
32-bit confirmation number : Indicates the ordinal of the first data byte that expects to receive the next message from the other party, and also indicates that the byte identified by the sender before the confirmation sequence number has been received;
Data Offset : 4-bit, indicating the length of the header in the message, that is, from the beginning of the message to offset how much is the beginning of valid data, but also the data distance message header how far;
reserved : Reserved for later use, currently set at 0;
URG (Urgent): Emergency field, when set to 1 o'clock, indicates that the following emergency pointer is valid;
ACK (acknowlegment): Confirm the field, when set to 1 o'clock, the previous confirmation sequence is valid;
PSH (push): Push field, when set to 1 o'clock, for the timely delivery of incoming messages to the processing;
RST (reset): Reset field, when set to 1 o'clock, the table TCP link serious error, not to release the connection and then re-establish;
SYN (Synchronization): The synchronization field, when set to 1 o'clock, indicates that this is a connection request message;
FIN (finish): The terminating field, when set to 1 o'clock, indicates that a transport connection is required;
window : Refers to the maximum amount of data that can be received by itself, which is the receiving window for sending this section, not the sending window;
Inspection and : Error checking, accounting for 2 bytes;
Emergency Pointers : When Urg=1 is valid, it indicates the number of bytes of emergency data in the message, prioritizing emergency data;
options : variable length, up to 40 bytes;
padding : Used to populate when the message is below 46 bytes;
-------------------------------------------------------------------------------------------
Three. TCP connection establishment process (three handshake)
The TCP connection takes the client server mode, the party that initiates the connection request is the client process, and the party receiving the connection request is the server process;
First, both the client and the server are in the CLOSED (off) state, and the client actively requests the connection;
The server process will first create a transport control block TCB, prepare to receive the client's request, and then the server process will always be in LISTEN (listening) state, waiting for the client to send the request at any time;
The client process will also first create a transport control block TCB, send the connection request message to the server side, this times the SYN field is set to 1, indicating the request connection, and Select an initial ordinal seq placed in the message header, such as SEQ = n And at this time, the client will be in syn_sent(synchronous sent) state;
When the server side receives the request connection message sent by the client, if the consent connection will respond, send the acknowledgement message to the client, the SYN synchronization field and ack acknowledgment field in the message should be set to 1, and Select one of your own initial sequence numbers such as Seq = k, while confirming that the ordinal part is set to Ack=n+1 , which indicates that the next time the client sends the data ordinal, and that the data before N+1 is all received, the server is in SYN_RCVD (synchronous and received) State;
When the client receives a response from the server to confirm the connection, it sends a confirmation to the server that it has received a confirmation message that the server has agreed to establish the connection, and theAck field is set to 1in the message sent by the client. seq = n+1, which indicates the beginning of this data byte, and the confirmation sequence of the server is ack=k+1, indicating that all data before the server k+1 has been received , when the client enters the enstablished (connection established) State;
When the server receives the acknowledgement message from the client, it also enters the enstablished (connection established) state;
Note: The above connection process does not carry data information, because the connection is not yet established, but the status field such as SYN and ACK will still consume a sequence number, so each time the confirmation sequence of the last received message should be added 1, indicating that the serial number before all the numbers represent the data has been received If you carry a corresponding size of data, confirm that the number should be added to the size of the data and the status field of 1 ordinal;
The above procedure is expressed as:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/1F/wKiom1c4WFWCS-2pAAAevg_elm0490.png "title=" Three-time handshake. png "alt=" Wkiom1c4wfwcs-2paaaevg_elm0490.png "/>
Here are the reasons why three handshakes are not two times four or five handshakes:
First of all, a handshake is certainly not possible, because the connection is not one side said the connection is connected; second, if it is a two-time handshake, in this case, each time the server side to the client to send a connection request, as long as the consent to connect to the client sent a confirmation that the connection answer even if it is connected, this will happen: if the client's first request to connect the message does not arrive in time to the server side, but in the middle of the loss or wait, then the client will send a second request, when the second successful to the server side, as long as the server side agreed to establish a connection, Then when the client receives a server-side acknowledgment response sent after the connection is established, then if the data transfer completed after the connection is released, the client first request to reach the server side, then the server side will agree to establish a connection, then there is a connection is established, But in fact, the client does not need to establish a connection again, then it will not transfer data, so that a connection will be wasted, if there are many such circumstances, will waste a lot of resources; Finally, if it is four times five times the handshake is not necessary at all, This is equivalent to the Blue Army White Army problems, there are always the last to confirm that the answer is unreliable;
Therefore, the three-time handshake is the best number of times, when the client requests the connection, if the server side agrees to connect, sends the acknowledgment connection answer, when the client receives the reply to the server side again, indicating that it has received the message that the connection is agreed, so that the two parties can establish a connection;
-------------------------------------------------------------------------------------------
Four. TCP connection disconnect process (four waves)
The process of connection release is slightly more complicated than the process of connection creation, and it is still necessary to describe the status field in the message header and the changes in both states:
-
FIN field is set to 1 , and the sequence number is the last byte that you sent the data for, plus 1, (TCP specifies that Even if the fin message does not carry data, it consumes a sequence number, such as seq = x , because it is disconnected so it does not carry valid data. The client will then enter the status of fin_wait_1 (terminating wait 1) , waiting for server-side confirmation;
The server side receives the client sends the disconnection message to respond, responds to a confirmation message, theACK field is set to 1, the sequence number is itself the previous send the paper the last byte sequence number plus 1, such as seq = y, confirm that the serial number is ack = x+1, then the server will enter a close_wait (shutdown wait) state, at this time the client-to-server connection is disconnected, but if the server side of the data has not been processed, can still send data to the client , the client still receives data because only one party disconnects ;
When the client receives the acknowledgement of a disconnected response sent by the server, it enters the state of the fin_wait_2 (terminating wait 2) because only one of the parties disconnects and waits for the disconnected message to be sent after the server has finished processing the data before the connection can be truly disconnected. ;
After the server has finished processing all the data, a request for a disconnected message is sent to the client, and the FIN field is set to 1, confirming that the serial number is seq = i, Because in the semi-closed state may also send some data to the client , the server side will enter a last_ack (final confirmation) state, waiting for the client's confirmation;
When the client receives a request for a disconnected message from the server side, it responds with a confirmation message with the ACK field set to 1, the sequence number seq = x+1, and the ACK = I+1, At this point the client will enter a time_wait (time waiting) state;
When the server receives the acknowledgement of the disconnected response message sent by the client, it will disconnect the previous one, and the server will enter the state of CLOSED (shutdown) and the corresponding transfer control block TCB.
When the client exceeds time_wait (time Waits) , it enters the state of CLOSED (off) and revokes the transfer control block TCB;
The disconnection process can be expressed as:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/1D/wKioL1c4ayTwcSdcAAApEGh-R7Y592.png "title=" Four times wave. png "alt=" Wkiol1c4aytwcsdcaaapegh-r7y592.png "/>
Here we talk about the time_wait (Time wait) state:
When the client enters the TIME_WAIT state, it must pass time to wait for the timer design time of 2MSL , the client will be able to enter the closed state, and the time MSL is called the longest message segment life (Maximum Segment Lifetime), RFC793 is recommended to be set to 2 minutes, TCP allows different implementations to use smaller MSL values depending on the situation. Therefore, it takes 4 minutes after the client enters the TIME_WAIT state to enter the closed state before it can begin establishing the next new connection .
Then why should the client wait for the specified 2MSL time before it can enter the closed state:
is to ensure that the client sends an ACK acknowledgement message to the server side , when the server sends a request to the client to disconnect the message without receiving a response, if the client does not wait for the time to go directly into the closed state to release the connection, Then the server can not receive the client's acknowledgement message, it has been unable to enter the closed state, so when the client sends the acknowledgement ACK message to the server is lost, the server side waits timeout will resend the request disconnected fin message, In this way, the client will re-receive the FIN request disconnection message sent by the server on the waiting 2MSL time, and will resend the ACK acknowledgement disconnection message again, so that the server can be assured that the disconnection message is properly entered into the closed state.
Finish
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1773775
TCP ———— Three-time handshake and four waves