UDPRecv
从打开的套接字接收数据.
UDPRecv ( socketarray, maxlen [, flag = 0] )
参数
socketarray
|
使用 UDPBind() 函数返回的套接字/数组
|
maxlen
|
最大接收字符数
|
flag
|
[可选]
UDP_DATA_DEFAULT (0) - 自动检测二进制/字符串数据.
UDP_DATA_BINARY (1) - 返回二进制数据
$UDP_DATA_ARRAY (2) - 返回结果到数组 : [0] 数据, [1] IP, [2] 端口.
(3) - 上述条件都使用
常量定义在 "AutoItConstants.au3"
|
返回值
成功:
|
返回已连接套接字接收的二进制/字符串的值, 如果标志为 2 或 3, 则返回一个数组.
|
失败:
|
返回 "", @error 设置 为非 0 值.
|
@error:
|
-1 无效的套接字.
-2 未连接.
-3 或 -4 无效的套接字数组.
或为 windows API WSAGetError 的返回值 (见 MSDN)
|
备注
由于向后兼容的原因, 函数将试着返回默认字符串. 如果接收空字符 (0x00), 则返回值将是二进制类型.
要强制函数总是返回二进制数据(最好的选择), 需将" 标志 "参数设置为 1.
相关
BinaryLen, BinaryMid, UDPBind, UDPOpen, UDPSend
函数示例
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
; Start First clicking on "1. Server"
; Then start a second instance of the script selecting "2. Client"
Example()
Func Example()
UDPStartup() ; Start the UDP service.
; Register OnAutoItExit to be called when the script is closed.
OnAutoItExitRegister("OnAutoItExit")
; Assign Local variables the loopback IP Address and the Port.
Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer.
Local $iPort = 65532 ; Port used for the connection.
#Region GUI
Local $sTitle = "UDP Start"
Local $hGUI = GUICreate($sTitle, 250, 70)
Local $idBtnServer = GUICtrlCreateButton("1. Server", 65, 10, 130, 22)
Local $idBtnClient = GUICtrlCreateButton("2. Client", 65, 40, 130, 22)
GUISetState(@SW_SHOW, $hGUI)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $idBtnServer
WinSetTitle($sTitle, "", "UDP Server started")
GUICtrlSetState($idBtnClient, $GUI_HIDE)
GUICtrlSetState($idBtnServer, $GUI_DISABLE)
If Not MyUDP_Server($sIPAddress, $iPort) Then ExitLoop
Case $idBtnClient
WinSetTitle($sTitle, "", "UDP Client started")
GUICtrlSetState($idBtnServer, $GUI_HIDE)
GUICtrlSetState($idBtnClient, $GUI_DISABLE)
If Not MyUDP_Client($sIPAddress, $iPort) Then ExitLoop
EndSwitch
Sleep(10)
WEnd
#EndRegion GUI
EndFunc ;==>Example
Func MyUDP_Server($sIPAddress, $iPort)
; Assign a Local variable the socket and bind to the IP Address and Port specified.
Local $iSocket = UDPBind($sIPAddress, $iPort)
; If an error occurred display the error code and return False.
If @error Then
; Someone is probably already binded on this IP Address and Port (script already running?).
Local $iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not bind, Error code: " & $iError)
Return False
EndIf
; Assign a Local variable to store the data received.
Local $sReceived = ""
Do
; We are waiting for the string "toto" OR "tata" (example script UDPSend): 4 bytes length.
$sReceived = UDPRecv($iSocket, 4)
Until $sReceived <> ""
; Notes: If you don't know how much length will be the data,
; use e.g: 2048 for maxlen parameter and call the function until the it returns nothing/error.
; Display the string received.
MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "Received: " & $sReceived)
; Close the socket.
UDPCloseSocket($iSocket)
EndFunc ;==>MyUDP_Server
Func MyUDP_Client($sIPAddress, $iPort)
; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified.
Local $iSocket = UDPOpen($sIPAddress, $iPort)
Local $iError = 0
; If an error occurred display the error code and return False.
If @error Then
; The server is probably offline/port is not opened on the server.
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError)
Return False
EndIf
; Send the string "toto" converted to binary to the server.
UDPSend($iSocket, StringToBinary("toto"))
; If an error occurred display the error code and return False.
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)
Return False
EndIf
; Close the socket.
UDPCloseSocket($iSocket)
EndFunc ;==>MyUDP_Client
Func OnAutoItExit()
UDPShutdown() ; Close the UDP service.
EndFunc ;==>OnAutoItExit
----------------------------------------
|