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.
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 Crittenreceive
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