Function Reference

首页  后退  前进

TCPAccept

 

允许套接字(socket)的尝试连接.

 

TCPAccept ( mainsocket )

参数

mainsocket

使用 TCPListen() 函数返回的套接字标识(SocketID)

返回值

成功:

返回连接套接字的标识符.

失败:

返回 -1, @error 设置 为非 0 值.

@error:

-2 未连接.

Windows API WSAGetError 返回的值 (见 MSDN)

相关

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

函数示例

示例 1

#include <MsgBoxConstants.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 connexions
    ;(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(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "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 connexions 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(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not accept the incoming connection, Error code: " & $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

示例 2

#include <MsgBoxConstants.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()
    TCPStartup()
    OnAutoItExitRegister("OnAutoItExit")
    Local $sIPAddress = "127.0.0.1"
    Local $iPort = 65432
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0
    If @error Then
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not listen, Error code: " & $iError)
        Return False
    EndIf
    Local $iSocket = 0
    Do
        $iSocket = TCPAccept($iListenSocket)
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not accept the incoming connection, Error code: " & $iError)
            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, "", "Client Connected, IP Address: " & $sClientIPAddress)
    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

----------------------------------------