|
本帖最后由 xunyar 于 2009-9-28 13:07 编辑
我想做一个时间同步工具,想下面代码判断wen.ini文件里面服务器地址是否正常,可是下面代码只找到最后一条地址的,第一条和第二条,正常的却找不到的?
我的wen.ini内容如下:
[地址]
0=time-nw.nist.gov;第一条正确的,可是只能找得到第二条,
0=www.bsdfsdf.com
时间同步工具.au3内容即下
#include <CoProc.au3>
#Include <Misc.au3>
#include <Date.au3>
$url = IniReadSection(@ScriptDir&"\wen.ini", "地址")
for $i = 0 to UBound($url,1)-1
$ntpServer = $url[$i][1] ;改为ini配置
local $var1,$var2
;Global Const $tagSYSTEMTIME = "short Year;short Month;short Dow;short Day;short Hour;short Minute;short Second;short MSeconds"
;Global Const $tagTIME_ZONE_INFORMATION = "dword Bias;byte StdName[64];ushort StdDate[8];dword StdBias;byte DayName[64];ushort DayDate[8];dword DayBias"
dim $TimeZone=8*60*60
UDPStartup()
Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123)
If @error <> 0 Then
MsgBox(0,"","连接服务器失败!")
Exit
EndIf
;$status = UDPSend($socket, MakePacket("1b0e010000000000000000004c4f434ccb1eea7b866665cb00000000000000000000000000000000cb1eea7b866665cb"))
$status = UDPSend($socket, MakePacket("1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
If $status = 0 Then ExitLoop
Next
$data=""
$i=0
While $data=""
$i+=1
$data = UDPRecv($socket, 100)
If $i = 5 Then
MsgBox(4096, "错误", $i )
Exit
EndIf
MsgBox(4096, "错误", $i )
sleep(100)
WEnd
UDPCloseSocket($socket)
UDPShutdown()
$unsignedHexValue=StringMid($data,83,8); Extract time from packet. Disregards the fractional second.
$value=UnsignedHexToDec($unsignedHexValue)
$value=$value+$TimeZone
$UTC=_DateAdd("s",$value,"1900/01/01 00:00:00")
$var1=StringSplit(StringLeft($UTC,10),'/')
$var2=StringSplit(StringRight($UTC,8),':')
_SetTime($var2[1],$var2[2],$var2[3])
_SetDate($var1[3],$var1[2],$var1[1])
MsgBox(0,"提示","NTP服务器时间同步成功,当前系统时间为: "&$UTC)
Func MakePacket($d)
Local $p=""
While $d
$p&=Chr(Dec(StringLeft($d,2)))
$d=StringTrimLeft($d,2)
WEnd
Return $p
EndFunc
Func UnsignedHexToDec($n)
$ones=StringRight($n,1)
$n=StringTrimRight($n,1)
Return dec($n)*16+dec($ones)
EndFunc
请看一下我上面的代码如何才能正常判断,我的意思是想如果第一条正常,就选择第一条,第一条不正常就选择第二条,有那位高手知道的呢? |
|