Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4077306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:32:02+00:00 2026-05-20T17:32:02+00:00

Lets assume you have a app that opens a socket port for communication purposes.

  • 0

Lets assume you have a app that opens a socket port for communication purposes. How can I get the path of this app only by knowing its port?

I want to do what netstat -b does. It lists all socket ports opened and the app that opened the socket.

I am using delphi 2010.
By knowing which app opened which port I am able to kill the app.

Note that I need a delphi code, not an Dos command or an explanation of how to use netstat.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:32:02+00:00Added an answer on May 20, 2026 at 5:32 pm

    Rafael, you can use the GetExtendedTcpTable function, this function retrieves a table that contains a list of TCP connections availables.

    first you must inspect the records returned by this function, and check the dwLocalPortor dwRemotePort (depending of what port your need to check), then you can get the pid of the application checking the dwOwningPid field and resolve the exe name using a windows api function like GetModuleFileNameEx

    Check this sample application which show all tcp connections like netstat. you can modify this sample to fit with your requirements.

    uses
          PsAPI,
          WinSock,
          Windows,
          SysUtils;
    
        const
           ANY_SIZE = 1;
           iphlpapi = 'iphlpapi.dll';
           TCP_TABLE_OWNER_PID_ALL = 5;
    
           MIB_TCP_STATE:
           array[1..12] of string = ('CLOSED', 'LISTEN', 'SYN-SENT ','SYN-RECEIVED', 'ESTABLISHED', 'FIN-WAIT-1',
                                     'FIN-WAIT-2', 'CLOSE-WAIT', 'CLOSING','LAST-ACK', 'TIME-WAIT', 'delete TCB');
    
        type
           TCP_TABLE_CLASS = Integer;
    
          PMibTcpRowOwnerPid = ^TMibTcpRowOwnerPid;
          TMibTcpRowOwnerPid  = packed record
            dwState     : DWORD;
            dwLocalAddr : DWORD;
            dwLocalPort : DWORD;
            dwRemoteAddr: DWORD;
            dwRemotePort: DWORD;
            dwOwningPid : DWORD;
            end;
    
    
          PMIB_TCPTABLE_OWNER_PID  = ^MIB_TCPTABLE_OWNER_PID;
          MIB_TCPTABLE_OWNER_PID = packed record
           dwNumEntries: DWord;
           table: array [0..ANY_SIZE - 1] OF TMibTcpRowOwnerPid;
          end;
    
        var
           GetExtendedTcpTable:function  (pTcpTable: Pointer; dwSize: PDWORD; bOrder: BOOL; lAf: ULONG; TableClass: TCP_TABLE_CLASS; Reserved: ULONG): DWord; stdcall;
    
    
    
    
        function GetPathPID(PID: DWORD): string;
        var
          Handle: THandle;
        begin
          Result := '';
          Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
          if Handle <> 0 then
            try
              SetLength(Result, MAX_PATH);
                if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
                  SetLength(Result, StrLen(PChar(Result)))
                else
                  Result := '';
            finally
              CloseHandle(Handle);
            end;
        end;
    
    
        procedure ShowCurrentTCPConnections;
        var
           Error        : DWORD;
           TableSize    : DWORD;
           i            : integer;
           IpAddress    : in_addr;
           RemoteIp     : string;
           LocalIp      : string;
           FExtendedTcpTable : PMIB_TCPTABLE_OWNER_PID;
        begin
          TableSize := 0;
          Error := GetExtendedTcpTable(nil, @TableSize, False, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
          if Error <> ERROR_INSUFFICIENT_BUFFER then
             Exit;
    
          GetMem(FExtendedTcpTable, TableSize);
          try
           if GetExtendedTcpTable(FExtendedTcpTable, @TableSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0) = NO_ERROR then
              for i := 0 to FExtendedTcpTable.dwNumEntries - 1 do
              if {(FExtendedTcpTable.Table[i].dwOwningPid=Pid) and} (FExtendedTcpTable.Table[i].dwRemoteAddr<>0) then //here you can check the particular port
              begin
                 IpAddress.s_addr := FExtendedTcpTable.Table[i].dwRemoteAddr;
                 RemoteIp  := string(inet_ntoa(IpAddress));
                 IpAddress.s_addr := FExtendedTcpTable.Table[i].dwLocalAddr;
                 LocalIp          := string(inet_ntoa(IpAddress));
                 Writeln(GetPathPID(FExtendedTcpTable.Table[i].dwOwningPid));
                 Writeln(Format('%-16s %-6d %-16s %-6d %s',[LocalIp,FExtendedTcpTable.Table[i].dwLocalPort,RemoteIp,FExtendedTcpTable.Table[i].dwRemotePort,MIB_TCP_STATE[FExtendedTcpTable.Table[i].dwState]]));
              end;
          finally
             FreeMem(FExtendedTcpTable);
          end;
        end;
    
        var
           libHandle : THandle;
        begin
          try
            ReportMemoryLeaksOnShutdown:=DebugHook<>0;
            libHandle           := LoadLibrary(iphlpapi);
            GetExtendedTcpTable := GetProcAddress(libHandle, 'GetExtendedTcpTable');
            ShowCurrentTCPConnections;
          except
            on E: Exception do
              Writeln(E.ClassName, ': ', E.Message);
          end;
    
          readln;
        end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume we have this xml: <?xml version=1.0 encoding=UTF-8?> <tns:RegistryResponse status=urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure xmlns:tns=urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0 xmlns:rim=urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0> <tns:RegistryErrorList
(code examples are python) Lets assume we have a list of percentages that add
Lets consider next scenario: assume I have a web app, and authentication of users
Lets assume i have a function that takes 32bit integer in, and returns random
Can I convert SVG image paths to Image map coordinates? Lets assume that I
Lets assume I have created a facebook app.So all those who(Admin) have facebook fan
Lets assume we have a table with a fulltext field on it. This field
Or vice versa. Update: Hmm, let's assume I have a shopping cart app, the
Let's assume I have a model called product. Let's assume that product has three
Lets assume that I'm dealing with a service that involves sending large amounts of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.