本帖最后由 C.L 于 2010-10-8 17:08 编辑
回复 1# sliqi
可以让客户端将自已名称和IP地址一起发送给S端,S端收到后将数据分解出来显示
以前写过的一个判断客户端是否在线的例子,改动一下,增加了客户端将自已名称发送给S端并显示
代码如下:
Server端:
#include <GUIConstantsEx.au3>
#include <array.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[3]&" "&$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
Client端:
HotKeySet ('{ESC}','_exit')
While 1
ToolTip ("Client is Running",200,200)
_UDPsend_String (@IPAddress1,@IPAddress1&"|ok|"&@ComputerName)
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
先运行那个都可以 |