C#:如何处理乱序的 TCP 数据包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/762779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: How to deal with out-of-order TCP packets?
提问by
Can please someone one explain how to deal with out-of-order packets. I'm using raw socket to capture packets, and parse them as they come, but some of them come in wrong order, for example:
可以请人解释如何处理乱序数据包。我正在使用原始套接字来捕获数据包,并在它们到来时解析它们,但其中一些顺序错误,例如:
- Id...........Flags
- 16390 : (PSH, ACK)
- 16535 : (PSH, ACK)
- 16638 : (ACK)
- 16640 : (PSH, ACK)
- 16639 : (ACK)
- 16695 : (PSH, ACK)
- Id…………标志
- 16390:(PSH,确认)
- 16535 : (PSH, ACK)
- 16638:(确认)
- 16640 : (PSH, ACK)
- 16639:(确认)
- 16695 : (PSH, ACK)
Packets with IDs: 16390, 16535, 16695 are separate packets and can be processed freely Packets with IDs: 16638, 16640, 16639 are a sequence of packets and should be put in ascending order before parsing.
ID:16390、16535、16695的数据包是独立的数据包,可以自由处理 ID:16638、16640、16639的数据包是一个数据包序列,在解析之前应该按升序排列。
To make it worse packets with Push flag sometimes come first so I just pass them along to parser, and then packet that preceds it comes and parser just discards it as corrupted.
更糟糕的是,带有 Push 标志的数据包有时首先出现,所以我只是将它们传递给解析器,然后在它之前的数据包出现,解析器只是将其丢弃为损坏的。
Is there any way to deal with it?
有什么办法可以处理吗?
采纳答案by Unknown
TCP guarantees order. So I will just assume you are talking about IP.
TCP保证顺序。所以我假设你在谈论IP。
One thing you could try is putting the packets in a min-heap and then waiting until the next packet ID number you want is available.
您可以尝试的一件事是将数据包放入最小堆中,然后等待您想要的下一个数据包 ID 号可用。
As for the push packets, those are supposed to be received as soon as possible without a restriction on ordering, so its up to you to decide how long you want to wait to see if you'll receive an earlier push packet.
至于推送数据包,应该是尽快收到,没有顺序限制,所以由您决定要等待多长时间,看看是否会收到更早的推送数据包。
回答by Joe Phillips
TCP segments will not be out of order because the next one will not be sent until you ACK the previous one.
TCP 段不会乱序,因为在您确认前一个段之前不会发送下一个段。
TCP numbers the segments that it sends to a particular destination port sequentially, so that if they arrive out of order, the TCP entity can reorder them.
TCP 对它发送到特定目标端口的数据段按顺序编号,以便如果它们无序到达,TCP 实体可以对它们重新排序。
This happens on a transport layer below TCP so any TCP connections would never "see" this happen. In terms of TCP they are always in order. So if you see them out of order then you are not working on the TCP transport layer, you're at a lower level.
这发生在 TCP 下方的传输层上,因此任何 TCP 连接都不会“看到”这种情况发生。就 TCP 而言,它们总是有序的。因此,如果您看到它们乱序,那么您就没有在 TCP 传输层上工作,您处于较低级别。
Also, FYI...
另外,仅供参考...
- TCP data is a "segment"
- IP data is a "datagram"
- Network-level is a "packet"
- TCP数据是一个“段”
- IP数据是一个“数据报”
- 网络级是一个“包”
Edit:The link you provided will provide you with a stream of IP datagrams so you would have to handle the TCP stream on your own. I'm not going to pretend like it's easy and try to explain that here.
编辑:您提供的链接将为您提供 IP 数据报流,因此您必须自己处理 TCP 流。我不会假装这很容易并试图在这里解释。
回答by justinhj
Why don't you use the normal tcp socket so they come in order?
为什么不使用普通的 tcp 套接字以便它们按顺序排列?
回答by Fred Phillips
The Sequence number will show the order. It wraps at 4G so you will have to account for that. And you will have to use the same basic routine that TCP uses. Buffer out of order packets and discard duplicates. Sequence Number + Len of payload is the nexxt sequence number.
序列号将显示顺序。它包含在 4G 中,因此您必须考虑到这一点。并且您必须使用 TCP 使用的相同基本例程。缓冲乱序数据包并丢弃重复数据。序列号+有效载荷的Len是下一个序列号。
TCP/IP Illistrated vol 2 or TCP.c ?
TCP/IP Illistrated vol 2 还是 TCP.c ?