TCP protocol analysis and related issues

What is TCP protocol:

       TCP is the protocol (TCP, Transmission Control Protocol) to control a transport layer to provide reliable end to end on a byte stream unreliable network specifically designed to a transmission protocol. It is connected at both ends to make possible to read a guarantee to send data to each other

The main features of the TCP protocol:

1. The connection-oriented transport layer protocol, the application before using tcp, tcp connection must be established, the connection must be released after the transmission is completed

2. The point-to-person connection

3.tcp provide reliable services delivered through data transfer TCP connection, no difference, is not lost, not repeat, arrive in order

4. full-duplex communication, communication parties can transmit data, it is provided at both ends of the TCP connection transmitting buffer and the reception buffer at any time, for temporarily storing data of two-way communication, when transmitting, and then transmits data to the application after the TCP buffer, you can do your own thing, TCP at the right time to send out data, when receiving, TCP data received into the cache, the upper application process reads data in the cache at the right time

5. oriented byte stream, TCP refers to a flow in a sequence of bytes flowing into or out of the process from the process, although the application is interactive and TCP data blocks of varying sizes, but only to ensure tcp, the sender and receiver party received the same byte stream.

TCP to transfer data format:

Each TCP connection has two endpoints of the socket (Socket) or socket. Ip address port number splicing constitute socket (IP address: port number)

Means for transmitting tcp segment, segment header = (20 + 4N bytes) data +

Header top twenty bytes fixed, this article describes what is mentioned several:

     SEQ ID SEQ : 4 bytes for the data segment order mark, the TCP connection all data bytes transmitted are compiled on a serial number, the first byte of the random number generated by the local; ed to bytes after the serial number, give each segment is assigned a serial number; seq is the sequence number of the first byte of the data segment number.

    Acknowledgment number ack: 4 bytes, the next expects to receive the other of the first data byte of a segment number; first byte of the segment sequence number indicates the number of data carrying; numbers refer confirmed It is expected to be received next byte number; therefore this number plus the last byte of the segment is the acknowledgment number.

    Acknowledgment ACK: one bit, only when ACK = 1, the acknowledgment number field is valid. When ACK = 0, the acknowledgment number is invalid

    Synchronization SYN: for synchronizing serial connection establishment. Means that when SYN = 1, ACK = 0: this is a connection request packet segments. If the agreed connection, in the response message segments that SYN = 1, ACK = 1. Accordingly, SYN = 1 indicates that this is a connection request, or the connection acceptance message. The SYN flag only if the TCP connection construction production will be set after the completion of the handshake SYN flag is set to 0.

    Termination FIN: for releasing a connection. FIN = 1 represents: a data sender of this segment has been transmitted, and for the release of the transport connection

  。。。。。。

 ACK, SYN and FIN flags of these capitalized words represent bits whose value is either 1 or a 0; ack, seq lowercase number word representation.

Reliable transmission of works:

1. Stop waiting for agreement

Suppose the sender A, B is the receiver.

     1) non-discriminatory conditions:

A transmission packet M1, stops sending after transmission, wait for confirmation of B, A sends an acknowledgment to be received after the B M1, B A after receiving the acknowledgment, M2 sent again, then the same reason.

     2) errors:

After A sends packets M1, occurs an error in the transmission process, leading to B is not received or discarded M1 M1, B will not send any acknowledgment message, A does not receive an acknowledgment after a period of time, retransmits M1, called retransmission timeout (in each time you send a packet when finished, set a time-out timer, if the acknowledgment is received before the timeout timer expires, the timer will be revoked, or retransmission of M1, M1 group is sent copy, after receiving a copy of the corresponding acknowledgment will clear the escrowed),

     3) loss or confirm late:

When B receives the transmitted Ml A, B A sends an acknowledgment is lost, A after the retransmission timeout, B will be the same M1, this time will discard duplicate packets B M1, send an acknowledgment to A

2. Continuous ARQ protocols (sliding window)

3. retransmission timeout

4. A sliding window based flow control

          T the CP ahead there is a field called Window, also known as Advertised-Window, this field is the receiving end tell the sender how much he still can receive data buffers. Then the transmitting end can transmit data to the processing capability of the receiving terminal, without causing the receiving terminal to handle, however. Sliding window may be a mechanism to improve the efficiency of the TCP transport.

5. Congestion Control

     Tcp congestion control method of the slow start, congestion avoidance, fast retransmit, and fast recovery

TCP connection establishment:

                            

Three-way handshake:

    1) The client and the client transport control block to create the TCB, enter listen state

     Active TCP client process makes a connection request packet segment header SYN = 1, segment the packet of sequence number seq = x (A), a request to establish a connection. State to SYN-SENT (sync is sent).

    2) After the server receives the corresponding segment (A), will receive a confirmation message segment (B), the SYN and ACK are set to 1, an acknowledgment number ack = x + 1 (A confirmation of the meaning), while provided given their initial sequence number seq = y. Status by the LISTEN (monitor) becomes SYN-RCVD (received synchronization).

    3) The client receives confirmation servers, the need to send an acknowledgment to the server. Acknowledgment segment ACK = 1, an acknowledgment number ack = y + 1 (which means the confirmation of B), the sequence number seq = x + 1. State to ESTABLISHED (connection has been established). After receiving the message server segment status also becomes ESTABLISHED. ,

Why the client needs to send a second confirmation: You can prevent the failure of the request to establish a connection segment and transferred to B, but an error

若建立连接只需两次握手,客户端并没有太大的变化,仍然需要获得服务端的应答后才进入ESTABLISHED状态,而服务端在收到连接请求后就进入ESTABLISHED状态。
此时如果网络拥塞,客户端发送的连接请求迟迟到不了服务端,客户端便超时重发请求,如果服务端正确接收并确认应答,双方便开始通信,通信结束后释放连接。
此时,如果那个失效的连接请求抵达了服务端,由于只有两次握手,服务端收到请求就会进入ESTABLISHED状态,等待发送数据或主动发送数据。
但此时的客户端早已进入CLOSED状态,服务端将会一直等待下去,这样浪费服务端连接资源

TCP的连接释放 

                   

 

四次挥手

     1)客户端进程发出连接释放报文,并且停止发送数据。释放数据报文首部,FIN=1,其序列号为seq=u(等于前面已经传送过来的数据的最后一个字节的序号加1,此时,客户端进入FIN-WAIT-1(终止等待1)状态。 TCP规定,FIN报文段即使不携带数据,也要消耗一个序号。
     2)服务器收到连接释放报文,发出确认报文,ACK=1,ack=u+1,并且带上自己的序列号seq=v,此时,服务端就进入了CLOSE-WAIT(关闭等待)状态。TCP服务器通知高层的应用进程,客户端向服务器的方向就释放了,这时候处于半关闭状态,即客户端已经没有数据要发送了,但是服务器若发送数据,客户端依然要接受。这个状态还要持续一段时间,也就是整个CLOSE-WAIT状态持续的时间。
     客户端收到服务器的确认请求后,进入FIN-WAIT-2(终止等待2)状态,等待服务器发送连接释放报文(在这之前还需要接受服务器发送的最后的数据)。
     3)服务器将最后的数据发送完毕后,就向客户端发送连接释放报文,FIN=1,ack=u+1,由于在半关闭状态,服务器很可能又发送了一些数据,假定此时的序列号为seq=w,此时,服务器就进入了LAST-ACK(最后确认)状态,等待客户端的确认。
     4)客户端收到服务器的连接释放报文后,必须发出确认,ACK=1,ack=w+1,而自己的序列号是seq=u+1,此时,客户端就进入了TIME-WAIT(时间等待)状态。注意此时TCP连接还没有释放,必须经过2MSL(2*最长报文段寿命,4分钟)的时间后,当客户端撤销相应的TCB后,才进入CLOSED状态。
        服务器只要收到了客户端发出的确认,立即进入CLOSED状态。同样,撤销TCB后,就结束了这次的TCP连接。可以看到,服务器结束TCP连接的时间要比客户端早一些。

为什么要等待2MSL

       1)为了保证A发送的最后一个ACK报文段能够到达B,客户端发送出最后的ACK报文段有可能会丢失,导致处于LAST-ACK状态的B收不到FIN+ACKb报文段的确认,服务器如果没有收到ACK,将不断重复发送FIN片段。所以客户端不能立即关闭,它必须确认服务器接收到了该ACK。客户端会在发送出ACK之后进入到TIME_WAIT状态。客户端会设置一个计时器,等待2MSL的时间。如果在该时间内再次收到FIN,那么Client会重发ACK并再次等待2MSL。所谓的2MSL是两倍的MSL(Maximum Segment Lifetime)。MSL指一个片段在网络中最大的存活时间,2MSL就是一个发送和一个回复所需的最大时间。如果直到2MSL,Client都没有再次收到FIN,那么Client推断ACK已经被成功接收,则结束TCP连接。

       2)防止已失效的连接请求报文段出现在本连接中,A在发送完最后一个ACK报文段后,经过2MSL,就可以使本连接持续的时间内所产生的所有报文段都从网络消失,这样可以使下一个新的链接中不会出现这种旧的连接请求报文段。 

假设客户端和服务器端已经成功连接,之后客户端主机出现故障

      服务器再也不会收到客户端的数据,不能让服务器白白等待下去,浪费资源,这个时候就用到了保活计时器, 服务器每收到一次客户的数据,就会重新设置保活计时器,通常为两小时,若两个小时都没有收到客户端的数据,服务器就会发送一个探测报文段,以后每隔75分钟发送一次,若连续发送10个探测报文段后,客户端依然无响应,服务器就会关闭本次连接。

 

Guess you like

Origin www.cnblogs.com/jiezai/p/11301040.html