Autoit UDP Request To NTP Timeserver -
i need use autoit time local ntp timeserver.
; function gets time timeserver not relay on pc time func get_time_from_time_server() local $isocket = udpopen(tcpnametoip($s_time_server), 123) ; port 123 local $data = "" while $data = "" $data = udprecv($isocket, 100) sleep(100) wend endfunc
opening udp
connection seems work, $data
stays equal ""
.
where request socket in code?
here example of receiving data local ntp (source):
#include <date.au3> $recv = ntp_connect("10.24.1.20") consolewrite($recv & @crlf) parse_time($recv) func ntp_connect($ntp_server) udpstartup() dim $socket = udpopen(tcpnametoip($ntp_server), 123) $status = udpsend($socket, makepacket("1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) $data = "" while $data = "" $data = udprecv($socket, 100) sleep(100) wend udpclosesocket($socket) udpshutdown() return $data endfunc ;==>ntp_connect func makepacket($d) local $p = "" while $d $p &= chr(dec(stringleft($d, 2))) $d = stringtrimleft($d, 2) wend return $p endfunc ;==>makepacket func parse_time($bdata) $unsignedhexvalue = stringmid($bdata, 83, 8) $value = unsignedhextodec($unsignedhexvalue) $tzinfo = _date_time_gettimezoneinformation() $utc = _dateadd("s", $value, "1900/01/01 00:00:00") consolewrite($utc & @crlf) if $tzinfo[0] <> 2 ; 0 = daylight savings not used in current time zone / 1 = standard time $tzoffset = ($tzinfo[1]) * - 1 else ; 2 = daylight savings time $tzoffset = ($tzinfo[1] + $tzinfo[7]) * - 1 endif $utc = _dateadd("n", $tzoffset, $utc) $m = stringmid($utc, 6, 2) $d = stringmid($utc, 9, 2) $y = stringmid($utc, 1, 4) $h = stringmid($utc, 12, 2) $mi = stringmid($utc, 15, 2) $s = stringmid($utc, 18, 2) endfunc func unsignedhextodec($n) $ones = stringright($n, 1) $n = stringtrimright($n, 1) return dec($n) * 16 + dec($ones) endfunc ;==>unsignedhextodec
explanation of request packet here.
Comments
Post a Comment