查找应用程序使用的 TCP 端口 [英] Finding TCP ports used by application

查看:52
本文介绍了查找应用程序使用的 TCP 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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 XP 及更高版本,因为我们的许多客户尚未升级.

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

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

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.

推荐答案

GetTcpTable2 -- 见下文

GetTcpTable2 -- see below

GetTcpTable2 函数

GetTcpTable 函数检索 IPv4 TCP 连接表.

The GetTcpTable function retrieves the IPv4 TCP connection table.

这将填充一个 MIB_TCPTABLE 结构.

This will fill in a MIB_TCPTABLE structure.

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

现在是 MIB_TCPROW

And now the MIB_TCPROW

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

重要事项:

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

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

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

dwOwningPid

这篇关于查找应用程序使用的 TCP 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆