TCP中的send()函数是否保证按顺序到达?(Is send() function in TCP Guaranteed to arrive in order?)

编程入门 行业动态 更新时间:2024-10-10 19:24:56
TCP中的send()函数是否保证按顺序到达?(Is send() function in TCP Guaranteed to arrive in order?)

众所周知,send()的返回值可能小于length,这意味着消息的一部分,而不是整个消息已经到达。 我应该发送2个内容分别为“ABC”和“DEF”的数据包,它们的长度为3.我希望在send()被调用以传输“ABC”之后通过send()发送“DEF”。 但是,有一种情况是“ABC”的send()的返回值小于其长度,3。我认为有可能无法按顺序传递消息。 例如,如果“ABC”的返回值为2,则收到的消息为“ABDEF”。

TCP中的send()函数是否保证按顺序到达?

It is known that the return value of send() can be less than length, and it means a part of message, not whole, has arrived. I'm supposed to send 2 packets whose contents are "ABC" and "DEF" respectively, and their length is 3. I want to send "DEF" by send() after send() was called to transfer "ABC". However, there is a case that the return value of send() for "ABC" is less than its length, 3. I think there is opportunity that messages are not delivered in order. For example, if the return value for "ABC" is 2, received message is "ABDEF".

Is send() function in TCP Guaranteed to arrive in order?

最满意答案

首先,send()本身不保证任何东西,send()只将你想通过网络发送的数据写入套接字的缓冲区。 它由操作系统分段(放置在TCP段中),管理传输的可靠性。 如果底层缓冲区已满,那么您将获得一个低于您想要写入的字节数的返回值。 这通常表示操作系统没有快速清空缓冲区,即将数据写入缓冲区的速率高于数据发送到网络的速率(或由另一方读取的速率) )。

其次,TCP是一种流协议,如果你发送()“ABC”然后发送“DEF”,则不能保证数据如何被分段,它可能最终在一个数据包中,或者在六个数据包中。 就像将数据写入文件一样。

第三,网络堆栈(OS中的TCP / IP实现)保证了按顺序交付,以及TCP承诺的其他好处 - 可靠性,拥塞控制,流量控制等。

First of all, send() itself doesn't guarantee anything, send() only writes the data you want to send over the network to the socket's buffer. There it's segmented (placed in TCP segments) by the operating system, which manages the reliability of the transmission. If the underlying buffer is full, then you'll get a return value that's lower that the number of bytes you wanted to write. This usually indicates that the buffer is not being emptied by the operating system fast enough, i.e. the rate at which you write data to the buffer is higher than the rate at which the data is being sent to the network (or read by the other party).

Second, TCP is a stream protocol, if you send() "ABC" and then "DEF", there's no guarantee about how that data will be segmented, it might end up in one packet, or in six packets. Exactly like writing data to a file.

Third, the network stack (TCP/IP implementation in the OS) guarantees in-order delivery, as well as the other nice things promised by TCP - reliability, congestion control, flow control, etc.

更多推荐

本文发布于:2023-08-08 01:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   顺序   send   TCP   order

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!