Re: Get time and date from a server using a socket [message #65331] |
Fri, 27 February 2009 08:34  |
Allan Whiteford
Messages: 117 Registered: June 2006
|
Senior Member |
|
|
bernat wrote:
> Lool !
>
> Thank you Allan... It works for me too.
> I never expected that it's required to read 2 time on the port.
>
> Thank you !
>
> Bernat
Bernat,
It only seems to be the NIST timeservers which add this extra line. I
don't know why though.
If I start a daytime server on my local machine I get this:
[allan@hostname ~]$ telnet localhost 13
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
27 FEB 2009 16:25:04 GMT
Connection closed by foreign host.
[allan@hostname ~]$
whereas the NIST server gives me:
[allan@hostname ~]$ telnet time-a.nist.gov 13
Trying 129.6.15.28...
Connected to time-a.nist.gov.
Escape character is '^]'.
54889 09-02-27 16:26:16 00 0 0 230.6 UTC(NIST) *
Connection closed by foreign host.
[allan@hostname ~]$
where the blank line has been sent back by NIST.
A glance over the RFC (http://www.faqs.org/rfcs/rfc867.html) suggests
that there isn't a particular format for what comes back so I don't
think you should try to parse it. It also suggets that the whole thing
should be on one line but doesn't explicitly say anything about whether
a blank line is allowable first. The recommendation is that if you want
something machine readable you should use port 37 (RFC868).
However, I don't think it's ever safe to ask for the time over TCP/IP
due to possible time-lag in the response (that's why NTP runs over UDP).
My opinion is still that you should just have the local time set
accurately using NTP and only ever ask your local machine for the time
from within IDL.
Thanks,
Allan
|
|
|
|
|
Re: Get time and date from a server using a socket [message #65343 is a reply to message #65340] |
Fri, 27 February 2009 01:19   |
Allan Whiteford
Messages: 117 Registered: June 2006
|
Senior Member |
|
|
bernat wrote:
> Thanks Allan,
>
> Ok, I can reject NTP to get the time from external server.
> That's true that maybe is the firewall who blocks the communication
> but I think that my code (read a simple string on port 13) should
> works. The IDL help, shows this same example and I wanna find my
> purpose
>
> More suggestions ?
>
>
The following works for me:
IDL> socket, lun, 'time-a.nist.gov', 13, /get_lun
IDL> mystring=''
IDL> readf, lun, mystring
IDL> readf, lun, mystring
IDL> free_lun, lun
IDL> print, mystring
54889 09-02-27 08:52:23 00 0 0 807.6 UTC(NIST) *
Note that this timeserver seems to first return a blank line and then on
the next line it returns the actual time. I don't know if daytime is
supposed to do this or not. Maybe that is what was causing you a problem.
For port 37, I guess the way to go is something like this:
IDL> socket, lun, 'time-a.nist.gov', 37, /get_lun,/swap_endian
IDL> data=0ul
IDL> readu, lun, data
IDL> free_lun, lun
IDL> print, data
3444714975
IDL> print, data / 60 / 60 / 24 / 365.26
109.152
where data is the number of seconds since 1/1/1900. The 109 and a
wee-bit years seems sensible. YMMV with the need to use /swap_endian.
Maye there is a function to do the (non-trivial) conversion for you in
IDL, I'm not sure - it's certainly not a case of just assuming there are
365.26 days in a year (which may not even be true).
Thanks,
Allan
|
|
|
|
Re: Get time and date from a server using a socket [message #65363 is a reply to message #65358] |
Thu, 26 February 2009 00:21   |
Allan Whiteford
Messages: 117 Registered: June 2006
|
Senior Member |
|
|
bernat wrote:
> Hello people,
>
> I'm trying to get the time and date from a public server using this
> code:
>
> SOCKET, 1, servername, 13
> date=''
> READF, 1, date
> CLOSE, 1
> PRINT, date
>
> I tried to get the date from all the servers that I found in the
> following link: http://tf.nist.gov/tf-cgi/servers.cgi
>
> The port 13 is the port for the daytime but I tried with 37 and 123
> ports (time and NTP protocol).
> I can't obtain a good result. Does anybody tried that ? Any
> suggestions or comments ?
>
> Thanks in advance,
>
> Bernat
>
>
Bernat,
NTP uses UDP rather than TCP for communication. Socket only does (as far
as I know) TCP communication. I don't think IDL can (easily) do UDP.
Probably spawning an external program is the way to go if you want to
use NTP over UDP. I have no idea about the other protocols.
If it's useful, the unix command "ntpdate -q servername" will attempt to
speak NTP for you and print the time back to the screen.
You should also check that firewalls, either on your local machine or at
your connection to the internet, aren't blocking things. Some large
organisations run their own time servers and block access to external ones.
Stepping back from the problem (and making some assumptions) another
possible solution is probably for you to run time synchronization
software on your local computer so that the time and date are always
correct when just ask IDL for the local system time.
Thanks,
Allan
|
|
|
Re: Get time and date from a server using a socket [message #65388 is a reply to message #65363] |
Mon, 02 March 2009 10:22  |
Rick Towler
Messages: 821 Registered: August 1998
|
Senior Member |
|
|
Allan Whiteford wrote:
> bernat wrote:
> NTP uses UDP rather than TCP for communication. Socket only does (as far
> as I know) TCP communication. I don't think IDL can (easily) do UDP.
For the record, I've modified Randall Frank's socket DLM to offer both
TCP and UDP sockets and a class that wraps it up making it a bit easier
to use. The project has whithered on the vine as I ended up doing it in
python (a phrase I seem to be saying more and more these days) but the
code is available upon request.
-Rick
|
|
|
Re: Get time and date from a server using a socket [message #65407 is a reply to message #65343] |
Sat, 28 February 2009 14:01  |
DMW
Messages: 1 Registered: February 2009
|
Junior Member |
|
|
"Allan Whiteford" <allan.remove@phys.remove.strath.ac.remove.uk> wrote in
message news:go8b7n$is7$1@menace.cc.strath.ac.uk...
> bernat wrote:
>
> The following works for me:
>
> IDL> socket, lun, 'time-a.nist.gov', 13, /get_lun
> IDL> mystring=''
> IDL> readf, lun, mystring
> IDL> readf, lun, mystring
> IDL> free_lun, lun
> IDL> print, mystring
> 54889 09-02-27 08:52:23 00 0 0 807.6 UTC(NIST) *
>
Something's wrong here. With the change to DST in the U.S. and Canada only
a couple of weeks away, why is the DST field set to 00? As of today it
should read 58 if I understand the formatting correctly.
It's not just *.nist.gov servers that are displaying this. bldrdoc.gov is
doing the same.
Brian Garrett
|
|
|