In-depth understanding of the TCP three-way handshake and its source code

  • Introduction to TCP

    TCP service:

  Transmission Control Protocol (TCP, Transmission Control Protocol) is a connection-oriented, reliable transport layer protocol based on a stream of bytes by the IETF 793 of the RFC  . TCP is designed to adapt to support multi-network applications layered protocol hierarchy. Connected between the pair of host computers of different processes but interconnected computer communications networks rely on TCP provides reliable communication services. TCP assumes it can obtain a simple, potentially unreliable datagram service from the lower level protocols. In principle, TCP should be able to operate over a variety of communication systems connected to a packet switched from a hard-wired or circuit-switched network.
  

  The user data TCP segment constituting the package, it starts a timer when transmitting data, the other end of the received data confirmed the data of the out of sequence reordering, discarding duplicate data. TCP provides a reliable byte stream connection-oriented service, means that the two connection-oriented applications using TCP (B / S) with each other before exchanging data, must first establish a TCP connection, the process similar to the call, to dial ringing, waiting for the other to say hello, and then reply. In a TCP connection, only two parties communicate with each other.
  

  Reliability TCP from:
(1) the application data is divided into TCP optimal transmission data block
(2) when sending a TCP segment, starting a timer, waiting for a destination point to acknowledge receipt of the packet, if not timely receive a confirmed that it would resend the message.
(3) When the TCP connection receives the data sent, it will send a fraction of a delayed acknowledgment.
(4) TCP header holding it and test data, and, this is a test end to end and the aim of detecting whether a change occurs in the data transmission process. (There is an error, no acknowledgment, the sender will retransmit)
(. 5) TCP IP packets are transmitted, the data are unordered IP, TCP after receiving all sorts of data, and then to the application layer
(6 ) IP datagram will be repeated, so TCP will de-emphasis
(. 7) TCP provides flow control, where each TCP connection has a fixed buffer space. Receiving TCP only allows the other end of the transmit buffer can accept data.
(. 8) of the TCP byte stream without any explanation, explanation of the application layer from the byte stream interpretation both TCP connections.
  

  TCP messages

  TCP data is encapsulated in an IP data.

 

 

 

 

  TCP header

 

 

 

 

   TCP three-way handshake

  TCP is a connection-oriented protocol, regardless of which direction the other party before sending data, must first establish a connection between the two sides, following procedures established a connection there.
  1, requester (client) sends a SYN segment specified in the client wants to port, and an initial sequence number (ISN) connection to the server, the SYN packets range. 1.
  2, the server sends back to the server's initial sequence number contained in the SYN segment (segment 2) as a response. At the same time, the acknowledgment number to customers ISN plus one SYN segment to the customer for confirmation. A SYN will take a character.
  3, the customer must explicitly set the server's serial number ISN plus 1 for confirmation (segment 3) of the server's SYN segment
  4, the three segments of the complete connection is established, this process becomes three-way handshake.

 

 

  TCP connection termination process (fourth wave)

  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(最后确认)状态,等待客户端的确认。
客户端收到服务器的连接释放报文后,必须发出确认,ACK=1,ack=w+1,而自己的序列号是seq=u+1,此时,客户端就进入了TIME-WAIT(时间等待)状态。注意此时TCP连接还没有释放,必须经过2∗MSL(最长报文段寿命)的时间后,当客户端撤销相应的TCB后,才进入CLOSED状态。
  4、服务器只要收到了客户端发出的确认,立即进入CLOSED状态。同样,撤销TCB后,就结束了这次的TCP连接。可以看到,服务器结束TCP连接的时间要比客户端早一些。

 

 

connect及bind、listen、accept背后的三次握手

  首先明确三次握手发生在什么时候。

  经过我的验证,在客户端执行connect的时候,便是向已经listen的服务器发出三次握手,等connect返回的时候,三次握手已经完成,和accept没有任何关系。验证过程如下。

客户端程序只执行到connect,服务器端程序只执行到listen,然后用tcpdump对本地进行抓包。这里要注意tcpdump想抓本地还回的包需要监听 lo 也就是tcpdump -i lo port 12346 

再用netstat观察端口状态 发现两边都是establish

int main ()
{
    int sock;
    sock=socket(AF_INET,SOCK_STREAM,0);
    assert(sock>=0);
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(12346);
    inet_pton(AF_INET,"127.0.0.1",&addr.sin_addr);
    int err=connect(sock,(sockaddr*)&addr,sizeof(addr));
    assert(err==0);
    sleep(10000);
}
int main ()
{
    int listenfd;
    listenfd=socket(AF_INET,SOCK_STREAM,0);
    assert(listenfd>=0);
    int opt=1;
    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(12346);
    inet_pton(AF_INET,"127.0.0.1",&addr.sin_addr);
    int err=bind(listenfd,(sockaddr*)&addr,sizeof(addr));
    assert(err==0);
    err=listen(listenfd,10);
    assert(err==0);
    sleep(10000);
}

 

 

 

  再来看listen函数的参数。它的作用是告诉内核设置连接队列的长度。

  内核为每一个listen状态的套接字设置两个队列,未完成连接队列和已完成连接队列,这两个队列共用listen设置的连接长度。

当客户端发送syn报文的时候,服务器检测连接队列是否已满,如果满了就丢弃这个syn,如果没满就把这个链接放入未完成队列,发送ack和syn,当客户端收到ack和syn之后,会发送ack报文,并且从connect返回,服务器收到这个ack后把连接从未完成队列中取出放入已完成队列,等待accept把这个链接取走。此时三次握手已经全部完成,两端连接都是establish状态。

实际上连接在未完成队列中的时间是很短的,这段时间代表服务器发送syn和ack并且从客户端收到ack的时间,没有特殊情况的话,一般很快就会完成,但是如果服务器发送syn+ack后超过一定时间都没有收到客户端的ack,服务器会把这个链接丢弃掉,超时时限一般被设为75秒。

  以下是我把listen的参数设为1,开了三个客户端向同一个服务器发起连接,并且服务器没有accept的抓包情况。

 

 

  可见在等待队列里最多有两个链接,第三个client一直在发送syn,因为没有收到ack,所以执行超时重传,超时时间每次*2。打印了每个client从connect返回后的errno。

 

 

  errno110表示  Connection timed out

  最后来说accept。这个函数只是把已完成队列中的链接取出来,如果已完成连接队列里没有连接,accept就会阻塞。

  这里要说一点,就算是队列满了,新的client在发起连接之后,服务器也并没有拒绝他的syn,只是把它丢弃了,这样client还是会向服务器发送syn,只不过因为超时重传机制,每次发送的间隔时间会变长,如果在这个阶段服务器accept了已完成的连接,那么连接队列就会空出来,这样受到syn后,服务器还是会正常的和客户端完成三次握手。

 

Guess you like

Origin www.cnblogs.com/smjsoftware/p/12104623.html