Sign in to follow this  
Followers 0
acmecd

Socket To IP and port

6 posts in this topic

There is very nice well know script to get Socket To IP, that’s work good.

But, I need get with connected IP also remote port, (IP:Port) there are any ideas how to upgrade script.

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP

Share this post


Link to post
Share on other sites



I'm going to go ahead and bump this, as it is exactly the same question I was going to ask.  I'd like to know how I can retrieve the client's port.

Share this post


Link to post
Share on other sites

Give me just a bit more information what exactly you have and what exactly you want to achieve.

For example you have opened a connection to a server and want to know what local port is opened or what?

With a little bit more details I'm sure I can help you (if I have time ;)).


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Yes, that is correct.  I would like to know which local port is in use by the connected socket.

Share this post


Link to post
Share on other sites

I wrote this function a few years ago:

Func _Ws2_GetLocalPort($iSocket)
    ;funkey
    Local $hDLL = DllOpen("Ws2_32.dll")
    Local $tagIN_ADDR = "ulong S_addr;"
    Local $tagSOCKADDR_IN = "short sin_family;ushort sin_port;ptr sin_addr;char sin_zero[8];"
    Local $tIn_Addr = DllStructCreate($tagIN_ADDR)
    Local $tName = DllStructCreate($tagSOCKADDR_IN)
    DllStructSetData($tName, "sin_addr", DllStructGetPtr($tIn_Addr))

    Local $aRes = DllCall($hDLL, "int", "getsockname", "uint", $iSocket, "struct*", $tName, "int*", 512)
    Local $aRes2 = DllCall($hDLL, "ushort", "ntohs", "ushort", DllStructGetData($tName, 2))

    DllClose($hDLL)
    Return $aRes2[0]
EndFunc

Hope this is what you want.

1 person likes this

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Funkey, that was exactly what I was looking for.  Thank you.

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
Sign in to follow this  
Followers 0