TCP三次握手过程详解(二) three way handshake of TCP

来源:互联网 发布:广州管家婆网络 编辑:程序博客网 时间:2024/06/05 08:20
Connection establishment

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 (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.


To establish a connection, each device must send aSYN and receive anACK for it from the other device. Thus, conceptually, we need to have four control messages pass between the devices. However, it's inefficient to send aSYN and anACK in separate messages when one could communicate both simultaneously. Thus, in the normal sequence of events in connection establishment, one of theSYNs and one of theACKs is sent together by setting both of the relevant bits (a message sometimes called aSYN+ACK). This makes a total of three messages, and for this reason the connection procedure is called athree-way handshake.

Key Concept:The normal process of establishing a connection between a TCP client and server involves three steps: the client sends aSYN message; the server sends a message that combines anACK for the client’sSYN and contains the server’s SYN; and then the client sends anACK for the server’sSYN. This is called the TCP three-way handshake.


 TCP “Three-Way Handshake” Connection Establishment Procedure

Client

Server

Start State

Action

Move To State

Start State

Action

Move To State

CLOSED

The client cannot do anything until the server has performed a passiveOPEN and is ready to accept a connection. (Well, it can try, but nothing will be accomplished until the server is ready.)

CLOSED

The server performs a passive OPEN, creating a transmission control block (TCB) for the connection and readying itself for the receipt of a connection request (SYN) from a client.

LISTEN

CLOSED

Step #1 Transmit: The client performs an active OPEN, creating a transmission control block (TCB) for the connection and sending aSYN message to the server.

SYN-SENT

LISTEN

The server waits for contact from a client.

SYN-SENT

The client waits to receive an ACK to the SYN it has sent, as well as the server's SYN.

LISTEN

Step #1 Receive, Step #2 Transmit: The server receives theSYN from the client. It sends a singleSYN+ACK message back to the client that contains anACK for the client'sSYN, and the server's own SYN.

SYN-RECEIVED

SYN-SENT

Step #2 Receive, Step #3 Transmit: The client receives from the server theSYN+ACK containing theACK to the client'sSYN, and theSYN from the server. It sends the server anACK for the server'sSYN. The client is now done with the connection establishment.

ESTABLISHED

SYN-RECEIVED

The server waits for an ACK to theSYN it sent previously.

ESTABLISHED

The client is waiting for the server to finish connection establishment so they can operate normally.

 

SYN-RECEIVED

Step #3 Receive: The server receives theACK to itsSYN and is now done with connection establishment.

ESTABLISHED

ESTABLISHED

The client is ready for normal data transfer operations.

 

ESTABLISHED

The server is ready for normal data transfer operations.


TCP 3-Way Handshake Diagram

Below is a (very) simplified diagram of the TCP 3-way handshake process. Have a look at the diagram on the right as you examine the list of events on the left.

  EVENTDIAGRAM

Host A sends a TCP SYNchronize packet to Host B

Host B receives A's SYN

Host B sends a SYNchronize-ACKnowledgement

Host A receives B's SYN-ACK

Host A sends ACKnowledge

Host B receives ACK.
TCP socket connection is ESTABLISHED.

tcp three-way handshake,syn,syn-ack,ack

TCP Three Way Handshake
(SYN,SYN-

原创粉丝点击