Jump to content

Getting Connected IP and Port info


Zaxon
 Share

Recommended Posts

I'm trying to get the IP address, port #, and possibly even do a lookup to resolve the name of the remote peer that you're connected with via TCP/IP (using the inbuild AutoIt TCP connection commands). This needs to work on both the client and server sides. I can do this with PERL quite easily, but I'm trying do replicate that functionality with AutoIt.

The SocketToIP() function given in the AutoIt help doesn't seem to work, even after I modify it because int_ptr is now a deprecated type. In addition, it never gave the port #. I've combed the forums for this info and read countless posts, but haven't seen mention of how to get this basic info about your connection.

Link to comment
Share on other sites

@Zaxon

OK it"s external but what is the difference if you use an external DLLCall or an external EXE

both return the information :

#include <Constants.au3>

$sCommand= Run("netstat -a", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($sCommand)
    If @error Then ExitLoop
    ConsoleWrite( "STDOUT read:" & $line & @LF)
Wend

While 1
    $line = StderrRead($sCommand)
    If @error Then ExitLoop
    ConsoleWrite( "STDERR read:" & $line & @LF) 
Wend

regards

ptrex

Link to comment
Share on other sites

OK it"s external but what is the difference if you use an external DLLCall or an external EXE

both return the information

External programs start a whole new process - an expensive operation in windows. Certainly in compiled or linked languages, you don't have this overhead with .dlls. I'm not sure how DllCall is treated in AutoIt, whether the autoit.exe program links to the dll directly from the process, or whether a new process entirely is launched.

With the commands you've given, and thank you for what you've provided so far :P , netstat -a gives a list of every connection on your system. I could string parse that to find the details I need I presume, but that's a far cry from what I'm expecting to do. In PERL, I can interrogate the connection object of my current connection directly and it will happy tell me the IP and port of my current connection. No need for external commands or parsing down a couple of pages of netstat output to find the one thing I'm interested in.

The SocketToIP($SHOCKET) used to work (I'm assuming). I've changed one of the types from int_ptr to int, as directed by the output I received, but I'm currently just get "0" return. Plus, a port was never returned, though I'm sure it's just another dll call away.

Link to comment
Share on other sites

  • 1 year later...

This is still actual for me. Could any body help?

Something is wrong here:

Func SocketToPort($SHOCKET)
    Local $sockaddr, $aRet

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

    $aRet = DllCall("Ws2_32.dll", "int", "getnameinfo", "int", $SHOCKET, _
            "int", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "int", "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

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...