常用的获取端口信息的函数:

GetTcpTable
GetExtendedTcpTable
GetUdpTable
GetExtendedUdpTable

GetTcpTable function

  • 12/05/2018
  • 3 minutes to read

The GetTcpTable function retrieves the IPv4 TCP connection table.

Syntax

C++Copy
ULONG GetTcpTable(
PMIB_TCPTABLE TcpTable,
PULONG SizePointer,
BOOL Order
);

Parameters

TcpTable

A pointer to a buffer that receives the TCP connection table as a MIB_TCPTABLE structure.

SizePointer

On input, specifies the size in bytes of the buffer pointed to by the pTcpTable parameter.

On output, if the buffer is not large enough to hold the returned connection table, the function sets this parameter equal to the required buffer size in bytes.

On the Windows SDK released for Windows Vista and later, the data type for this parameter is changed to a PULONG which is equivalent to a PDWORD.

Order

A Boolean value that specifies whether the TCP connection table should be sorted. If this parameter is TRUE, the table is sorted in the order of:

  1. Local IP address
  2. Local port
  3. Remote IP address
  4. Remote port

Return Value

If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is one of the following error codes.

Return code Description
ERROR_INSUFFICIENT_BUFFER
The buffer pointed to by the pTcpTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the pdwSizeparameter.

This error is also returned if the pTcpTable parameter is NULL.

ERROR_INVALID_PARAMETER
The pdwSize parameter is NULL, or GetTcpTable is unable to write to the memory pointed to by the pdwSize parameter.
ERROR_NOT_SUPPORTED
This function is not supported on the operating system in use on the local system.
STATUS_UNSUCCESSFUL
If you receive this return code then calling the function again is usually enough to clear the issue and get the desired result. This return code can be a consequence of the system being under high load. For example, if the size of the TCP connection table changes by more than 2 additional items 3 consecutive times.
Other
Use FormatMessage to obtain the message string for the returned error.

Remarks

On the Windows SDK released for Windows Vista and later, the return value from the GetTcpTable function is changed to a data type of ULONG which is equivalent to a DWORD.

Examples

The following example retrieves the TCP connection table for IPv4 and prints the state of each connection.

C++Copy
// Need to link with Iphlpapi.lib and Ws2_32.lib
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h> #pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib") #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) /* Note: could also use malloc() and free() */ int main()
{ // Declare and initialize variables
PMIB_TCPTABLE pTcpTable;
DWORD dwSize = ;
DWORD dwRetVal = ; char szLocalAddr[];
char szRemoteAddr[]; struct in_addr IpAddr; int i; pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
if (pTcpTable == NULL) {
printf("Error allocating memory\n");
return ;
} dwSize = sizeof (MIB_TCPTABLE);
// Make an initial call to GetTcpTable to
// get the necessary size into the dwSize variable
if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(pTcpTable);
pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);
if (pTcpTable == NULL) {
printf("Error allocating memory\n");
return ;
}
}
// Make a second call to GetTcpTable to get
// the actual data we require
if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
for (i = ; i < (int) pTcpTable->dwNumEntries; i++) {
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr)); printf("\n\tTCP[%d] State: %ld - ", i,
pTcpTable->table[i].dwState);
switch (pTcpTable->table[i].dwState) {
case MIB_TCP_STATE_CLOSED:
printf("CLOSED\n");
break;
case MIB_TCP_STATE_LISTEN:
printf("LISTEN\n");
break;
case MIB_TCP_STATE_SYN_SENT:
printf("SYN-SENT\n");
break;
case MIB_TCP_STATE_SYN_RCVD:
printf("SYN-RECEIVED\n");
break;
case MIB_TCP_STATE_ESTAB:
printf("ESTABLISHED\n");
break;
case MIB_TCP_STATE_FIN_WAIT1:
printf("FIN-WAIT-1\n");
break;
case MIB_TCP_STATE_FIN_WAIT2:
printf("FIN-WAIT-2 \n");
break;
case MIB_TCP_STATE_CLOSE_WAIT:
printf("CLOSE-WAIT\n");
break;
case MIB_TCP_STATE_CLOSING:
printf("CLOSING\n");
break;
case MIB_TCP_STATE_LAST_ACK:
printf("LAST-ACK\n");
break;
case MIB_TCP_STATE_TIME_WAIT:
printf("TIME-WAIT\n");
break;
case MIB_TCP_STATE_DELETE_TCB:
printf("DELETE-TCB\n");
break;
default:
printf("UNKNOWN dwState value\n");
break;
}
printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
printf("\tTCP[%d] Local Port: %d \n", i,
ntohs((u_short)pTcpTable->table[i].dwLocalPort));
printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);
printf("\tTCP[%d] Remote Port: %d\n", i,
ntohs((u_short)pTcpTable->table[i].dwRemotePort));
}
} else {
printf("\tGetTcpTable failed with %d\n", dwRetVal);
FREE(pTcpTable);
return ;
} if (pTcpTable != NULL) {
FREE(pTcpTable);
pTcpTable = NULL;
} return ;
}

Requirements

   
Minimum supported client Windows 2000 Professional [desktop apps | UWP apps]
Minimum supported server Windows 2000 Server [desktop apps | UWP apps]
Target Platform Windows
Header iphlpapi.h
Library Iphlpapi.lib
DLL Iphlpapi.dll

vc枚举本机端口信息API的更多相关文章

  1. vc枚举本机端口信息

    关于查看本机端口信息,可能大多数人都知道在cmd下的netstat 命令,殊不知该命令在底层也是调用相关api来实现的,相关函数有:GetTcpTableGetExtendedTcpTableGetU ...

  2. 利用DescriptionAttribute定义枚举值的描述信息 z

    System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类 ...

  3. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  4. linux下用iptables做本机端口转发方法(转载)

    一 :从一台机到另一台机端口转发 启用网卡转发功能 #echo 1 > /proc/sys/net/ipv4/ip_forward 举例:从192.168.0.132:21521(新端口)访问1 ...

  5. iptables 从一台机到另一台机端口转发

    启用网卡转发功能#echo 1 > /proc/sys/net/ipv4/ip_forward 举例:从192.168.0.132:21521(新端口)访问192.168.0.211:1521端 ...

  6. Docker容器内部端口映射到外部宿主机端口的方法小结

    转自:https://www.cnblogs.com/kevingrace/p/9453987.html Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务.容器启动之后,容器中可以 ...

  7. html5获取地理位置信息API

    html5获取地理位置信息API 在HTML5中,可以看下如何使用Geolocation API来获得用户的地理位置信息,如果该浏览器支持的话,且设备具有定位功能,就能够直接使用这组API来获取当前位 ...

  8. 写了个TP5下PHP和手机端通信的API接口校验

    写了个PHP和手机端通信的API接口校验 直接发函数吧 public function _initialize() { //定义密码和盐 $password="123456"; $ ...

  9. 微信小程序把玩(三十八)获取设备信息 API

    原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...

随机推荐

  1. 【198】Synergy - 鼠标键盘共享软件

    参考:Synergy X64 v1.7.4 官方最新版 参考:Synergy安装方法 功能介绍: 可以将配置局域网的电脑实现同一个鼠标键盘控制两台电脑,效果类似一台电脑使用双屏的效果,键盘会根据鼠标的 ...

  2. HDU 1207 汉诺塔II (简单DP)

    题意:中文题. 析:在没有第四个柱子时,把 n 个盘子搬到第 3 个柱子时,那么2 ^ n -1次,由于多了一根,不知道搬到第四个柱子多少根时是最优的, 所以 dp[i] 表示搬到第4个柱子 i 个盘 ...

  3. Gym 100962G Green Day (找规律)

    题意:你用k 个生成树构成一个完全图. 析:n 个点的完全图有n(n-1)/2个边,一个生成树有n-1个边,你有k 个生成树 即边数等于 K(n-1) ,即  n(n-1)/2 == k(n-1)   ...

  4. find 是区分大小写的。对于不区分大小写的写法(转载)

    转自:http://justwinit.cn/post/3633/ 默认情况下,find 是区分大小写的.对于不区分大小写的 find,将 -iname 测试替换为 -name 测试. find do ...

  5. ORACLE PL/SQL 实例精解之第七章 迭代控制之二

    7.1CONTINUE语句 CONTINUE语句会导致循环终止当前迭代.并且当CONTINUE的条件为TRUE时,开始执行该循环的下一次迭代.需要借助于IF语句来计算COUNTINUE条件.当CONT ...

  6. 张高兴的 .NET Core IoT 入门指南:(三)使用 I2C 进行通信

    什么是 I2C 总线 I2C 总线(Inter-Integrated Circuit Bus)是设备与设备间通信方式的一种.它是一种串行通信总线,由飞利浦公司在1980年代为了让主板.嵌入式系统或手机 ...

  7. 洛谷 - P2045 - 方格取数加强版 - 费用流

    原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...

  8. SDUT2161:Simple Game(NIM博弈+巴什博弈)

    传送门 题意 n堆石子,每次可以取一堆至三堆任意非零石子数,取完者赢,问最后谁能赢 分析 以前我们做过NIM博弈是对一堆进行操作,现在换成了三堆,其实对于n堆都一样一堆的情况 如果最后二进制每位数的1 ...

  9. python __builtins__ copyright类 (14)

    14.'copyright', 版权 class _Printer(builtins.object) | interactive prompt objects for printing the lic ...

  10. 推荐一个比较专业的EasyUI中文网

    jQuery EasyUI中文网:http://www.jeasyui.cn,对EasyUI官网的完全整理翻译! 一个基于EasyUI构建的强大前端框架:TopJUI前端框架 致力于打造既美观实用又能 ...