getting time from a time server

Started by
4 comments, last by hplus0603 12 years, 6 months ago
Hi, my CMOS battery socket is damaged and so, my PC won't show the correct time every time I start it up (it will always show 01/01/2002 00:00 AM which I think is the time and date the motherboard created). I found it quite annoying to have to set the time to correct value every time I boot my PC, and I came up with an idea to make a small app to get time from a time server and set the system time accordingly. thing is, I forgot how to do that. I remember ever creating such a program when I was learning networking (Winsock) for the first time. IIRC, it used UDP socket, I forgot the port number, and I forgot the time server address. So in my humble confusion I beg you, if anyone can, to give hints on how it can be done.

to sum it up, what I need is a guide to:
-how to get time from the time server, and the requirements (what message to send, etc)

Thanks :D
the hardest part is the beginning...
Advertisement

... thing is, I forgot how to do that. I remember ever creating such a program when I was learning networking (Winsock) for the first time. IIRC, it used UDP socket, I forgot the port number, and I forgot the time server address. So in my humble confusion I beg you, if anyone can, to give hints...


Maybe you're referring to NTP? http://www.nist.gov/pml/div688/grp40/its.cfm

If so, googling a few terms from there should lead you to numerous examples and information - more than we can suggest here :)
I solved it. I use the simplistic TIME protocol by:
-connect()-ing to time server on port 37
-recv() 4 bytes and copy it to 32 bit unsigned integer and translate the byte order using ntohl()
-since the returned value is seconds since 1 jan 1900, convert it to suits my need and I'm done

I hope it can be helpful for those who seek similar information, thanks and see ya :wink:
the hardest part is the beginning...
For the record, any recent version of Windows can be configured to sync clocks to an NTP server automatically. How exactly you do it depends on your particular setup, but try right clicking on the clock in the notification area ;-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

@ApochPiQ, the built-in time synchronization in windows only works if the date/time isn't too much different. It doesn't work if my PC date is 9 years left behind (which will always be since the CMOS battery socket's blasted)...that's why I needed to create my own tool for that :)
the hardest part is the beginning...

@ApochPiQ, the built-in time synchronization in windows only works if the date/time isn't too much different. It doesn't work if my PC date is 9 years left behind (which will always be since the CMOS battery socket's blasted)...that's why I needed to create my own tool for that :)


Cygwin comes with ntpdate. You can add this to a startup script:

ntpdate -q pool.ntp.org
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement