|
发表于 2008-6-23 20:04:17
|
显示全部楼层
服务端:
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,"server:"&@IPAddress1)
Sleep(100)
UDPCloseSocket($socket1)
EndIf
;其他代码.
EndIf
sleep(10)
WEnd
Func OnAutoItExit()
UDPCloseSocket($socket)
UDPShutdown()
EndFunc
客户端:
$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 StringInStr($data,"server:") Then
$serverip=StringSplit($data,":")
UDPCloseSocket($socket1)
UDPShutdown()
MsgBox(0,"服务器IP是:",$serverip[2])
Exit
EndIf
WEnd
EndIf
EndIf
Next
EndIf |
|