本帖最后由 C.L 于 2010-9-1 14:27 编辑
回复 4# 261869247
给你写一段代码,当客户上线时,发送自已的IP和在线信号,服务器端接收到客户端在线信号后,显示上线的IP地址和在线状态
下面的代码,先运行那个都可以正常使用,客户端按ESC键可以热键退出
服务器:#include <GUIConstantsEx.au3>
Opt ('GUIOnEventMode',1)
$Form1 = GUICreate("Form1", 403, 143, 251, 162)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$l_IPAddress = GUICtrlCreateLabel('没有在线的Ip', 88, 56, 84, 25)
$l_status = GUICtrlCreateLabel('离线', 232, 56, 84, 33)
GUISetState(@SW_SHOW)
UDPStartup()
$connect = UDPBind(@IPAddress1, 12345)
If @error <> 0 Then
MsgBox(0, "UDPBind", @error, 3)
Exit
EndIf
$timer = TimerInit ()
While 1
$BinData = UDPRecv($connect, 1024, 1)
$str = BinaryToString ($BinData,4)
If $str <> "" Then
$aStr = StringSplit($str,"|")
If $aStr[2] == "ok" Then
GUICtrlSetData ($l_IPAddress,$aStr[1])
GUICtrlSetData ($l_status,'在线')
$timer = TimerInit ()
EndIf
EndIf
If TimerDiff ($timer) > 1000 Then
GUICtrlSetData ($l_IPAddress,'没有在线的IP')
GUICtrlSetData ($l_status,'离线')
EndIf
WEnd
Func _exit()
Exit
EndFunc
客户端:HotKeySet ('{ESC}','_exit')
While 1
ToolTip ("Client is Running",200,200)
_UDPsend_String (@IPAddress1,@IPAddress1&"|ok")
Sleep (500)
WEnd
Func _UDPsend_String($tagIP,$string)
$bstring = StringToBinary ($string,4)
UDPStartup()
$udpsocket = UDPOpen($tagIP, 12345)
UDPSend($udpsocket, $string)
UDPCloseSocket($udpsocket)
UDPShutdown()
EndFunc
Func _exit()
Exit
EndFunc
|