简体   繁体   English

如何在Java中的TCP套接字中检测重传的数据包以将其丢弃

[英]How to detect retransmited packets to discard them in TCP Sockets in java

I sometimes receive already received packets (I used sniffer and system ACKs them). 有时我会收到已经收到的数据包(我使用了嗅探器并对其进行了系统确认)。 Now I read all data (until socket timeout) and then send new request, but this is ugly. 现在,我读取了所有数据(直到套接字超时),然后发送新请求,但这很丑陋。 I was thinking about using sequence numbers but i didn't find it in Socket interface. 我当时正在考虑使用序列号,但我在Socket接口中找不到它。 Any clues? 有什么线索吗?

No you don't. 不,你不会。 If the receiving TCP stack misses a packet, it will re-request it, but it can't have delivered the original one to you, because it missed it. 如果接收方TCP堆栈丢失了一个数据包,它将重新请求该数据包,但由于它丢失了原始数据包,因此它无法将原始数据包交付给您。 And if it gets a packet it has already received, it will drop it. 如果收到已经接收到的数据包,它将丢弃它。

TCP will deliver all the bytes that are sent, in the order they are sent. TCP将按发送顺序传递所有发送的字节。 Nothing else (well, except some edge cases around disconnects). 没别的(好吧,除了一些断开连接的边缘情况)。

Something else is going on. 发生了其他事情。

EDIT: 编辑:

To be clear, I'm talking about the bytes that are delivered to your application through the socket's InputStream. 明确地说,我说的是通过套接字的InputStream传递给您的应用程序的字节。 What happens on the wire is largely irrelevant unless you have some horrific network retransmission problem that you're trying to investigate. 除非您要研究一些可怕的网络重传问题,否则网络上发生的一切基本上无关紧要。 And if the receiving stack does get a duplicate packet, it will ACK it, because if it didn't then the sender would re-send it... again. 如果接收堆栈确实收到了重复的数据包,它将对其进行确认,因为如果没有,那么发送方将再次发送它。

It sounds like you're trying to account for things that TCP already takes care of. 听起来您正在尝试说明TCP已经解决的问题。 It has sequence numbers built in and will take care of any lost data for you, and from the receiving point you should be waiting until you receive all your expected data, rather than reissuing a request. 它内置了序列号,将为您处理所有丢失的数据,从接收点开始,您应该等待直到收到所有期望的数据,而不是重新发出请求。 If you don't want to wait for a response to complete before issuing a new request, consider pipe-lining requests with multiple connections. 如果您不想在发出新请求之前等待响应完成,请考虑对具有多个连接的请求进行流水线处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM