0

I'm working on an in-house TCP Server, and TCP client. When there is 0% packet loss the Server and Client work fine. However, when I have 20% or more packet loss I am seeing duplicate TCP messages. I am receiving something like this....

Client <-- MessageA -- Server
Client -- MessageB --> Server
Client <-- MessageCMessageA -- Server

Is it possible that MessageA is not completely making it to the Client, it times outs, then TCP resends it, and then the original message makes it which is received at a later time by the Client?

My question is if TCP works like that, and if that's a possible scenario with a network containing 20% packet loss or more.

Barebones of how the client and server are sending/receiving data...

socket.recv(1024)
socket.send(1024)
3
  • 1
    Assuming that you are using the built in TCP/IP library and assuming that all of the pieces eventually arrive before the connection times out, TCP provides the guarantee that you do not need to worry about duplicates and retransmissions. It's one of the main reasons you would choose TCP over UDP... That and the guarantee that the data is properly ordered. Commented Apr 27, 2016 at 21:15
  • Ok, thank you. I just wanted to make sure before I started looking for a bug. Commented Apr 27, 2016 at 21:34
  • Perhaps you assumed that your recv call will always return exactly 1,024 bytes? The buffer size is the maximum number of bytes that will be received, not the actual number. Show us your complete code to receive a message. Commented Apr 28, 2016 at 1:39

1 Answer 1

1

No, it is not possible. TCP guarantees that it will either deliver data exactly once and in the order in which it was sent, or signal an error to the application. Hence, there is probably a bug in your code. The most likely is that your code fails to deal with partial reads.

When you perform a write or send on a TCP socket, the TCP module will segment your data into as many packets as are needed. In the presence of packet loss, it is possible that some packets have arrived successfully but others must be resent. In that case, the corresponding read or recv will only receive part of the data — the remaining data will arrive in a subsequent read or recv. In other words, TCP does not preserve message boundaries.

Your code is probably interpreting such a split message as multiple messages. Make sure that you accumulate a full message in your buffer before attempting to parse it.

Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.