Taztingo
Taztingo

Reputation: 1945

Python TCP Duplicate Message

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)

Upvotes: 0

Views: 1898

Answers (1)

jch
jch

Reputation: 5651

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.

Upvotes: 1

Related Questions