# Function Reference

# TCPAccept

Permits an incoming connection attempt on a socket.

TCPAccept ( mainsocket )

## Parameters

mainsocket | The main socket identifier (SocketID) as returned by a TCPListen() function.

## Return Value

Success: | the connected socket identifier.
Failure: | -1 and sets the @error flag to non-zero.
@error: | -2 not connected. Windows API WSAGetLastError return value (see MSDN).

## Related

TCPCloseSocket, TCPListen, TCPRecv, TCPStartup, TCPTimeout (Option)

## Example

### Example 1

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

    ; I am the server, start me first! (Start in second the TCPConnect 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.

        ; Bind to the IP Address and Port specified with a maximum of 100 pending connections
        ;(Take a look at the example of this function for further details).
        Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
        Local $iError = 0

        ; 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?).
            $iError = @error
            MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), "", "Could not listen, Error code: " & $iError)
            Return False
        EndIf

        ; Assign Local variable to be used by Listening and Client sockets.
        Local $iSocket = 0
        Do ; Wait for someone to connect (Unlimited).
            ; Accept incomming connections if present (Socket to close when finished; one socket per client).
            $iSocket = TCPAccept($iListenSocket)

            ; If an error occurred display the error code and return False.
            If @error Then
                $iError = @error
                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), "", "Could not accept the incoming connection, Error code: " & $iError & @CRLF & _WinAPI_GetErrorMessage($iError))
                Return False
            EndIf
        Until $iSocket <> -1 ;if different from -1 a client is connected.

        ; Close the Listening socket to allow afterward binds.
        TCPCloseSocket($iListenSocket)

        MsgBox($MB_SYSTEMMODAL, "", "Client Connected.")

        ; Close the socket.
        TCPCloseSocket($iSocket)
    EndFunc   ;==>Example

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

### Example 2

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

    ; Note: Check the Example 1 to get the useful comments, this example only demonstrates the SocketToIP user-defined function.

    ; I am the server, start me first! (Start in second the TCPConnect example script).

    Example()

    Func Example()
        Local $sMsgBoxTitle = "AutoItVersion = " & @AutoItVersion

        TCPStartup()

        OnAutoItExitRegister("OnAutoItExit")

        Local $sIPAddress = "127.0.0.1"
        Local $iPort = 65432

        Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)

        If @error Then
            MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), $sMsgBoxTitle, "Could not listen, Error code: " & @error & @CRLF & @CRLF & _WinAPI_GetErrorMessage(@error))
            Return False
        EndIf

        Local $iSocket = 0
        Do
            $iSocket = TCPAccept($iListenSocket)

            If @error Then
                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), $sMsgBoxTitle, "Could not accept the incoming connection, Error code: " & @error & @CRLF & @CRLF & _WinAPI_GetErrorMessage(@error))
                Return False
            EndIf
        Until $iSocket <> -1

        TCPCloseSocket($iListenSocket)

        ; Retrieve the IP Address associated with the accepted socket and assign it to a Local variable.
        Local $sClientIPAddress = SocketToIP($iSocket)

        ; Note: The above function does NOT work with the Listen socket, you can also use it with the socket returned by the TCPConnect function.

        ; Display the sucessful message with the client IP Address.
        MsgBox($MB_SYSTEMMODAL, $sMsgBoxTitle, "Client Connected, IP Address: " & $sClientIPAddress, 3)

        TCPCloseSocket($iSocket)
    EndFunc   ;==>Example

    Func SocketToIP($iSocket)
        Local $tSockAddr = 0, $aRet = 0
        $tSockAddr = DllStructCreate("short;ushort;uint;char[8]")
        $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSocket, "struct*", $tSockAddr, "int*", DllStructGetSize($tSockAddr))
        If Not @error And $aRet[0] = 0 Then
            $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($tSockAddr, 3))
            If Not @error Then Return $aRet[0]
        EndIf
        Return 0
    EndFunc   ;==>SocketToIP

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