梦想吧技术论坛's Archiver

似水年华 发表于 2007-4-22 06:10 PM

AU3-基于UDP协议的局域网通信工具

;;;;服务端------------------

#include <GUIConstants.au3>
HotKeySet("!q","CLOSEClicked")
hotkeyset("!s","startbtn")
dim $socket = -1,$PS = True
$mainwindow = GUICreate("基于UDP协议的局域网通信工具--服务端", 481, 440, 193, 121)
$text1 = GUICtrlCreateEdit("", 12, 12, 457, 384,0x0844 + 0x0008)
$startBtn = GUICtrlCreateButton("启动服务(ALT+&S)", 340, 405, 130, 25)
guictrlcreatelabel("启动服务 ALT+S,暂停服务 ALT+P,退出程序 ALT+Q",10,410)
GUISetState(@SW_SHOW)

pause()
Func pause()
if $socket <> -1 then UDPCloseSocket($socket)
While 1
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
  CLOSEClicked()
Case $msg = $startBtn
  startBtn()
EndSelect
WEnd
EndFunc

Func startbtn()
if $ps = True then
guictrlsetdata($startbtn,"暂停服务(ALT+&P)")
HotKeySet("!p","startbtn")
HotKeySet("!s")
$ps = not $ps
UDPs()
else
guictrlsetdata($startbtn,"启动服务(ALT+&S)")
$ps = not $ps
HotKeySet("!s","startbtn")
HotKeySet("!p")
pause()
endif
EndFunc

Func udps()
UDPStartup()
$socket = UDPBind("127.0.0.1", 64213)
If @error <> 0 Then Exit
While 1
    $data = UDPRecv($socket, 50)
    If $data <> "" Then
        GUICtrlSetData($text1,$data & @CRLF & guictrlread($text1) )
    EndIf
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
  CLOSEClicked()
Case $msg = $startBtn
  startBtn()
EndSelect
    sleep(50)
WEnd
EndFunc

Func CLOSEClicked()
$ynexit = MsgBox(262196, "提示", "确定退出本程序吗?")
if $ynexit = 6 then
  if $socket <> -1 then
   UDPCloseSocket($socket)
   UDPShutdown()
  EndIf
  Exit
EndIf
EndFunc


;;;;客户端----------------------

UDPStartup()
$socket = UDPOpen("127.0.0.1", 64213)
If @error <> 0 Then Exit
UDPSend($socket,@MON & "-" & @MDAY & "  " & @hour & ":" & @min & ":" & @sec & "  " &  @ipaddress1 & " 启动计算机!")
UDPCloseSocket($socket)
UDPShutdown()

;;;官方帮助文档自带的客户端--------

UDPStartup()
$socket = UDPOpen("127.0.0.1", 64213)
If @error <> 0 Then Exit

$n=0
While 1
    Sleep(2000)
    $n = $n + 1
    $status = UDPSend($socket, "Message #" & $n)
    If $status = 0 then
        MsgBox(0, "ERROR", "Error while sending UDP message: " & @error)
        Exit
    EndIf
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.