Function Reference


Creates a socket listening for an incoming connection.

TCPListen ( IPAddr, port [, MaxPendingConnection] )


IPAddr Internet Protocol dotted address(IpV4) as "".
port port on which the created socket will be connected.
MaxPendingConnection [optional] Maximum length of the queue of pending connections. By default the maximum reasonable value will be set.

Return Value

Success: the main socket identifier.
Failure: -1 or 0 and sets the @error flag to non-zero.
@error: 1 IPAddr is incorrect.
2 port is incorrect.
Windows API WSAGetLastError return value (see MSDN).


TCPAccept, TCPCloseSocket, TCPConnect, TCPSend, TCPShutdown, TCPStartup, TCPTimeout (Option)


#include <MsgBoxConstants.au3>


Func Example()
    TCPStartup() ; Start the TCP service.

    ; Register OnAutoItExit to be called when the script is closed.

    ; Assign Local variables the loopback IP Address and the Port.
    Local $sIPAddress = "" ; This IP Address only works for testing on your own computer.
    Local $iPort = 65432 ; Port used for the connection.

    ; Assign a Local variable the Listening socket and bind to the IP Address and Port specified with a maximum of 100 pending connexions.
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)

    ; Notes: You can only listen on private IPs, such as the one used here;
    ; or on the range of 192 to 223 (generally 192.168.X.X, use @IPAddress1 to test on your local IP [you will need another computer]).
    ; The Listen socket identifier is only used for the TCP Accept function.

    ; If an error occurred display the error code and return False.
    If @error Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        Local $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not listen, Error code: " & $iError)
        Return False
        MsgBox($MB_SYSTEMMODAL, "", "Listen successful.")

    ; Close the Listening socket to allow afterward binds.
    ; While not closed, any other program can NOT bind to the same IP Address and Port.
EndFunc   ;==>Example

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit