Foreword
In the course of the interview, TCP transport protocol often occur. Before I attended the interview process he asked, and now my turn to interview other people, I would ask some pertinent questions. As a developer, no matter what kind of development language, the most basic knowledge of the network must be understood thoroughly, so as to get a better career development.
TCP Link Protocol Overview
We need to establish a TCP three-way handshake to establish, but you need to disconnect the four-way handshake. The whole process is shown below:
Connection establishment
First Client sends a connection request message, Server segment reply ACK packet after accepting connections, and allocate resources for this connection. Client ACK packet also occur after receiving ACK packet to the Server segment and allocate resources so that the TCP connection is established.
Process connection is disconnected
Client side may be disconnected from the end, it may be a Server side. Client-initiated interrupt assume a connection request, it first sends FIN packets. Server-side after receiving FIN messages, but if there is no data transmission is complete, you do not hurry to close the Socket, you can continue to send data. So the server to send ACK, told Client-side: The request has been received, but I'm not ready, please continue to wait for news stopped. Client-side this time entered FIN_WAIT state, continue to wait FIN packets Server side. Server data to determine when the end has been sent, then send a FIN packet to the Client side, told Client-side: data sent over the server side, ready to close the connection. Client after client receives FIN message, you know that you can close the connection, but he still did not trust the Internet, it sends an ACK after entering TIME_WAIT state, the client receives ACK Server, you can disconnect the know. Client-waiting after 2MSL still not received a reply, the end prove Server is down to normal, and finally, Client-side can also close the connection to this, TCP connection has been completely shut down! Closing the connection procedure as shown below:
Three-way handshake
TCP is the host to host transmission control protocol layer, to provide reliable connectivity services, three-way handshake to establish a connection confirmation:
位码即tcp标志位,有6种标示:SYN(synchronous建立联机) ACK(acknowledgement 确认) PSH(push传送) FIN(finish结束) RST(reset重置) URG(urgent紧急)
Sequence number(顺序号码) Acknowledge number(确认号码)
第一次握手:主机A发送位码为syn=1,随机产生seq number=1234567的数据包到服务器,主机B由SYN=1知道,A要求建立联机;
第二次握手:主机B收到请求后要确认联机信息,向A发送ack number=(主机A的seq+1),syn=1,ack=1,随机产生seq=7654321的包
第三次握手:主机A收到后检查ack number是否正确,即第一次发送的seq number+1,以及位码ack是否为1,若正确,主机A会再发送ack number=(主机B的seq+1),ack=1,主机B收到后确认seq值与ack=1则连接建立成功。
完成三次握手,主机A与主机B开始传送数据。
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接。
第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认;
第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn=k),即SYN+ACK包,此时服务器进入SYN_RECV状态; 第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送完毕,客户端和服务器进入ESTABLISHED状态,完成三次握手。 完成三次握手,客户端与服务器开始传送数据.
实例:
IP 192.168.1.116.3337 > 192.168.1.123.7788: S 3626544836:3626544836
IP 192.168.1.123.7788 > 192.168.1.116.3337: S 1739326486:1739326486 ack 3626544837
IP 192.168.1.116.3337 > 192.168.1.123.7788: ack 1739326487,ack 1
第一次握手:192.168.1.116发送位码syn=1,随机产生seq number=3626544836的数据包到192.168.1.123,192.168.1.123由SYN=1知道192.168.1.116要求建立联机;
第二次握手:192.168.1.123收到请求后要确认联机信息,向192.168.1.116发送ack number=3626544837,syn=1,ack=1,随机产生seq=1739326486的包;
第三次握手:192.168.1.116收到后检查ack number是否正确,即第一次发送的seq number+1,以及位码ack是否为1,若正确,192.168.1.116会再发送ack number=1739326487,ack=1,192.168.1.123收到后确认seq=seq+1,ack=1则连接建立成功。
图解:
一个三次握手的过程(图1,图2)
(图1)
(图2)
第一次握手的标志位(图3)
我们可以看到标志位里面只有个同步位,也就是在做请求(SYN)
(图3)
第二次握手的标志位(图4)
我们可以看到标志位里面有个确认位和同步位,也就是在做应答(SYN + ACK)
(图4)
第三次握手的标志位(图5)
我们可以看到标志位里面只有个确认位,也就是再做再次确认(ACK)
(图5)
一个完整的三次握手也就是 请求---应答---再次确认
转自:http://swiftlet.net/archives/1082