How TCP Establishes A Connection

来源:互联网 发布:简述什么是javascript 编辑:程序博客网 时间:2024/06/05 08:27

TCP Is a Connection-Oriented Protocol

TCP opens up a virtual connection between the client and serverprograms running on separate computers so that multiple and/or sporadicstreams of data can be sent over an indefinite period of time betweenthem. TCP keeps track of the packets sent by giving each one a sequencenumber with the remote server sending back acknowledgment packetsconfirming correct delivery. Programs that use TCP therefore have ameans of detecting connection failures and requesting theretransmission of missing packets. TCP is a good example of aconnection-oriented protocol.

How TCP Establishes A Connection

Any form of communication requires some form of acknowledgement forit to become meaningful. Someone knocks on the door to a house, theperson inside asks "Who is it?", to which the visitor replies, "It'sme!" Then the door opens. Both persons knew who was on the other sideof the door before it opened and now a conversation can now begin.

TCP acts in a similar way. The server initiating the connectionsends a segment with the SYN bit set in TCP header. The target replieswith a segment with the SYN and ACK bits set, to which the originatingserver replies with a segment with the ACK bit set. This SYN, SYN-ACK,ACK mechanism is often called the "three-way handshake".

The communication then continues with a series of segmentexchanges, each with the ACK bit set. When one of the servers needs toend the communication, it sends a segment to the other with the FIN andACK bits set, to which the other server also replies with a FIN-ACKsegment also. The communication terminates with a final ACK from theserver that wanted to end the session.

This is the equivalent of ending a conversation by saying "Ireally have to go now, I have to go for lunch", to which the reply is"I think I'm finished here too, see you tomorrow..." The conversationends with a final "bye" from the hungry person.

 

You can clearly see the three way handshake to connect and disconnect the session.

In this trace, the sequence number represents the serial number ofthe first byte of data in the segment. So in the first line, a randomvalue of 9766 was assigned to the first byte and all subsequent bytesfor the connection from this host will be sequentially tracked. Thismakes the second byte in the segment number 9767, the third number 9768etc. The acknowledgment number or Ack, not to be confused with the ACKbit, is the byte serial number of the next segment it expects toreceive from the other end, and the total number of bytes cannot exceedthe Win or window value that follows it. If data isn't receivedcorrectly, the receiver will re-send the requesting segment asking forthe information to be sent again. The TCP code keeps track of all thisalong with the source and destination ports and IP addresses to ensurethat each unique connection is serviced correctly.