Coffeehouse Thread

16 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Anyone has experience with Network Time Protocol?

Back to Forum: Coffeehouse
  • User profile image
    TommyCarlier

    I'm writing code that implements the Network Time Protocol to connect to a time server and calculate the difference between the local system clock and the remote server clock. My test code connects to 3 different time servers. The first time I tested, the difference was 2 minutes. After synchronizing my system clock with 1 of the servers, the next test ran like I expected: a tiny difference (of a few milliseconds). But as I run more tests (using the same code), the difference is increasing. Half an hour after I synchronized, the difference is already 4 seconds!

    I can't believe my local system clock runs that much slower (or faster), so there must be something wrong with my code. Anyone got experience with implementing/using the Network Time Protocol?

  • User profile image
    Maddus Mattus

    Are you developing in a VPC? The VPC clock tends not to run in sync with the host machine.

  • User profile image
    TommyCarlier

    Maddus Mattus said:
    Are you developing in a VPC? The VPC clock tends not to run in sync with the host machine.

    No, regular Windows XP.

    It appears that my code is correct and that the problem is with my machine. If I run the same code on a different PC, the difference stays very small (tens of milliseconds). This means that the system clock of my PC loses 2 to 3 minutes each day!

  • User profile image
    Pace

    TommyCarlier said:
    Maddus Mattus said:
    *snip*

    No, regular Windows XP.

    It appears that my code is correct and that the problem is with my machine. If I run the same code on a different PC, the difference stays very small (tens of milliseconds). This means that the system clock of my PC loses 2 to 3 minutes each day!

    But you would notice that surely right? After a week your clock would be 14 - 21 mins out.

    Id check your CMOS battery Wink

  • User profile image
    Maddus Mattus

    TommyCarlier said:
    Maddus Mattus said:
    *snip*

    No, regular Windows XP.

    It appears that my code is correct and that the problem is with my machine. If I run the same code on a different PC, the difference stays very small (tens of milliseconds). This means that the system clock of my PC loses 2 to 3 minutes each day!

    It's always the cue, the balls or the table's fault!

    Shameless english translation of a dutch saying

  • User profile image
    TommyCarlier

    Pace said:
    TommyCarlier said:
    *snip*
    But you would notice that surely right? After a week your clock would be 14 - 21 mins out.

    Id check your CMOS battery Wink
    Maybe my boss sabotaged my PC to squeeze extra working time out of me :-O

  • User profile image
    Maddus Mattus

    TommyCarlier said:
    Pace said:
    *snip*
    Maybe my boss sabotaged my PC to squeeze extra working time out of me :-O
    maybe he sped up your clock to get rid of you ten minutes early each day Big Smile Big Smile Big Smile Big Smile Big Smile

  • User profile image
    AndyC

    When you say "synchronizing my system clock with 1 of the servers", do you actually mean you set it up to synchronize with that server or just that you set the time to be the same?

    If you just set the time the same and the server in question is not in synch with time.windows.com (or whichever NTP server Windows is using), Windows will slowly adjust the clock to bring it back in line with it's time server, which might account for the difference.

  • User profile image
    TommyCarlier

    AndyC said:
    When you say "synchronizing my system clock with 1 of the servers", do you actually mean you set it up to synchronize with that server or just that you set the time to be the same?

    If you just set the time the same and the server in question is not in synch with time.windows.com (or whichever NTP server Windows is using), Windows will slowly adjust the clock to bring it back in line with it's time server, which might account for the difference.
    No, I let Windows synchronize with the server via the Date and Time Properties dialog. And the server I use is one of the servers my code uses.

  • User profile image
    TommyCarlier

    Fun fact: when you ask a time server for a timestamp, it also returns the IP-address of the actual machine that was used. When I connect to time.windows.com, I sometimes get a timestamp generated by clock1.redhat.com.

  • User profile image
    Minh

    TommyCarlier said:
    Fun fact: when you ask a time server for a timestamp, it also returns the IP-address of the actual machine that was used. When I connect to time.windows.com, I sometimes get a timestamp generated by clock1.redhat.com.
    I had a motherboard that gained 10 minutes every day. The solution was to update the clock automatically every hour. So, yeah, bad clocks do happen.

    Re: the time protocol, I'd always wonder how they cancel out the time it takes for the time server's response to reach your computer.

  • User profile image
    figuerres

    I do not have the code for this but I have used the programs from this site for a very long time:
    http://www.kaska.demon.co.uk/

    it works, and the cost is almost nothing, for one pc $12 us, for a bunch it gets cheaper....

    they have ways of keeping any clock on time, they do a bunch of stuff to make it work right.


    PS:  Minh,  the time gets figured out by data that goes with the packets, I have not messed with it but it's much like how ping figures the trip-time for a ping...
    then you can adjust the data you get by the offset of the 1/2 of the trip time, and you do the time query more than one time to filter out network or server "burps" of odd triptimes, also you can check multiple servers and see how many of them give you the 'same time' within a small window of say a tenth of a second for example.  if you ask 6 for the time and 5 agree on the same time then you ignore the odd one.
    bunch of tests like that...

  • User profile image
    TommyCarlier

    Minh said:
    TommyCarlier said:
    *snip*
    I had a motherboard that gained 10 minutes every day. The solution was to update the clock automatically every hour. So, yeah, bad clocks do happen.

    Re: the time protocol, I'd always wonder how they cancel out the time it takes for the time server's response to reach your computer.

    T1 = time that the client sends to the server (client clock)
    T2 = time that the server receives the request (server clock)
    T3 = time that the server sends the reply (server clock)
    T4 = time that the client receives the reply (client clock)

    system clock offset = ((T2 - T1) + (T3 - T4)) / 2

  • User profile image
    Minh

    TommyCarlier said:
    Minh said:
    *snip*

    T1 = time that the client sends to the server (client clock)
    T2 = time that the server receives the request (server clock)
    T3 = time that the server sends the reply (server clock)
    T4 = time that the client receives the reply (client clock)

    system clock offset = ((T2 - T1) + (T3 - T4)) / 2

    Shouldn't it just be

    (T4 - T1) / 2 ... which is only an average...

    Because (T2 - T1) has no context -- they are not synchronized.

    Neither does T4 - T3

    Unless I'm missing something

    Unless, like what figuerres implied, the timings are tracked in the packets layer itself.

  • User profile image
    figuerres

    Minh said:
    TommyCarlier said:
    *snip*
    Shouldn't it just be

    (T4 - T1) / 2 ... which is only an average...

    Because (T2 - T1) has no context -- they are not synchronized.

    Neither does T4 - T3

    Unless I'm missing something

    Unless, like what figuerres implied, the timings are tracked in the packets layer itself.
    well if you want the details here is the RFC:   http://tools.ietf.org/html/rfc1305

    and to serve as a starter here is the first  one for ntp : http://tools.ietf.org/html/rfc778

  • User profile image
    TommyCarlier

    Minh said:
    TommyCarlier said:
    *snip*
    Shouldn't it just be

    (T4 - T1) / 2 ... which is only an average...

    Because (T2 - T1) has no context -- they are not synchronized.

    Neither does T4 - T3

    Unless I'm missing something

    Unless, like what figuerres implied, the timings are tracked in the packets layer itself.

    T1 is sent in the request from the client to the server. T1, T2 and T3 are sent in the reply from the server to the client.

    You can check out RFC1769 which explains SNTP (Simple Network Time Protocol). On page 8 and 9, you can find the chapter SNTP Client Operations where you can find the formula to calculate the local clock offset and the roundtrip delay ( (T4 - T1) - (T2 - T3) ).

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.