What's a good way to detect wrap-around in a fixed-width message counter?

Posted by Kristo on Stack Overflow See other posts from Stack Overflow or by Kristo
Published on 2010-04-07T17:04:52Z Indexed on 2010/04/07 17:43 UTC
Read the original article Hit count: 225

Filed under:
|
|
|
|

I'm writing a client application to communicate with a server program via UDP. The client periodically makes requests for data and needs to use the most recent server response. The request message has a 16-bit unsigned counter field that is echoed by the server so I can pair requests with server responses.

Since it's UDP, I have to handle the case where server responses arrive out of order (or don't arrive at all). Naively, that means holding on to the highest message counter seen so far and dropping any incoming message with a lower number. But that will fail as soon as we pass 65535 messages and the counter wraps back to zero. Is there a good way to detect (with reasonable probability) that, for example, message 5 actually comes after message 65,000?

The implementation language is C++.

© Stack Overflow or respective owner

Related posts about udp

Related posts about overflow