TCPConnect
创建连接服务器的套接字(socket).
TCPConnect ( IPAddr, port )
参数
IPAddr
|
互联网协议点地址(IpV4) 如 "192.162.1.1".
|
port
|
创建套接字连接的端口.
|
返回值
成功:
|
返回主套接字标识符.
|
失败:
|
返回 -1 或 0, @error 设置 为非 0 值..
|
@error:
|
-2 未连接.
1 错误的IP地址.
2 错误的端口.
10060 连接超时.
一个连接尝试失败因为连接放在一段时间后没有适当的反应, 或因连接的主机没有响应导致已建立的连接失败.
见连接超时示例.
或为 Windows API WSAGetError 返回的值 (见 MSDN)
|
备注
函数用于客户端连接到服务器.
相关
TCPListen, TCPRecv, TCPSend, TCPStartup, TCPTimeout (Option)
函数示例
示例 1
#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.
; 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)
; 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.
Local $iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not connect, Error code: " & $iError)
Return False
Else
MsgBox($MB_SYSTEMMODAL, "", "Connection successful")
EndIf
; Close the socket.
TCPCloseSocket($iSocket)
EndFunc ;==>Example
Func OnAutoItExit()
TCPShutdown() ; Close the TCP service.
EndFunc ;==>OnAutoItExit
TCPConnect with timeout
#include <MsgBoxConstants.au3>
; I am the client, start me after the server no more than 10sec ! (The server script is the TCPAccept example script).
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.
Opt("TCPTimeout", 1000)
Local $nMaxTimeout = 10 ; script will abort if no server available after 10 secondes
Local $iSocket, $iError
While 1
; Assign a Local variable the socket and connect to a Listening socket with the IP Address and Port specified.
$iSocket = TCPConnect($sIPAddress, $iPort)
; If an error occurred display the error code and return False.
If @error = 10060 Then
; Timeout occurs try again
$nMaxTimeout -= 1
If $nMaxTimeout < 0 Then
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not connect, after " & 10 - $nMaxTimeout & " TimeOut")
Return False
EndIf
ContinueLoop
ElseIf @error Then
$iError = @error
; The server is probably offline/port is not opened on the server.
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not connect, Error code: " & $iError)
Return False
Else
MsgBox($MB_SYSTEMMODAL, "", "Connection successful after " & 10 - $nMaxTimeout & " TimeOut")
ExitLoop
EndIf
WEnd
; Close the socket.
TCPCloseSocket($iSocket)
EndFunc ;==>Example
Func OnAutoItExit()
TCPShutdown() ; Close the TCP service.
EndFunc ;==>OnAutoItExit
----------------------------------------
|