TCP数据包有时会合并(并延迟?)

时间:2018-01-30 16:53:46

标签: c++ c sockets networking tcp

我每500毫秒从服务器向客户端发送数据包,反之亦然,表明连接仍然存在。然而,有时这些分组被延迟并且多个分组被组合在一起。问题是这会导致连接超时"什么时候可能只是一个延迟。

在任何现代互联网连接上,对我来说,数据包发送延迟10秒似乎很长。关于如何解决这个问题的任何想法?

在服务器和客户端上都发送和接收消息,如下例所示,其中最终接收的数据包由多个数据包组合而成。这同时发生在服务器和客户端上。

SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
SENT ALIVE PACKET
RECEIVED ALIVE PACKET RECEIVED ALIVE PACKET RECEIVED ALIVE
PACKET RECEIVED ALIVE PACKET RECEIVED ALIVE PACKET RECEIVED ALIVE
PACKET RECEIVED ALIVE PACKET RECEIVED ALIVE PACKET RECEIVED ALIVE
PACKET RECEIVED ALIVE PACKET ****RECEIVED TIMEOUT PACKET****

Connection Timeout.


修改

在我运行相同代码的其他PC上从未发生这种情况。

修改2

数据包的组合不是问题,但数据包应该直接发送而不是延迟。

1 个答案:

答案 0 :(得分:-1)

KEEPALIVE选项添加到我的套接字似乎解决了这个问题。

感谢@cdarke。