Break up more trouble than to determine a relationship? Also between this computer!

 

                                                                             

 

Once upon a time, there is no Internet, we communicate mainly by writing letters. A small bar and a small US high school students have a crush each other, two people went to different universities of the city to go to school. Small bar in the university felt empty and alone cold, the courage to write a letter to the US confession small.

 

After a small bar sent a letter, my heart has been very nervous, afraid in case the address is wrong, the letter sent less than how to do it?

 

After Mel received a letter from a small bundle, unable to bear the hearts of surprise, this little bar finally opens ah. Then immediately back to the letter, the United States also worry about small little bar not receive the letter, reminding small bar immediately after receiving the letter reply.

 

After a small bar received a small US letter, that he gave Mary's address is right, Mary's address is right, both sides believe can receive. Mel must worry her letter I did not get it?

 

So the little bar and went straight back to the small US letter.

 

Little America after receiving the letter, the two sides know that there is nothing wrong address, the parties can receive a letter, can rest assured. They kept two people sent to and fro many, many letters, love couple the most expensive of paper. . .

 

 

The network connection between the computer protocol many years later emerged, there is something called TCP is responsible for the computer, the client and the server then wanted to start a small bar and a small beauty communicate the same.

 

1. The client sends a SYN packet, and sets the sequence number seq j, the client enters a state SYNC_SENT

(Small bar to small US letter confession)

 

2. After the server receives the data packet, a SYN packet is provided seq = k, and returns an ACK packet provided seq = j + 1, enters the SYN_RCVD state.

(Small US small bar to reply agreed to establish a relationship, after a small bar received a letter confirming communication between them is no problem)

 

3. After the client receives the ACK packet, represents a one-way client to server connection is successful, the state becomes establised client, the server and the client will respond to the SYN packet, it returns an ACK packet, seq of k + 1 .

 

After the server receives the ACK packet the client, enter ESTABLISED state, it indicates that the service is connected to client success.

(Small bar to small US letter acknowledged receipt of the letter Mel, Mel after receiving the letter, confirming communication between them is no problem)

 

Since then, they are connected, you can rest assured that the data is sent to each other.

 

Below to map a serious point:

 

Perhaps you have questions, why should three-way handshake? Two okay?

 

The answer is no.

 

按上面的故事举例,第一次握手:小扎写信向小美表白;第二次握手:小美向小扎回信;注意,这时候小扎(客户端)的连接建立成功了,因为小扎(客户端)已经可以确定自己的发送和接收都没有问题。

 

但是小美(服务器)只是收到了小扎(客户端)的来信,确定了自己接收数据没问题,但是自己寄出去的信小扎(客户端)能不能收到还不确定。

 

于是有了第三次握手:小扎向小美寄出了确认回信。小美(服务器)收到信后,就是知道自己发送数据的功能没问题,小扎确实收到信了,确认连接建立成功了。

 

 

 

小扎和小美热恋了几年后,他们之间的感觉越来越微妙,慢慢的小美发现小扎对自己的关心越来越少了。终于小扎抵不住异地恋的痛苦,移情别恋了,看上了新来的学妹,小扎决定分手了,给小美寄了封分手信。

 

分手就算了,居然还有脸要回送我的东西!渣男!小美,收到信后,气得跳脚,立马回了封信,让他滚蛋。

 

第二天,小美就去邮局,把渣男送给她的东西都打包寄还了给他。小美还不忘在信中提醒,收到东西之后给我回封信,不要到时候耍赖说没收到,我再也不想看见你了!渣男!

 

小扎收到小美寄的包裹之后,立马回了封信,说东西收到了,我们以后也别联系了。

 

一段恋情就这么结束了。。。

 

 

情侣之间分分合合,当然计算机之间的连接也经常需要断开连接,断开的方式竟然和情侣之间很相似。

 

1.客户端发送一个FIN包,表示要断开连接,并设置seq序列号为m到服务器端,客户端进入FIN_WAIT_1状态。

(小扎写分手信给小美,进入等待回信的状态)

 

2.服务器端收到客户端的FIN包后,就知道客户端想要断开连接了,于是返回一个ACK包,设置seq为m+1,服务器端进入CLOSE_WAIT状态。服务器端对客户端说,我知道你想要断开连接了,不过先等等,我这还有些数据没发完,你等我发完再关闭。

 

客户端收到服务器端的确认后,进入FIN_WAIT_2状态,客户端现在只接收服务器端的数据,不再发送数据。

(小美回信给小扎,同意分手,但是东西要整理下再寄给小扎,小扎进入等待包裹状态)

 

3.服务器端发送完所有数据之后,发送一个FIN报文,设置seq序列号为n,进入LAST_ACK状态,表示我的数据都已经发送完了,你可以断开连接了。

(小美给小扎寄回包裹,表示我们已经两清了,你收到之后给我回个信,就可以滚蛋了)

 

4.客户端收到服务器端的FIN包后,返回一个ACK包,设置seq序列号为n+1,客户端就进入了TIME-WAIT(时间等待)状态。服务器只要收到了客户端发出的确认,立即进入CLOSED状态,关闭的连接。

(小扎回信给小美说,你的包裹我收到了,我们以后不用再联系了)

 

6.注意此时TCP连接还没有释放,必须经过2MSL(最长报文段寿命)的时间后,才进入CLOSED状态。

(小扎。。。编不下去了。。。)

 

自此,客户端和服务器端就断开了连接。

 

下面来一张正经点的图:

 

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

 

讲道理,四个报文都发送完毕,我们应该可以直接进入CLOSE状态了,但是我们必须假象网络是不可靠的。如果客户端发送出最后的ACK回复,服务器没有收到,服务器将不断重复发送FIN报文。所以客户端不能立即关闭,它必须确认服务器端接收到了该ACK。

 

客户端会在发送出ACK之后进入到TIME_WAIT状态,同时设置一个计时器,等待2MSL的时间。如果在该时间内再次收到FIN,那么客户端会重发ACK并再次等待2MSL。

 

所谓的2MSL是两倍的MSL(Maximum Segment Lifetime)。MSL指一个片段在网络中最大的存活时间,2MSL就是一个发送和一个回复所需的最大时间。如果直到2MSL,客户端都没有再次收到FIN,那么客户端推断ACK已经被成功接收,则结束TCP连接。

 

 

TCP断开连接为什么比建立连接多一个步骤呢?其实很简单,因为谈恋爱的时候还没有共同财产,但是分手的时候还需要分东西呀!

 

 

 

 

 

 

发布了13 篇原创文章 · 获赞 169 · 访问量 6649

Guess you like

Origin blog.csdn.net/zhanyd/article/details/103552137