|
发表于 2008-5-26 00:00:05
|
显示全部楼层
以前也做过这方面的实验,发代码上来大家研究一下,代码是修改的 titer 兄弟发的 局域网内客户端自动连接服务端 所以客户机不需要指定服务器IP地址 [url]http://www.autoitx.com/forum.php?mod=viewthread&tid=811&extra=page%3D2[url]
服务器代码:
[au3]UDPStartup()
$socket = UDPBind(@IPAddress1, 8410)
If @error <> 0 Then Exit
While 1
$data = UDPRecv($socket, 50)
If $data <> "" Then
If StringInStr($data,"client:") Then
$ip=StringSplit($data,":")
$socket1=UDPOpen($ip[2],8411)
$send=UDPSend($socket1,@HOUR & "," & @MIN & "," & @SEC & "," & @YEAR & "," & @MON & "," & @MDAY)
Sleep(100)
UDPCloseSocket($socket1)
EndIf
EndIf
sleep(10)
WEnd
Func OnAutoItExit()
UDPCloseSocket($socket)
UDPShutdown()
EndFunc[/au3]
客户机代码:
[au3]$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", 0x10 + 0x20)
If IsObj($colItems) then
For $objItem In $colItems
Dim $netmask=""
$localIP=$objItem.IPAddress(0)
$ip=StringSplit($localIP,".")
$net=StringSplit($objItem.IPSubnet(0),".")
For $i=1 To $ip[0]
If $i = $ip[0] Then
$netmask=$netmask&BitOR($ip[$i],BitXOR($net[$i],255))
Else
$netmask=$netmask&BitOR($ip[$i],BitXOR($net[$i],255))&"."
EndIf
Next
If Ping($netmask,10) Then
UDPStartup()
$socket = UDPOpen($netmask, 8410)
$socket1 = UDPBind($localIP,8411)
$status = UDPSend($socket, "client:"&$localIP )
If $status <> 0 then
UDPCloseSocket($socket)
While 1
$data = UDPRecv($socket1, 50)
If $data <> "" Then
UDPCloseSocket($socket1)
UDPShutdown()
$Timen=StringSplit($data,",")
SetLocalTime($Timen[1],$Timen[2],$Timen[3],$Timen[4],$Timen[5],$Timen[6])
EndIf
WEnd
EndIf
EndIf
Next
EndIf
Func SetLocalTime($Hour, $Min, $Sec =0,$Year = 0,$Month = 0,$Day = 0)
Local $Error = 0
Local $Return = False
If $Hour < 0 Or $Hour > 23 Then $Error = 1
If $Min < 0 Or $Min > 59 Then $Error = 1
If $Sec < 0 Or $Sec > 59 Then $Error = 1
If @NumParams > 3 And ($Day < 1 Or $Day > 31) Then $Error = 1
If @NumParams > 4 And ($Month < 1 Or $Month > 12) Then $Error = 1
If @NumParams > 5 And ($Year < 1601 Or $Year > 30827) Then $Error = 1
If Not $Error Then
Local $Struct = DLLStructCreate('Short;Short;Short;Short;Short;Short;Short;Short')
Local $StructPtr = DLLStructGetPtr($Struct)
If @NumParams < 6 Then
DLLCall('Kernel32', 'None', 'GetLocalTime', 'Ptr', $StructPtr)
If @Error Then $Error = 2
EndIf
If Not $Error Then
DLLStructSetData($Struct, 5, $Hour)
DLLStructSetData($Struct, 6, $Min)
DLLStructSetData($Struct, 7, $Sec)
DLLStructSetData($Struct, 8, 0)
If @NumParams > 3 Then DLLStructSetData($Struct, 4, $Day)
If @NumParams > 4 Then DLLStructSetData($Struct, 2, $Month)
If @NumParams > 5 Then DLLStructSetData($Struct, 1, $Year)
DLLCall('Kernel32', 'None', 'SetLocalTime', 'Ptr', $StructPtr)
Local $Result = DLLCall('Kernel32', 'Int', 'SetLocalTime', 'Ptr', $StructPtr)
If @Error Then
$Error = 2
Else
$Return = ($Result[0] <> 0)
EndIf
EndIf
EndIf
SetError($Error)
Return $Return
EndFunc[/au3]
[ 本帖最后由 bing614 于 2008-5-26 00:02 编辑 ] |
评分
-
查看全部评分
|