1.三次握手的维基百科解释:
To establish a connection, TCP uses a three-way handshake. Before a client attempts to connect with a server, the server must first bind to and listen at a port to open it up for connections: this is called a passive open. Once the passive open is established, a client may initiate an active open. To establish a connection, the three-way (or 3-step) handshake occurs:
- SYN: The active open is performed by the client sending a SYN to the server. The client sets the segment's sequence number to a random value A.
- SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the received sequence number i.e. A+1, and the sequence number that the server chooses for the packet is another random number, B.
- ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement value i.e. A+1, and the acknowledgement number is set to one more than the received sequence number i.e. B+1.
At this point, both the client and server have received an acknowledgment of the connection. The steps 1, 2 establish the connection parameter (sequence number) for one direction and it is acknowledged. The steps 2, 3 establish the connection parameter (sequence number) for the other direction and it is acknowledged. With these, a full-duplex communication is established.
三次握手的目的就是建立连接。那什么SYN,ACK.
- SYN(Synchronize Sequence Number)
客户端悬着一个随机序列号X,并发送一个SYN分组,其中可能换包括其他TCP标志和选项。 - SYN ACK
服务器给X加1,并选择自己的一个随机序列号Y,追加自己的标注和选项,然后返回响应。 - ACK(Acknowledgement)
客户端给X和Y加1并发送握手期间的最后一个ACK分组。
三次握手弯沉够,客户端与服务器就可以通信了。客户端可以在发送ACK分组之后立即发送数据,二服务器必须等收到ACK分组之后才能发送数据。