<p>这是我根椐官方说明文件上面带的一个实例改的,嘿嘿,大家请勿见笑.</p><p>运行服务器,点击"启动服务",然后运行客户端,这时在服务端上就可以看到客户端发来的消息.</p><p>大家有兴趣可以去看看官方的帮助文件里面的几个例子,写的很不错.</p><p>;;;;服务端------------------</p><p>#i nclude <GUIConstants.au3><br />HotKeySet("!q","CLOSEClicked")<br />hotkeyset("!s","startbtn")<br />dim $socket = -1,$PS = True<br />$mainwindow = GUICreate("基于UDP协议的局域网通信工具--服务端", 481, 440, 193, 121)<br />$text1 = GUICtrlCreateEdit("", 12, 12, 457, 384,0x0844 + 0x0008)<br />$startBtn = GUICtrlCreateButton("启动服务(ALT+&S)", 340, 405, 130, 25)<br />guictrlcreatelabel("启动服务 ALT+S,暂停服务 ALT+P,退出程序 ALT+Q",10,410)<br />GUISetState(@SW_SHOW)</p><p>pause()<br />Func pause()<br />if $socket <> -1 then UDPCloseSocket($socket)<br />While 1<br /> $msg = GuiGetMsg()<br /> Select<br /> Case $msg = $GUI_EVENT_CLOSE<br /> CLOSEClicked()<br /> Case $msg = $startBtn<br /> startBtn()<br /> EndSelect<br />WEnd<br />EndFunc</p><p>Func startbtn()<br />if $ps = True then<br /> guictrlsetdata($startbtn,"暂停服务(ALT+&P)")<br /> HotKeySet("!p","startbtn")<br /> HotKeySet("!s")<br /> $ps = not $ps<br /> UDPs()<br />else<br /> guictrlsetdata($startbtn,"启动服务(ALT+&S)")<br /> $ps = not $ps<br /> HotKeySet("!s","startbtn")<br /> HotKeySet("!p")<br /> pause()<br />endif<br />EndFunc</p><p>Func udps()<br />UDPStartup()<br />$socket = UDPBind("127.0.0.1", 64213)<br />If @error <> 0 Then Exit<br />While 1<br /> $data = UDPRecv($socket, 50)<br /> If $data <> "" Then<br /> GUICtrlSetData($text1,$data & @CRLF & guictrlread($text1) )<br /> EndIf<br /> $msg = GuiGetMsg()<br /> Select<br /> Case $msg = $GUI_EVENT_CLOSE<br /> CLOSEClicked()<br /> Case $msg = $startBtn<br /> startBtn()<br /> EndSelect<br /> sleep(50)<br />WEnd<br />EndFunc</p><p>Func CLOSEClicked()<br /> $ynexit = MsgBox(262196, "提示", "确定退出本程序吗?")<br /> if $ynexit = 6 then <br /> if $socket <> -1 then<br /> UDPCloseSocket($socket)<br /> UDPShutdown()<br /> EndIf<br /> Exit<br /> EndIf<br />EndFunc<br /></p><p>;;;;客户端----------------------</p><p>UDPStartup()<br />$socket = UDPOpen("127.0.0.1", 64213)<br />If @error <> 0 Then Exit<br />UDPSend($socket,@MON & "-" & @MDAY & " " & @hour & ":" & @min & ":" & @sec & " " & @ipaddress1 & " 启动计算机!")<br />UDPCloseSocket($socket)<br />UDPShutdown()</p><p>;;;官方帮助文档自带的客户端--------</p><p><span>UDPStartup</span><span>()</span><br /><span>$socket</span><span> </span><span>=</span><span> </span><span>UDPOpen</span><span>(</span><span>"127.0.0.1"</span><span>,</span><span> </span><span>64213</span><span>)</span><br /><span>If</span><span> </span><span>@error</span><span> </span><span><></span><span> </span><span>0</span><span> </span><span>Then</span><span> </span><span>Exit</span><br /><br /><span>$n</span><span>=</span><span>0</span><br /><span>While</span><span> </span><span>1</span><br /><span> </span><span>Sleep</span><span>(</span><span>2000</span><span>)</span><br /><span> </span><span>$n</span><span> </span><span>=</span><span> </span><span>$n</span><span> </span><span>+</span><span> </span><span>1</span><br /><span> </span><span>$status</span><span> </span><span>=</span><span> </span><span>UDPSend</span><span>(</span><span>$socket</span><span>,</span><span> </span><span>"Message #"</span><span> </span><span>&</span><span> </span><span>$n</span><span>)</span><br /><span> </span><span>If</span><span> </span><span>$status</span><span> </span><span>=</span><span> </span><span>0</span><span> </span><span>then</span><span> </span><br /><span> </span><span>MsgBox</span><span>(</span><span>0</span><span>,</span><span> </span><span>"ERROR"</span><span>,</span><span> </span><span>"Error while sending UDP message: "</span><span> </span><span>&</span><span> </span><span>@error</span><span>)</span><br /><span> </span><span>Exit</span><br /><span> </span><span>EndIf</span><br /><span>WEnd</span><br /><br /><span>Func</span><span> OnAutoItExit</span><span>()</span><br /><span> </span><span>UDPCloseSocket</span><span>(</span><span>$socket</span><span>)</span><br /><span> </span><span>UDPShutdown</span><span>()</span><br /><span>EndFunc</span></p> |