[ntp:questions] Re: Question about synchronizing client and server clock

Danny Mayer mayer at gis.net
Mon Jan 5 14:15:09 UTC 2004


Atri Mandal <mandala at ics.uci.edu> wrote in message news:<3FF8F54B.31B9AF5F at ics.uci.edu>...
> Hi ,
> I am writing a client server code using WINSOCK 2.0 in which I have to
> calculate the exact time required by a piece of message to go from the
> client to the server or vice versa.
> i.e Suppose I have a client server model in which A is the client and B
> is the server.
> Let's say A has to send a message M to B by socket communication. I want
> the exact time taken by M to go from A to B.
> Currently I'm sending the message from A to B and then sending an
> acknowledgement from B to A(ack having the same size as original
> message) and then calculating the total time T taken for the round
> trip(this is easy because I just need  2 timer function calls
> within the client). Then I'm assuming that the time taken for A-B is
> T/2. This is however not precise because it might take a longer time to
> go from A-B rather than B-A.
> So is there any way I can calculate the exact one way communication time
> (with the precision at microsecond level or better) ?
> I think the best way is to use a synchronized clock for the client and
> server - Can anyone in this group please tell me how to implement this
> or give me pointers to example source code. I am quite new to this
> field.
> 
> Thanks,
> Atri.
> 
> --

Well this is exactly what NTP does. If you want to do timing experiments
like this, set up ntp on both client and server, disabling Windows time
service which would otherwise muck up the time as well. This will
synchronize the time on both systems. If you point both to the same
servers you will likely get both systems to agree on the time. Now you
can write your code to send and receive messages on the sockets to
calculate the time. If you do it right, you will include a timestamp
of the sending system with it's current time in the message, and the
server will return that along with its own timestamp. Now when you get
it back you add a third timestamp and you can save and analyze the time
taken in each direction. You will need to do this as many times as necessary
to get a good statistical average (there is no such thing as an exact time)
to smooth out activity on the network between the two systems.

If you are using Windows, I might suggest looking at two functions
SystemTimeToFileTime() and FileTimeToSystemTime(). Do your calculations
using FileTime which provides 64-bit integers though you have to copy
it to a ULARGE_INTEGER structure to do that. If you want to use ntp code
itself there is are two undocumented Win32 API functions in the IPHLPAPI DLL
called NTPTimeToNTFileTime() and NTTimeToNTPTime(). Unfortunately I don't
know the arguments to the calls.

Danny



More information about the questions mailing list