首页 > P2P > p2p-tcp滑动窗口机制、连接机制
2015一月7

p2p-tcp滑动窗口机制、连接机制

[隐藏]

1.tcp滑动窗口机制

(1).窗口机制

滑动窗口协议的基本原理就是在任意时刻,发送方都维持了一个连续的允许发送的帧的序号,称为发送窗口;同时,接收方也维持了一个连续的允许接收的帧的序号,称为接收窗口。发送窗口和接收窗口的序号的上下界不一定要一样,甚至大小也可以不同。不同的滑动窗口协议窗口大小一般不同。发送方窗口内的序列号代表了那些已经被发送,但是还没有被确认的帧,或者是那些可以被发送的帧。下面举一个例子(假设发送窗口尺寸为2,接收窗口尺寸为1):

1.gif

分析:①初始态,发送方没有帧发出,发送窗口前后沿相重合。接收方0号窗口打开,等待接收0号帧;②发送方打开0号窗口,表示已发出0帧但尚确认返回信息。此时接收窗口状态不变;③发送方打开0、1号窗口,表示0、1号帧均在等待确认之列。至此,发送方打开的窗口数已达规定限度,在未收到新的确认返回帧之前,发送方将暂停发送新的数据帧。接收窗口此时状态仍未变;④接收方已收到0号帧,0号窗口关闭,1号窗口打开,表示准备接收1号帧。此时发送窗口状态不变;⑤发送方收到接收方发来的0号帧确认返回信息,关闭0号窗口,表示从重发表中删除0号帧。此时接收窗口状态仍不变;⑥发送方继续发送2号帧,2号窗口打开,表示2号帧也纳入待确认之列。至此,发送方打开的窗口又已达规定限度,在未收到新的确认返回帧之前,发送方将暂停发送新的数据帧,此时接收窗口状态仍不变;⑦接收方已收到1号帧,1号窗口关闭,2号窗口打开,表示准备接收2号帧。此时发送窗口状态不变;⑧发送方收到接收方发来的1号帧收毕的确认信息,关闭1号窗口,表示从重发表中删除1号帧。此时接收窗口状态仍不变。

若从滑动窗口的观点来统一看待1比特滑动窗口、后退n及选择重传三种协议,它们的差别仅在于各自窗口尺寸的大小不同而已。1比特滑动窗口协议:发送窗口=1,接收窗口=1;后退n协议:发窗口>1,接收窗口>1;选择重传协议:发送窗口>1,接收窗口>1。

(2).1比特滑动窗口协议

当发送窗口和接收窗口的大小固定为1时,滑动窗口协议退化为停等协议(stop-and-wait)。该协议规定发送方每发送一帧后就要停下来,等待接收方已正确接收的确认(acknowledgement)返回后才能继续发送下一帧。由于接收方需要判断接收到的帧是新发的帧还是重新发送的帧,因此发送方要为每一个帧加一个序号。由于停等协议规定只有一帧完全发送成功后才能发送新的帧,因而只用一比特来编号就够了。其发送方和接收方运行的流程图如图所示。

1.gif

(3).后退n协议

由于停等协议要为每一个帧进行确认后才继续发送下一帧,大大降低了信道利用率,因此又提出了后退n协议。后退n协议中,发送方在发完一个数据帧后,不停下来等待应答帧,而是连续发送若干个数据帧,即使在连续发送过程中收到了接收方发来的应答帧,也可以继续发送。且发送方在每发送完一个数据帧时都要设置超时定时器。只要在所设置的超时时间内仍收到确认帧,就要重发相应的数据帧。如:当发送方发送了N

(4).选择重传协议

在后退n协议中,接收方若发现错误帧就不再接收后续的帧,即使是正确到达的帧,这显然是一种浪费。另一种效率更高的策略是当接收方发现某帧出错后,其后继续送来的正确的帧虽然不能立即递交给接收方的高层,但接收方仍可收下来,存放在一个缓冲区中,同时要求发送方重新传送出错的那一帧。一旦收到重新传来的帧后,就可以原已存于缓冲区中的其余帧一并按正确的顺序递交高层。这种方法称为选择重发(SELECTICE REPEAT),其工作过程如图所示。显然,选择重发减少了浪费,但要求接收方有足够大的缓冲区空间。个帧后,若发现该N帧的前一个帧在计时器超时后仍未返回其确认信息,则该帧被判为出错或丢失,此时发送方就不得不重新发送出错帧及其后的N帧。

从这里不难看出,后退n协议一方面因连续发送数据帧而提高了效率,但另一方面,在重传时又必须把原来已正确传送过的数据帧进行重传(仅因这些数据帧之前有一个数据帧出了错),这种做法又使传送效率降低。由此可见,若传输信道的传输质量很差因而误码率较大时,连续测协议不一定优于停止等待协议。此协议中的发送窗口的大小为k,接收窗口仍是1。

9e9f80e2c70e7b79ee5084cec659e540_thumb.gif

2.TCP三次握手四次挥手

建立TCP需要三次握手才能建立,而断开连接则需要四次握手。整个过程如下图所示:

2.jpg


先来看看如何建立连接的。

2.png

首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源。Client端接收到ACK报文后也向Server段发生ACK报文,并分配资源,这样TCP连接就建立了。

那如何断开连接呢?简单的过程如下:

21.png

【注意】中断连接端可以是Client端,也可以是Server端。

假设Client端发起中断连接请求,也就是发送FIN报文。Server端接到FIN报文后,意思是说"我Client端没有数据要发给你了",但是如果你还有数据没有发送完成,则不必急着关闭Socket,可以继续发送数据。所以你先发送ACK,"告诉Client端,你的请求我收到了,但是我还没准备好,请继续你等我的消息"。这个时候Client端就进入FIN_WAIT状态,继续等待Server端的FIN报文。当Server端确定数据已发送完成,则向Client端发送FIN报文,"告诉Client端,好了,我这边数据发完了,准备好关闭连接了"。Client端收到FIN报文后,"就知道可以关闭连接了,但是他还是不相信网络,怕Server端不知道要关闭,所以发送ACK后进入TIME_WAIT状态,如果Server端没有收到ACK则可以重传。“,Server端收到ACK后,"就知道可以断开连接了"。Client端等待了2MSL后依然没有收到回复,则证明Server端已正常关闭,那好,我Client端也可以关闭连接了。Ok,TCP连接就这样关闭了

【注意】 在TIME_WAIT状态中,如果TCP client端最后一次发送的ACK丢失了,它将重新发送。TIME_WAIT状态中所需要的时间是依赖于实现方法的。典型的值为30秒、1分钟和2分钟。等待之后连接正式关闭,并且所有的资源(包括端口号)都被释放。


【问题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报文。

文章作者:hgy413
本文地址:https://hgy413.com/2157.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

20 Responses to “p2p-tcp滑动窗口机制、连接机制”

  1. #1 minecraft 回复 | 引用 Post:2018-10-05 08:50

    This piece of writing is genuinely a fastidious one it assists new the
    web viewers, who are wishing for blogging.

  2. #2 minecraft 回复 | 引用 Post:2018-10-06 09:00

    Hi there everyone, it’s my first go to see at this site,
    and piece of writing is actually fruitful for me, keep up posting such articles.

  3. #3 Coconut Oil Benefits 回复 | 引用 Post:2018-10-19 00:47

    This post gives clear idea in support of the new users of blogging, that actually how to do blogging and site-building.

  4. Thanks for your marvelous posting! I definitely enjoyed reading it, you’re a great author.I will be
    sure to bookmark your blog and may come back at
    some point. I want to encourage one to continue your great job,
    have a nice day!

  5. Descargar facebook
    hi!,I love your writing so much! share we keep in touch extra
    about your article on AOL? I need a specialist on this area to unravel my problem.
    Maybe that’s you! Having a look ahead to see you.
    descargar facebook

  6. #6 Quest Bars 回复 | 引用 Post:2018-11-09 02:29

    Valuable info. Fortunate me I discovered your web site by chance, and I’m shocked why this twist of fate didn’t came about in advance!
    I bookmarked it.

  7. What’s up friends, good piece of writing and nice
    urging commented at this place, I am actually enjoying by these.

  8. #8 Sling TV 回复 | 引用 Post:2018-11-11 12:02

    I am really enjoying the theme/design of your weblog.
    Do you ever run into any internet browser compatibility problems?

    A couple of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Safari.
    Do you have any advice to help fix this problem?

  9. What a stuff of un-ambiguity and preserveness of valuable experience about unexpected
    feelings.

  10. I have read so many articles or reviews concerning
    the blogger lovers except this piece of writing is in fact a fastidious article, keep it up.

  11. Amazing! This blog looks exactly like my old one! It’s on a completely different subject but it has
    pretty much the same page layout and design. Outstanding
    choice of colors!

  12. #12 descargar facebook 回复 | 引用 Post:2018-11-22 02:46

    Wonderful, what a blog it is! This website gives useful data to us, keep it up.

  13. #13 pickle lanyard 回复 | 引用 Post:2020-05-14 09:14

    Just desire to say your article is as amazing.
    The clarity in your post is simply spectacular and i can assume you’re an expert on this subject.
    Well with your permission allow me to grab your
    RSS feed to keep up to date with forthcoming post.
    Thanks a million and please continue the enjoyable
    work.

  14. #14 g these 回复 | 引用 Post:2020-06-11 02:07

    I’ve been surfing online more than 3 hours today, yet I never discovered any fascinating article like yours.
    It’s pretty worth enough for me. In my view, if all webmasters and bloggers made good
    content as you probably did, the internet shall be a lot more useful than ever
    before.

  15. #15 g know 回复 | 引用 Post:2020-06-11 17:06

    It’s really a great and useful piece of information. I’m glad that you
    simply shared this helpful information with us.
    Please keep us up to date like this. Thank you for sharing.

  16. #16 hack afk arena cheats 回复 | 引用 Post:2020-06-13 16:57

    Ahaa, its pleasant discussion about this article at this place at this blog, I have read all that,
    so now me also commenting here.

  17. Howdy! Someone in my Myspace group shared this website with us so I
    came to check it out. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers!
    Great blog and wonderful design.

  18. It’s going to be ending of mine day, except before end I am reading this great article to improve my know-how.

  19. #19 ks.jiali.tw 回复 | 引用 Post:2020-06-14 13:41

    Wow, that’s what I was searching for, what a stuff!
    existing here at this website, thanks admin of this website.

  20. #20 g 回复 | 引用 Post:2020-06-15 12:41

    As the admin of this site is working, no uncertainty very quickly it will be renowned, due to its feature contents.

发表评论