Jump to content

Socket To IP and port


acmecd
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • 5 months later...

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.

Link to comment
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.

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...