TCPRecv
接收套接字(socket)连接的数据.
TCPRecv ( mainsocket, maxlen [, flag = 0] )
参数
mainsocket
|
使用 TCPAccept() 或 TCPConnect() 函数返回的套接字标识符
|
maxlen
|
最大接收字符数
|
flag
|
[可选] 设置为 1, 则强制函数返回二进制数据. (默认为 0, 自行检测二进制/字符串数据).
$TCP_DATA_DEFAULT (0) - (默认) 二进制/字符串之间会自动检测
$TCP_DATA_BINARY (1) - 返回二进制数据
常量定义在 "AutoItConstants.au3"
|
返回值
成功:
|
返回套接字连接发送的二进制/字符串
|
失败:
|
返回 "", @error 设置 为非 0 值
|
@error:
|
-1 无效的套接字
-2 未连接
或为 Windows API WSAGetError 的返回值 (见 MSDN)
|
@extended:
|
1 没有接收到字节数
|
备注
为向后兼容的原因, 函数会尝试返回默认字符串. 如果收到空(0x00)字符, 则返回值将是一个二进制类型.
要强制函数总是返回二进位的数据(最好的选择), 需要将"标志"参数设置为 1.
如果需要传递 Unicode 字符串, 它们必须被 StringToBinary()/BinaryToString() 编/解码.
相关
BinaryLen, BinaryMid, BinaryToString, TCPAccept, TCPConnect, TCPSend, TCPStartup, TCPTimeout (Option)
函数示例
示例 1
#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()
TCPStartup() ; Start the TCP 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 = 65432 ; Port used for the connection.
#Region GUI
Local $sTitle = "TCP 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, "", "TCP Server started")
GUICtrlSetState($idBtnClient, $GUI_HIDE)
GUICtrlSetState($idBtnServer, $GUI_DISABLE)
If Not MyTCP_Server($sIPAddress, $iPort) Then ExitLoop
Case $idBtnClient
WinSetTitle($sTitle, "", "TCP Client started")
GUICtrlSetState($idBtnServer, $GUI_HIDE)
GUICtrlSetState($idBtnClient, $GUI_DISABLE)
If Not MyTCP_Client($sIPAddress, $iPort) Then ExitLoop
EndSwitch
Sleep(10)
WEnd
#EndRegion GUI
EndFunc ;==>Example
Func MyTCP_Client($sIPAddress, $iPort)
; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified.
Local $iSocket = TCPConnect($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 "tata" to the server.
TCPSend($iSocket, "tata")
; 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.
TCPCloseSocket($iSocket)
EndFunc ;==>MyTCP_Client
Func MyTCP_Server($sIPAddress, $iPort)
; Assign a Local variable the socket and bind to the IP Address and Port specified with a maximum of 100 pending connexions.
Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
Local $iError = 0
If @error Then
; Someone is probably already listening on this IP Address and Port (script already running?).
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not listen, Error code: " & $iError)
Return False
EndIf
; Assign a Local variable to be used by the Client socket.
Local $iSocket = 0
Do ; Wait for someone to connect (Unlimited).
; Accept incomming connexions if present (Socket to close when finished; one socket per client).
$iSocket = TCPAccept($iListenSocket)
; If an error occurred display the error code and return False.
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
Return False
EndIf
If GUIGetMsg() = $GUI_EVENT_CLOSE Then Return False
Until $iSocket <> -1 ;if different from -1 a client is connected.
; Close the Listening socket to allow afterward binds.
TCPCloseSocket($iListenSocket)
; Assign a Local variable the data received.
Local $sReceived = TCPRecv($iSocket, 4) ;we're waiting for the string "tata" OR "toto" (example script TCPRecv): 4 bytes length.
; 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.
TCPCloseSocket($iSocket)
EndFunc ;==>MyTCP_Server
Func OnAutoItExit()
TCPShutdown() ; Close the TCP service.
EndFunc ;==>OnAutoItExit
示例 2
; I am the client, start me after the server! (Start first the example 2 of the TCPSend function).
#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
TCPStartup() ; Start the TCP 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 = 65432 ; Port used for the connection.
; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified.
Local $iSocket = TCPConnect($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
; Assign a Local variable the path of the file which will be received.
Local $sFilePath = FileSaveDialog("Save as", @MyDocumentsDir, "All types (*.*)", BitOR($FD_PATHMUSTEXIST, $FD_PROMPTOVERWRITE))
; If an error occurred display the error code and return False.
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONEXCLAMATION), "", "Client:" & @CRLF & "Invalid file chosen, Error code: " & $iError)
Return False
EndIf
; Assign a Local variable the handle of the file opened in binary overwrite mode.
Local $hFile = FileOpen($sFilePath, BitOR($FO_BINARY, $FO_OVERWRITE))
; Assign Locales Constant variables the number representing 4 KiB; the binary code for the end of the file and the length of the binary code.
Local Const $i4KiB = 4096, $bEOF = Binary(@CRLF & "{EOF}"), $iEOFLen = BinaryLen($bEOF)
; Assign a Local variable the empty binary data which will contain the binary data of the file.
Local $bData = Binary("")
; Assign a Local variable to store the length of the data received.
Local $iDataLen = 0
; Assign a Local variable a boolean.
Local $bEOFReached = False
Do
$bData = TCPRecv($iSocket, $i4KiB, $TCP_DATA_BINARY)
; If an error occurred display the error code and return False.
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Connection lost, Error code: " & $iError)
Return False
EndIf
$iDataLen = BinaryLen($bData)
; If nothing is received, retry for the incoming data.
If $iDataLen = 0 Then ContinueLoop
; If the end of the file is reached.
If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
; Strip the EOF code from the file data.
$bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)
; Set the EOFReached to True.
$bEOFReached = True
EndIf
FileWrite($hFile, $bData)
Until $bEOFReached
; Close the file handle.
FileClose($hFile)
; Display the successful message.
MsgBox($MB_SYSTEMMODAL, "", "Client:" & @CRLF & "File received.")
; Close the socket.
TCPCloseSocket($iSocket)
EndFunc ;==>Example
Func OnAutoItExit()
TCPShutdown() ; Close the TCP service.
EndFunc ;==>OnAutoItExit
----------------------------------------
|