2

I am developing SNTP client which gets time stamp from NTP server via UDP. Everything works fine, I can download time stamp and update windows clock, but I'd like to get a message if UDP packet has been lost. How can I detect when packet has been lost? I need that information, because I want to get new time stamp if data lost.

... and how can I test that feature? How can I lost UDP packet purposely?

user1528100
  • 49
  • 2
  • 5
  • 5
    UDP is one-way, you can't detect that a packet has been lost. That's the main difference between TCP and UDP. – Kevin Gosse Aug 28 '12 at 10:59
  • Exactly how does this service work? Does your client make a request to the server and wait for a response, after which it updates it's time? Or does your client wait and listen for UDP packets from the server? Either way, you can't actually verify the transmission of UDP packets. Depending on your needs, setting a timeout may perhaps be a better option for you? Show a warning or throw an exception or whatever, if the time has not been updated within the last X seconds, or something like that? – Kjartan Aug 28 '12 at 11:09

5 Answers5

14

You have three options:

  1. Ignore dropped packets
  2. Use TCP instead
  3. Build your own detection system

What you're asking is how to do #3 and the answer is:

  • Add a packet number and a packet timestamp to every packet.
  • Create a packet stack and insert incoming packets into the stack sorted by packet number.
  • Remove packets from the stack only when there is a packet with a packet number of (last packet number + 1).
  • If there is a gap in packets and the timestamp of the (gap + 1) packet is greater than some threshold, send a "re-transmit request" packet to get the dropped packet re-transmitted.

Hold on a second.. that's exactly how TCP does it(*)! You should just use TCP!

(*) That was a simplification, TCP does a great deal of work to make the process reliable.

Tergiver
  • 14,171
  • 3
  • 41
  • 68
9

That's not possible with UDP. If you need a reliable connection you should use TCP.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • +1 despite you can still implement connections with sequence numbers, acknowledges and retransmissions over UDP. Although it's pointless, I've had to do it as a lab assignment (Many hours and tests to do it almost as good as TCP does, a real pain in the a***). – NotGaeL Aug 28 '12 at 11:13
1

UDP is a connectionless protocol. When there is no connection, you have no way to "detect" anything. You have to use a connection oriented protocol such as TCP in order to resolve this.

Comparison

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
0

ok. I got it. But can I check packet loss by doing something like this? For example if I set timer to 2 seconds, and then run this function and I can see if server responded correctly. Then I should know if there was a packet loss?

public bool IsResponseValid()
    {
        if(SNTPData.Length < SNTPDataLength || Mode != _Mode.Server)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
user1528100
  • 49
  • 2
  • 5
  • Couldn't resist the urge to optimize your code: return SNTPData.Length >= SNTPDataLength && Mode == _Mode.Server; – rmp251 May 21 '14 at 15:14
0

Because there is no way to test if UDP failed to arrive, the only true option would be to prefix all data sent with a "packet number" and so if its not the next number in line, you know it lost a packet..

As far as I know, there is no way to deliberately lose 1 udp packet, other than to yank the cable and replace it.. and hope..

BugFinder
  • 17,474
  • 4
  • 36
  • 51