Create a socket connected to an existing server.
TCPConnect ( IPAddr, port )
IPAddr | Internet Protocol dotted address(IpV4) as "192.162.1.1". |
port | port on which the created socket will be connected. |
Success: | the main socket identifier. |
Failure: | -1 or 0 and sets the @error flag to non-zero. |
@error: | -2 not connected. 1 IPAddr is incorrect. 2 port is incorrect. 10060 Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond. See connect timeout example. Windows API WSAGetLastError return value (see MSDN). |
This function is used by a client to communicate with the server.
TCPListen, TCPRecv, TCPSend, TCPStartup, TCPTimeout (Option)
#include <MsgBoxConstants.au3>
#include <WinAPIError.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(($MB_ICONERROR + $MB_SYSTEMMODAL), "", "Could not connect, Error code: " & $iError & @CRLF & _WinAPI_GetErrorMessage($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
#include <MsgBoxConstants.au3> #include <WinAPIError.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() Local $sMsgBoxTitle = "AutoItVersion = " & @AutoItVersion 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, $hTimer = TimerInit() 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(($MB_ICONERROR + $MB_SYSTEMMODAL), $sMsgBoxTitle, "Could not connect, after " & Int(TimerDiff($hTimer) / 1000) & " sec") Return False EndIf ContinueLoop ElseIf @error Then ; The server is probably offline/port is not opened on the server. MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), $sMsgBoxTitle, "Could not connect, Error code: " & @error & @CRLF & _WinAPI_GetErrorMessage(@error)) Return False Else MsgBox($MB_SYSTEMMODAL, $sMsgBoxTitle, "Connection successful after " & Int(TimerDiff($hTimer) / 1000) & " sec") ExitLoop EndIf WEnd ; Close the socket. TCPCloseSocket($iSocket) EndFunc ;==>Example Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit