TCP three-way handshake fourth wave of Linux Advanced

The TCP ( Transfer Control Protocol ) transport control protocol

One, two transmission modes:

TCP for connection

reliable

Commonly used in Point to Point

Micro letter

UDP a connectionless

high speed

Commonly used in the opposite point

Live

Second, the data direction: in the communication of the internet, the client is always actively connected to the server

Third, the port role : a virtual computer interface for data communication

Fourth, the port type:

Physical port

For connecting a physical device, such as cable, the USB , etc.

Virtual port

For data transmission, for example: 80/21/22 etc.

Added: Virtual Port range: 1-65535

Five, three-way handshake:

SEQ ID SEQ : accounting . 4 bytes for the data segment order mark, the TCP all data bytes transmitted are connected to a programmed number, the first byte of the random number generated by the local; ed to bytes after the serial number, give each segment is assigned a number; the sequence number seq is the first byte of the data segment number.

No acknowledgment ACK : accounting . 4 bytes, the next expects to receive the other of the first data byte of a segment number; first byte of the segment sequence number indicates the number of data carrying; numbers refer confirmed It is expected to be received next byte number; therefore the current number of the last byte of the segment +1 is the acknowledgment number.

Confirmed the ACK : accounting . 1 bit only when ACK = 1 , the acknowledgment number field is valid. ACK = 0 , the acknowledgment number is invalid.

Synchronizing the SYN : for synchronizing serial connection establishment. When the SYN =. 1 , the ACK = 0 indicates when: It is a connection request packet segments. If the agreed connection, in the response message segments such that the SYN =. 1 , the ACK =. 1 . Accordingly, the SYN =. 1 indicates that this is a connection request, or the connection acceptance message. SYN This flag only TCP when connections will build production is set to 1 , after the completion of the handshake SYN flag is set to 0 .

Terminating the FIN : for releasing a connection. FIN = 1 represents: a data sender of this segment has been transmitted, and for the release of the transport connection.

Note: the ACK , the SYN and FIN these capitalized word representation flag, whose value is either 1 or a 0 ; ACK , SEQ lowercase number word representation.

1 , the handshake process:


00

SYN = 0, ACK = O

Logogram

No operation

10

SYN = 1, ACK = 0

SYN=1

Connection request

11

SYN = 1, ACK = 1

SYN = 1, ACK = 1

Request confirmation

01

SYN = 0, ACK = 1

ACK=1

Connection confirmation

The first handshake: connection is established, the client sends syn packets ( syn = J ) to the server, and enters the SYN_SENT state, waiting for the server to confirm; the SYN : synchronization sequence number ( the Synchronize Sequence Numbers ).

Second handshake: server receives syn packets, must confirm the customer's SYN ( ACK = + J. 1 ), while themselves transmit a SYN packet ( syn = K ), i.e. SYN + ACK packet, then the server enters SYN_RECV state.

Third handshake: a client server receives the SYN + ACK packet, the server sends an acknowledgment packet to the ACK (ACK = K +. 1 ), this packet is sent, the client and the server into the ESTABLISHED ( the TCP connection succeeds) state, complete the three handshake.

2 , field meanings:

Field

meaning

URG

The urgent pointer is valid. Is 1 , it represents a bit needs to be a priority processing

ACK

Represents the response acknowledgment number is valid, is generally set to 1 .

PA

Expressed DATA data transmission, the receiver application immediately prompts from TCP to read data buffer away.

RST

Other requirements to re-establish a connection reset.

SYN

Request to establish a connection and an initial value setting its sequence number in a sequence number field. Establish a connection, is set to 1 .

END

Want to disconnect disables the connection

3 Why do you need three-way handshake:

A handshake

Break the TCP reliability

The second handshake

Server wait

4 , English terminology:

LISTEN

monitor

SYN-SENT

SYN Sent

SYN-RCVD

SYN Received

ESTABLISHED

Already connected

Six, four off

Disconnection process:

00

FINE = 0, ACK = 0

Logogram

No operation

10

FINE = 1, ACK = 0

END = 1

Disconnect request

11

FINE = 1, ACK = 1

FINE = 1, ACK = 1

Request confirmation

01

FINE = 0, ACK = 1

ACK=1

断开确认

1)客户端进程发出连接释放报文,并且停止发送数据。释放数据报文首部,FIN=1,其序列号为seq=u(等于前面已经传送过来的数据的最后一个字节的序号加1),此时,客户端进入FIN-WAIT-1(终止等待1)状态。 TCP规定,FIN报文段即使不携带数据,也要消耗一个序号。

2)服务器收到连接释放报文,发出确认报文,ACK=1ack=u+1,并且带上自己的序列号seq=v,此时,服务端就进入了CLOSE-WAIT(关闭等待)状态。TCP服务器通知高层的应用进程,客户端向服务器的方向就释放了,这时候处于半关闭状态,即客户端已经没有数据要发送了,但是服务器若发送数据,客户端依然要接受。这个状态还要持续一段时间,也就是整个CLOSE-WAIT状态持续的时间。

3)客户端收到服务器的确认请求后,此时,客户端就进入FIN-WAIT-2(终止等待2)状态,等待服务器发送连接释放报文(在这之前还需要接受服务器发送的最后的数据)。

4)服务器将最后的数据发送完毕后,就向客户端发送连接释放报文,FIN=1ack=u+1,由于在半关闭状态,服务器很可能又发送了一些数据,假定此时的序列号为seq=w,此时,服务器就进入了LAST-ACK(最后确认)状态,等待客户端的确认。

5)客户端收到服务器的连接释放报文后,必须发出确认,ACK=1ack=w+1,而自己的序列号是seq=u+1,此时,客户端就进入了TIME-WAIT(时间等待)状态。注意此时TCP连接还没有释放,必须经过2∗∗MSL(最长报文段寿命)的时间后,当客户端撤销相应的TCB后,才进入CLOSED状态。

6)服务器只要收到了客户端发出的确认,立即进入CLOSED状态。同样,撤销TCB后,就结束了这次的TCP连接。可以看到,服务器结束TCP连接的时间要比客户端早一些。

常见面试题

【问题1】为什么连接的时候是三次握手,关闭的时候却是四次握手?

答:因为当Server端收到Client端的SYN连接请求报文后,可以直接发送SYN+ACK报文。其中ACK报文是用来应答的,SYN报文是用来同步的。但是关闭连接时,当Server端收到FIN报文时,很可能并不会立即关闭SOCKET,所以只能先回复一个ACK报文,告诉Client端,"你发的FIN报文我收到了"。只有等到我Server端所有的报文都发送完了,我才能发送FIN报文,因此不能一起发送。故需要四步握手。

【问题2】为什么TIME_WAIT状态需要经过2MSL(最大报文段生存时间)才能返回到CLOSE状态?

答:虽然按道理,四个报文都发送完毕,我们可以直接进入CLOSE状态了,但是我们必须假象网络是不可靠的,有可以最后一个ACK丢失。所以TIME_WAIT状态就是用来重发可能丢失的ACK报文。在Client发送出最后的ACK回复,但该ACK可能丢失。Server如果没有收到ACK,将不断重复发送FIN片段。所以Client不能立即关闭,它必须确认Server接收到了该ACKClient会在发送出ACK之后进入到TIME_WAIT状态。Client会设置一个计时器,等待2MSL的时间。如果在该时间内再次收到FIN,那么Client会重发ACK并再次等待2MSL。所谓的2MSL是两倍的MSL(Maximum Segment Lifetime)MSL指一个片段在网络中最大的存活时间,2MSL就是一个发送和一个回复所需的最大时间。如果直到2MSLClient都没有再次收到FIN,那么Client推断ACK已经被成功接收,则结束TCP连接。

【问题3】为什么不能用两次握手进行连接?

答:3次握手完成两个重要的功能,既要双方做好发送数据的准备工作(双方都知道彼此已准备好),也要允许双方就初始序列号进行协商,这个序列号在握手过程中被发送和确认。

       现在把三次握手改成仅需要两次握手,死锁是可能发生的。作为例子,考虑计算机SC之间的通信,假定CS发送一个连接请求分组,S收到了这个分组,并发 送了确认应答分组。按照两次握手的协定,S认为连接已经成功地建立了,可以开始发送数据分组。可是,CS的应答分组在传输中被丢失的情况下,将不知道S 是否已准备好,不知道S建立什么样的序列号,C甚至怀疑S是否收到自己的连接请求分组。在这种情况下,C认为连接还未建立成功,将忽略S发来的任何数据分 组,只等待连接确认应答分组。而S在发出的分组超时后,重复发送同样的分组。这样就形成了死锁。

【问题4】如果已经建立了连接,但是客户端突然出现故障了怎么办?

TCP还设有一个保活计时器,显然,客户端如果出现故障,服务器不能一直等下去,白白浪费资源。服务器每收到一次客户端的请求后都会重新复位这个计时器,时间通常是设置为2小时,若两小时还没有收到客户端的任何数据,服务器就会发送一个探测报文段,以后每隔75秒钟发送一次。若一连发送10个探测报文仍然没反应,服务器就认为客户端出了故障,接着就关闭连接。

Guess you like

Origin www.cnblogs.com/renyz/p/11233858.html