TCP保证按顺序到达吗?

时间:2021-12-10 20:58:43

If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-specific answer for my own peace of mind, that'd be appreciated :-)

如果我发送两条TCP消息,我是否需要处理后者到达之前的情况?还是保证按我发送的顺序到达?我假设这不是Twisted特定的例子,因为它应该符合TCP标准,但是如果熟悉Twisted的人可以为我自己的安心提供Twisted特定的答案,那将不胜感激:-)

4 个解决方案

#1


47  

As long as the two messages were sent on the same TCP connection, order will be maintained. If multiple connections are opened between the same pair of processes, you may be in trouble.

只要两条消息在同一TCP连接上发送,就会保持订单。如果在同一对进程之间打开了多个连接,则可能会遇到麻烦。

Regarding Twisted, or any other asynchronous event system: I expect you'll get the dataReceived messages in the order that bytes are received. However, if you start pushing work off onto deferred calls, you can, erm... "twist" your control flow beyond recognition.

关于Twisted或任何其他异步事件系统:我希望您将按接收字节的顺序获取dataReceived消息。但是,如果你开始将工作推迟到延期通话中,你可以,呃......“扭曲”你的控制流程而无法识别。

#2


21  

TCP is connection-oriented and offers its Clients in-order delivery. Of course this applies to the connection level: individual connections are independent.

TCP是面向连接的,并为其客户提供按顺序交付。当然这适用于连接级别:各个连接是独立的。

You should note that normally we refer to "TCP streams" and "UDP messages".

您应该注意,通常我们会引用“TCP流”和“UDP消息”。

Whatever Client library you use (e.g. Twisted), the underlying TCP connection is independent of it. TCP will deliver the "protocol messages" in order to your client. By "protocol message" I refer of course to the protocol you use on the TCP layer.

无论您使用哪种客户端库(例如Twisted),底层TCP连接都与其无关。 TCP将为您的客户提供“协议消息”。通过“协议消息”,我当然指的是您在TCP层上使用的协议。

Further note that I/O operation are async in nature and very dependent on system load + also compounding network delays & losses, you cannot rely on message ordering between TCP connections.

进一步注意,I / O操作本质上是异步的,并且非常依赖于系统负载+复合网络延迟和丢失,您不能依赖TCP连接之间的消息排序。

#3


14  

TCP "guarantees" that a receiver will receive the reconstituted stream of bytes as it was originally sent by the sender. However, between the TCP send/receive endpoints (i.e., the physical network), the data can be received out of order, it can be fragmented, it can be corrupted, and it can even be lost. TCP accounts for these problems using a handshake mechanism that causes bad packets to be retransmitted. The TCP stack on the receiver places these packets in the order in which they were transmitted so that when you read from your TCP socket, you are receive the data as it was originally sent.

TCP“保证”接收器将接收由发送者最初发送的重构字节流。但是,在TCP发送/接收端点(即物理网络)之间,数据可能无序接收,可能被分段,可能被破坏,甚至可能丢失。 TCP使用握手机制解决了这些问题,这种机制导致重传错误的数据包。接收器上的TCP堆栈按发送顺序放置这些数据包,这样当您从TCP套接字读取时,您将收到最初发送的数据。

When you call the doRead method in Twisted, the data is read from the socket up to the size of the buffer. This data may represent a single message, a partial message, or multiple messages. It is up to you to extract the messages from the buffer, but you are guaranteed that the bytes are in their transmitted order at this point.

在Twisted中调用doRead方法时,将从套接字读取数据,直至达到缓冲区的大小。该数据可以表示单个消息,部分消息或多个消息。您可以从缓冲区中提取消息,但是您可以确保此时字节按其传输顺序排列。

Sorry for muddying the waters with my earlier post...

对不起,我早先的帖子弄乱了水域......

#4


7  

TCP is a stream, UDP is a message. You're mixing up terms. For TCP it is true that the stream will arrive in the same order as it was send. There are no distict messages in TCP, bytes appear as they arrive, interpreting them as messages is up to you.

TCP是一个流,UDP是一个消息。你混淆了条款。对于TCP,流确实以与发送时相同的顺序到达。 TCP中没有distict消息,字节在到达时出现,将消息解释为消息取决于您。

#1


47  

As long as the two messages were sent on the same TCP connection, order will be maintained. If multiple connections are opened between the same pair of processes, you may be in trouble.

只要两条消息在同一TCP连接上发送,就会保持订单。如果在同一对进程之间打开了多个连接,则可能会遇到麻烦。

Regarding Twisted, or any other asynchronous event system: I expect you'll get the dataReceived messages in the order that bytes are received. However, if you start pushing work off onto deferred calls, you can, erm... "twist" your control flow beyond recognition.

关于Twisted或任何其他异步事件系统:我希望您将按接收字节的顺序获取dataReceived消息。但是,如果你开始将工作推迟到延期通话中,你可以,呃......“扭曲”你的控制流程而无法识别。

#2


21  

TCP is connection-oriented and offers its Clients in-order delivery. Of course this applies to the connection level: individual connections are independent.

TCP是面向连接的,并为其客户提供按顺序交付。当然这适用于连接级别:各个连接是独立的。

You should note that normally we refer to "TCP streams" and "UDP messages".

您应该注意,通常我们会引用“TCP流”和“UDP消息”。

Whatever Client library you use (e.g. Twisted), the underlying TCP connection is independent of it. TCP will deliver the "protocol messages" in order to your client. By "protocol message" I refer of course to the protocol you use on the TCP layer.

无论您使用哪种客户端库(例如Twisted),底层TCP连接都与其无关。 TCP将为您的客户提供“协议消息”。通过“协议消息”,我当然指的是您在TCP层上使用的协议。

Further note that I/O operation are async in nature and very dependent on system load + also compounding network delays & losses, you cannot rely on message ordering between TCP connections.

进一步注意,I / O操作本质上是异步的,并且非常依赖于系统负载+复合网络延迟和丢失,您不能依赖TCP连接之间的消息排序。

#3


14  

TCP "guarantees" that a receiver will receive the reconstituted stream of bytes as it was originally sent by the sender. However, between the TCP send/receive endpoints (i.e., the physical network), the data can be received out of order, it can be fragmented, it can be corrupted, and it can even be lost. TCP accounts for these problems using a handshake mechanism that causes bad packets to be retransmitted. The TCP stack on the receiver places these packets in the order in which they were transmitted so that when you read from your TCP socket, you are receive the data as it was originally sent.

TCP“保证”接收器将接收由发送者最初发送的重构字节流。但是,在TCP发送/接收端点(即物理网络)之间,数据可能无序接收,可能被分段,可能被破坏,甚至可能丢失。 TCP使用握手机制解决了这些问题,这种机制导致重传错误的数据包。接收器上的TCP堆栈按发送顺序放置这些数据包,这样当您从TCP套接字读取时,您将收到最初发送的数据。

When you call the doRead method in Twisted, the data is read from the socket up to the size of the buffer. This data may represent a single message, a partial message, or multiple messages. It is up to you to extract the messages from the buffer, but you are guaranteed that the bytes are in their transmitted order at this point.

在Twisted中调用doRead方法时,将从套接字读取数据,直至达到缓冲区的大小。该数据可以表示单个消息,部分消息或多个消息。您可以从缓冲区中提取消息,但是您可以确保此时字节按其传输顺序排列。

Sorry for muddying the waters with my earlier post...

对不起,我早先的帖子弄乱了水域......

#4


7  

TCP is a stream, UDP is a message. You're mixing up terms. For TCP it is true that the stream will arrive in the same order as it was send. There are no distict messages in TCP, bytes appear as they arrive, interpreting them as messages is up to you.

TCP是一个流,UDP是一个消息。你混淆了条款。对于TCP,流确实以与发送时相同的顺序到达。 TCP中没有distict消息,字节在到达时出现,将消息解释为消息取决于您。