unique009 发表于 2010-5-4 17:22:23

求监听本机任意端口的源代码

局域网中,服务器程序是C++做的,不停的向本机指定端口发送数据,我想要做的就是接收并处理这些数据,但是照着帮助中的例子做,能打开端口却收不到数据,请各位帮帮忙,帮我写个代码,谢谢

ghl781258 发表于 2010-5-6 17:55:59

#AutoIt3Wrapper_run_debug_mode=Y
#include <ButtonConstants.au3>



#include <EditConstants.au3>



#include <GUIConstantsEx.au3>



#include <StaticConstants.au3>



#include <WindowsConstants.au3>



Opt("GUIOnEventMode", 1)



$ip=@IPAddress1



UDPStartup()



$socket = UDPBind(@IPAddress1, 65532)

msgBox(0,"发送",$socket)

If @error <> 0 Then Exit



$Form1 = GUICreate("测试-发送", 291, 208, 192, 114)



GUISetOnEvent($GUI_EVENT_CLOSE, "_ext")



$Edit1 = GUICtrlCreateEdit("", 8, 28, 273, 116, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_CLIPSIBLINGS,$ES_READONLY,$ws_VSCROLL))



GUICtrlSetData(-1, "")



$Button1 = GUICtrlCreateButton("退出[&Esc]", 168, 176, 113, 25, 0)



GUICtrlSetOnEvent(-1, "_ext")



GUISetState(@SW_SHOW)





While 1



$i = 1

While$i<80000



      

    GUICtrlSetData($edit1, $i & @CRLF & GUICtrlRead($edit1))

      $connect = UDPOpen(@IPAddress1,514);udp发送数据测试

      $status = UDPSend($connect, StringToBinary($i ,4))

    $i=$i+1

WEnd



                     

WEnd





Func _ext()



    UDPCloseSocket($socket)



      UDPShutdown()



      Exit



EndFunc

----------------------------------------------------
#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$ip=@IPAddress1

UDPStartup()

$socket = UDPBind(@IPAddress1, 514)
MsgBox(0,"接收",$socket)

If @error <> 0 Then Exit


$Form1 = GUICreate("测试-接收", 291, 208, 192, 114)

GUISetOnEvent($GUI_EVENT_CLOSE, "_ext")

$Edit1 = GUICtrlCreateEdit("", 8, 28, 273, 116, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_CLIPSIBLINGS,$ES_READONLY,$ws_VSCROLL))


GUICtrlSetData(-1, "")


$Button1 = GUICtrlCreateButton("退出[&Esc]", 168, 176, 113, 25, 0)

GUICtrlSetOnEvent(-1, "_ext")

GUISetState(@SW_SHOW)


While 1

      $data = UDPRecv($socket, 500,1)
      If $data <> "" Then
                        $data = BinaryToString($data,4)
                        GUICtrlSetData($edit1, $data & @CRLF & GUICtrlRead($edit1))
                EndIf

WEnd



Func _ext()

    UDPCloseSocket($socket)

      UDPShutdown()

      Exit

EndFunc
页: [1]
查看完整版本: 求监听本机任意端口的源代码