TCP Introduction

TCP Introduction

        TCP (Transmission Control Protocol Transmission Control Protocol) is a connection-oriented, reliable transport layer protocol based on a stream of bytes defined by the IETF RFC 793. In the simplified OSI model of computer networks, it completes the fourth layer the transport layer functions specified, User Datagram Protocol (UDP) is the same layer [1] Another important transmission protocol. In the Internet protocol suite (Internet protocol suite) in, TCP layer is located above the IP layer, the intermediate layer below the application layer. Often you need a reliable application layer between different hosts, like pipelines are connected, but the IP layer does not provide a mechanism for flow, but to provide an unreliable packet switching.

TCP three-way handshake and interaction, including four off.

 

Client TCP state transition: CLOSED-> SYN_SENT-> ESTABLISHED-> FIN_WAIT_1-> FIN_WAIT_2-> TIME_WAIT-> CLOSED

 

Server TCP state transition: CLOSED-> LISTEN-> SYN-RECEIVED-> ESTABLISHED-> CLOSE_WAIT-> LAST_ACK-> CLOSED

 

 

  • Basic TCP client - server program basic framework

 

TCP state

LISTEN - listen for connection requests from the remote TCP port; 

SYN-SENT - connection request transmits the connection request after waiting for matching; 

SYN-RECEIVED - waiting for an acknowledgment of a connection request after receiving a connection request and the transmission; 

ESTABLISHED- represents an open connection, data can be transmitted to the user; 

FIN-WAIT-1 - waiting for remote TCP connection interrupt requests, or the previous connection acknowledgment of the interrupt request;

FIN-WAIT-2 - TCP waits for a connection from a remote interrupt request; 

CLOSE-WAIT - Wait sent from a local user connected to the interrupt request; 

CLOSING - Pending remote TCP connection interruptions; 

LAST-ACK - waiting for a confirmation of the original interrupt request to the remote TCP connection; 

TIME-WAIT -等待足够的时间以确保远程TCP接收到连接中断请求的确认; 

CLOSED - 没有任何连接状态。

 

TCP三次握手

TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接,如下图所示。

1)第一次握手:建立连接时,客户端A发送SYN包(SYN=x)到服务器B,并进入SYN_SENT状态,等待服务器B确认。

2)第二次握手:服务器B收到SYN包,必须确认客户A的SYN(ACK=x+1),同时自己也发送一个SYN包(SYN=y),即SYN+ACK包,此时服务器B进入SYN_RCVD状态。

3)第三次握手:客户端A收到服务器B的SYN+ACK包,向服务器B发送确认包ACK(ACK=y+1),此包发送完毕,客户端A和服务器B进入ESTABLISHED状态,完成三次握手。

完成三次握手,客户端与服务器开始传送数据。

确认序号:其数值等于发送方的发送序号 +1(即接收方期望接收的下一个序列号)。

 

TCP四次关闭

TCP的连接的拆除需要发送四个包,因此称为四次挥手(four-way handshake)。客户端或服务器均可主动发起挥手动作,在socket编程中,任何一方执行close()操作即可产生挥手操作。

1)客户端A发送一个FIN,用来关闭客户A到服务器B的数据传送。

2)服务器B收到这个FIN,它发回一个ACK,确认序号为收到的序号加1。和SYN一样,一个FIN将占用一个序号。

3)服务器B关闭与客户端A的连接,发送一个FIN给客户端A。

4)客户端A发回ACK报文确认,并将确认序号设置为收到序号加1。

以客户机发起关闭连接为例:1.服务器读通道关闭,2.客户机写通道关闭,3.客户机读通道关闭,4.服务器写通道关闭。

 

详细过程:

第一阶段 客户机发送完数据之后,向服务器发送一个FIN数据段,序列号为i;
1.服务器收到FIN(u)后,返回确认段ACK,序列号为u+1,关闭服务器读通道;
2.客户机收到ACK(u+1)后,关闭客户机写通道;
(此时,客户机仍能通过读通道读取服务器的数据,服务器仍能通过写通道写数据)

第二阶段 服务器发送完数据之后,向客户机发送一个FIN数据段,序列号为j;
3.客户机收到FIN(w)后,返回确认段ACK,序列号为w+1,关闭客户机读通道;
4.服务器收到ACK(w+1)后,关闭服务器写通道。
这是标准的TCP关闭两个阶段,服务器和客户机都可以发起关闭,完全对称。

 

 

参考文章:http://www.cnblogs.com/Jessy/p/3535612.html

http://blog.csdn.net/u010275850/article/details/46349689

https://www.cnblogs.com/shhnwangjian/p/5979757.html

Guess you like

Origin www.cnblogs.com/johnzheng/p/12163108.html