notsure

TCP get remote IP address?

2 posts in this topic

#1 ·  Posted (edited)

Hello, 

 

I was wondering, if it is possible to get the IP address which is connecting to the socket?

 

Like;

 

    TCPStartup()
    Local $sIPAddress = "10.1.1.1" ; This IP Address only works for testing on your own computer.
    Local $iPort = 4500
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0
     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
     
    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.

 

Socket will be <> -1 when somebody connects, but is it possible to determine which IP address is connecting? The socketstate is winsock i guess...

 

Haven't done AU3 for a while, be easy on me :P

Edited by notsure

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hey NotSure,

I didn't write this command, And unfortunately can't remember where on the forums I found it, so can't comment on the original author, But (see edit) this will achieve what you're looking for:

Func SocketToIP($iSock, $hDLL = "Ws2_32.dll") ;A rewrite of that _SocketToIP function that has been floating around for ages
    Local $structName = DllStructCreate("short;ushort;uint;char[8]")
    Local $sRet = DllCall($hDLL, "int", "getpeername", "int", $iSock, "ptr", DllStructGetPtr($structName), "int*", DllStructGetSize($structName))
    If Not @error Then
        $sRet = DllCall($hDLL, "str", "inet_ntoa", "int", DllStructGetData($structName, 3))
        If Not @error Then Return $sRet[0]
    EndIf
    Return "0.0.0.0" ;Something went wrong, return an invalid IP
EndFunc

So for your script put:
 

TCPStartup()
    Local $sIPAddress = "10.1.1.1" ; This IP Address only works for testing on your own computer.
    Local $iPort = 4500
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0
     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
     
    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.
    $sIP = SocketToIp($iSocket) ;<<<<<<<<

 

EDIT: Source is Fast Multi Client TCP server by Kealper

Edited by javiwhite

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now