0
votes

I am sending packets from server to client and vice versa every 500ms to indicate that the connection is still alive. However, sometimes these packets are delayed and mutliple packets are combined together. The problem is that this would cause a "connection timeout" when it might simply be a delay.

A 10 second delay of packet sending seems quite long to me on any modern Internet connection. Any ideas on how to resolve this?

On both server and client messages are sent and received like the following example where the final received packet consists of several packets combined. This happens on both server and client simultaneously.

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.


Edit

This has never happened on my other PC running the same code.

Edit 2

The combining of packets is not a problem, but packets should be sent directly and not delayed.

1
Are you using KEEPALIVE? Are you setting TCP_NODELAY? See setsockopt().cdarke
Possible duplicate of Packets sometimes get concatenatedXirema
TCP/IP at the level of send/receive is a stream not a packet protocol. So you get a continuous stream of bytes when reading it. At the lower layers it can merge / split however it feels like and you will read part/multiple "messages" you have sent. If you have blocks of data you conciser messages you need to implement a messaging format; length prefix is one of the simplest.Richard Critten
@Z0q As TCP/IP hides all of the packet details from you, you can't receive "packets". When you call receive it will pass you however much is in it's buffer. This can cause sent messages to be split or joined depending on the transport layer.Richard Critten
I have an additional question: Does it really make sense to ckeck periodically that a TCP connection is still existing? Such a mechanism is already included in TCP itself.MarcusS

1 Answers

-1
votes

Adding the KEEPALIVE option to my sockets seemed to have resolved this issue.

Thanks to @cdarke.