Function Reference


TCPCloseSocket

Closes a TCP socket.

TCPCloseSocket ( socket )

Parameters

socket The socket identifier (SocketID) as returned by a TCPListen(),TCPConnect() or TCPAccept() functions.

Return Value

Success: 1.
Failure: 0 and sets the @error flag to non-zero.
@error: Windows API WSAGetLastError return value (see MSDN).

Related

TCPAccept, TCPConnect, TCPListen, TCPShutdown, TCPStartup

Example

#include <MsgBoxConstants.au3>
#include <WinAPIError.au3>

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.

        ; 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 listenings on this IP Address and Port (script already running?).
                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), $sMsgBoxTitle, "Could not listen, Error code: " & @error & @CRLF & @CRLF & _WinAPI_GetErrorMessage(@error))
                Return False
        Else
                MsgBox($MB_SYSTEMMODAL, $sMsgBoxTitle, "Listen successful.", 3)
        EndIf

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

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