windows 查找应用程序使用的 TCP 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6499845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:13:22  来源:igfitidea点击:

Finding TCP ports used by application

windowsapiprocesstcpport

提问by RandomInsano

All right, so I'm extending my company's flexlm vendor daemon to be a little bit more revealing to client applications.

好的,所以我正在扩展我公司的 flexlm 供应商守护程序,以便对客户端应用程序更具启发性。

I need to be able to find out what port lmgrd is listening on before clients connect. The API documentation seems to be rather barren, and I believe they keep most of their code in a compiled form so I can't just look at their source.

我需要能够在客户端连接之前找出 lmgrd 正在侦听的端口。API 文档似乎相当贫乏,我相信他们将大部分代码保存在已编译的形式中,因此我不能只查看其源代码。

Is it possible to call upon the awesome power of the Windows API to find out what ports a particular process is using? If Process Explorer from Sysinternals can do it, I should be able to, right? What would be some sample code for this?

是否可以利用 Windows API 的强大功能来找出特定进程正在使用的端口?如果 Sysinternals 的 Process Explorer 可以做到,我应该也可以,对吧?什么是一些示例代码?

It needs to support Windows XP and higher since many of our clients have yet to upgrade.

它需要支持 Windows XP 及更高版本,因为我们的许多客户尚未升级。

I should note that it turns out FLEX has support for pulling the port from the license file. I don't have the code in front of me, but know that this isn't the best way to find out what ports your vendor daemon/lmgrd is running.

我应该注意到,事实证明 FLEX 支持从许可证文件中提取端口。我面前没有代码,但我知道这不是找出您的供应商守护程序/lmgrd 正在运行的端口的最佳方法。

采纳答案by flumpb

GetTcpTable2 -- see below

GetTcpTable2——见下文

GetTcpTable2 function

GetTcpTable2 函数

The GetTcpTable function retrieves the IPv4 TCP connection table.

GetTcpTable 函数检索 IPv4 TCP 连接表。

This will fill in a MIB_TCPTABLE structure.

这将填充 MIB_TCPTABLE 结构。

typedef struct _MIB_TCPTABLE {
  DWORD      dwNumEntries;
  MIB_TCPROW table[ANY_SIZE];
} MIB_TCPTABLE, *PMIB_TCPTABLE;

And now the MIB_TCPROW

现在 MIB_TCPROW

typedef struct _MIB_TCPROW {
  DWORD dwState;
  DWORD dwLocalAddr;
  DWORD dwLocalPort;
  DWORD dwRemoteAddr;
  DWORD dwRemotePort;
} MIB_TCPROW, *PMIB_TCPROW;

IMPORTANT:

重要

You need to use GetTcpTable2 in order to get the corresponding PID associated as well.

您还需要使用 GetTcpTable2 来获取关联的相应 PID。

typedef struct _MIB_TCPROW2 {
  DWORD                        dwState;
  DWORD                        dwLocalAddr;
  DWORD                        dwLocalPort;
  DWORD                        dwRemoteAddr;
  DWORD                        dwRemotePort;
  DWORD                        dwOwningPid;
  TCP_CONNECTION_OFFLOAD_STATE dwOffloadState;
} MIB_TCPROW2, *PMIB_TCPROW2;

dwOwningPid

dwningPid

回答by RandomInsano

Here's the code I ended up with, for anyone who hits this problem after me

这是我最终得到的代码,对于在我之后遇到这个问题的任何人

#include "stdafx.h"
#include <windows.h>
#include <iphlpapi.h>

// These are just for the ntohl function in the printf below
#include <winsock.h>
#pragma comment(lib, "Ws2_32.lib")

DWORD (WINAPI *pGetExtendedTcpTable)(
  PVOID pTcpTable,
  PDWORD pdwSize,
  BOOL bOrder,
  ULONG ulAf,
  TCP_TABLE_CLASS TableClass,
  ULONG Reserved
);

int _tmain(int argc, _TCHAR* argv[])
{
    MIB_TCPTABLE_OWNER_PID *pTCPInfo;
    MIB_TCPROW_OWNER_PID *owner;
    DWORD size;
    DWORD dwResult;

    HMODULE hLib = LoadLibrary("iphlpapi.dll");

    pGetExtendedTcpTable = (DWORD (WINAPI *)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG))
        GetProcAddress(hLib, "GetExtendedTcpTable");

    if (!pGetExtendedTcpTable)
    {
        printf("Could not load iphlpapi.dll. This application is for Windows XP SP2 and up.\n");
        return 1;
    }

    dwResult = pGetExtendedTcpTable(NULL,     &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0);
    pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)malloc(size);
    dwResult = pGetExtendedTcpTable(pTCPInfo, &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0);

    if (dwResult != NO_ERROR)
    {
        printf("Couldn't get our IP table");
        return 2;
    }

    printf("Iterating though table:\n");
    for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
    {
        owner = &pTCPInfo->table[dwLoop];

        printf("  PID: %5u - Port: %5u\n", owner->dwOwningPid, ntohs(owner->dwLocalPort));
    }

    // Pause a moment
    printf("Done Processing\n");

    return 0;
}

回答by spender

In the worst case, you could always parse the output of:

在最坏的情况下,您始终可以解析以下输出:

netstat -bna