本帖最后由 txm888 于 2014-7-22 10:43 编辑
现在新版的TCPRecv反复接收为空时,也返回错误@error = -1,以前版本不存在,如果是这样,要如何反复接收,且不要检测为错误呢?因为我还想用这种方式判断另一端是否在线。#include <MsgBoxConstants.au3>
监听端:
[code]#include <MsgBoxConstants.au3>
Example()
Func Example()
TCPStartup()
OnAutoItExitRegister("OnAutoItExit")
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.
Local $iListenSocket = TCPListen($sIPAddress, $iPort)
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), "", "Could not listen, Error code: " & $iError)
Return False
EndIf
Local $iSocket = 0
Do
$iSocket = TCPAccept($iListenSocket)
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not accept the incoming connection, Error code: " & $iError)
Return False
EndIf
Until $iSocket <> -1
While 1
sleep(100)
WEnd
TCPCloseSocket($iListenSocket)
MsgBox($MB_SYSTEMMODAL, "", "Client Connected.")
TCPCloseSocket($iSocket)
EndFunc ;==>Example
Func OnAutoItExit()
TCPShutdown() ; Close the TCP service.
EndFunc ;==>OnAutoItExit
请求端:#include <MsgBoxConstants.au3>
; I am the client, start me after the server! (Start first the TCPAccept example script).
Example()
Func Example()
TCPStartup() ; Start the TCP service.
OnAutoItExitRegister("OnAutoItExit")
Local $sIPAddress = "127.0.0.1"
Local $iPort = 65432
Local $iSocket = TCPConnect($sIPAddress, $iPort)
If @error Then
Local $iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not connect, Error code: " & $iError)
Return False
Else
MsgBox($MB_SYSTEMMODAL, "", "Connection successful")
While 1
$data = TCPRecv($iSocket, 1)
If @error Then
Msgbox(0, "已接收" & $data & "内容", "接收错误" & @error)
Exit
EndIf
WEnd
EndIf
TCPCloseSocket($iSocket)
EndFunc ;==>Example
Func OnAutoItExit()
TCPShutdown()
EndFunc ;==>OnAutoItExit
|