vc枚举本机端口信息API
常用的获取端口信息的函数:
GetTcpTable
GetExtendedTcpTable
GetUdpTable
GetExtendedUdpTable
- GetTcp6Table function
- GetTcp6Table2 function
- GetTcpStatistics function
- GetTcpStatisticsEx function
- GetTcpStatisticsEx2 function
- GetTcpTable
- GetTcpTable2 function
- GetUdp6Table function
- GetUdpStatistics function
- GetUdpStatisticsEx function
- GetUdpStatisticsEx2 function
- GetUdpTable function
GetTcpTable function
- 12/05/2018
- 3 minutes to read
The GetTcpTable function retrieves the IPv4 TCP connection table.
Syntax
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:
- Local IP address
- Local port
- Remote IP address
- 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 |
---|---|
|
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. |
|
The pdwSize parameter is NULL, or GetTcpTable is unable to write to the memory pointed to by the pdwSize parameter. |
|
This function is not supported on the operating system in use on the local system. |
|
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. |
|
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.
// 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的更多相关文章
- vc枚举本机端口信息
关于查看本机端口信息,可能大多数人都知道在cmd下的netstat 命令,殊不知该命令在底层也是调用相关api来实现的,相关函数有:GetTcpTableGetExtendedTcpTableGetU ...
- 利用DescriptionAttribute定义枚举值的描述信息 z
System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类 ...
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- linux下用iptables做本机端口转发方法(转载)
一 :从一台机到另一台机端口转发 启用网卡转发功能 #echo 1 > /proc/sys/net/ipv4/ip_forward 举例:从192.168.0.132:21521(新端口)访问1 ...
- iptables 从一台机到另一台机端口转发
启用网卡转发功能#echo 1 > /proc/sys/net/ipv4/ip_forward 举例:从192.168.0.132:21521(新端口)访问192.168.0.211:1521端 ...
- Docker容器内部端口映射到外部宿主机端口的方法小结
转自:https://www.cnblogs.com/kevingrace/p/9453987.html Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务.容器启动之后,容器中可以 ...
- html5获取地理位置信息API
html5获取地理位置信息API 在HTML5中,可以看下如何使用Geolocation API来获得用户的地理位置信息,如果该浏览器支持的话,且设备具有定位功能,就能够直接使用这组API来获取当前位 ...
- 写了个TP5下PHP和手机端通信的API接口校验
写了个PHP和手机端通信的API接口校验 直接发函数吧 public function _initialize() { //定义密码和盐 $password="123456"; $ ...
- 微信小程序把玩(三十八)获取设备信息 API
原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...
随机推荐
- webpack 工作方式
把你的项目当做一个整体,通过一个给定的主文件(如index.js),webpack将从这个文件开始找到你的项目的所有依赖文件,使用loaders处理它们,最后打包为一个或多个 浏览器可识别额javas ...
- OkHttp解析
今天花了一天时间研究了下OkHttp3的内部原理,记录在此处以便后期查阅 我们先来看下基本的使用方式: public void sendHttpRequest(String url,Callback ...
- Codeforces - 1033C - Permutation Game - 简单dp - 简单数论
https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...
- (水题)洛谷 - P1051 - 谁拿了最多奖学金
https://www.luogu.org/problemnew/show/P1051 这个根本就不用排序啊…… #include<bits/stdc++.h> using namespa ...
- sublime text3 注册码
—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- 《算法竞赛进阶指南》1.6Trie
142. 前缀统计 给定N个字符串S1,S2-SN,接下来进行M次询问,每次询问给定一个字符串T,求S1-SN中有多少个字符串是T的前缀. 输入字符串的总长度不超过106,仅包含小写字母. 输入格式 ...
- 使用Spring MVC的@RequestBody注解接收Json对象字符串
最近公司在开发移动APP,APP上通过jQuery提交表单的json字符串格式数据到Java后端,之前通过request手动接收,非常麻烦,其实Spring MVC已经为我们提供了一个注解@Reque ...
- windows 异步通知I/O模型与重叠I/O模型
一.异步IO模型(asynchronous IO) (1)什么是异步I/O 异步I/O(asynchronous I/O)由POSIX规范定义.演变成当前POSIX规范的各种早起标准所定义的实时函数中 ...
- 跟我一起玩Win32开发(17):启动和结束进程
这里我再次说明一下,我不知道为什么,现在的人那么喜欢走极端,估计是价值观都“升级”了的缘故吧. 我撰写这一系列Win32相关的文章,并不是叫大家一定要用Win32去开发项目,仅仅是给大家了解一下,Wi ...
- Educational Codeforces Round 18 A
Description There are n cities situated along the main road of Berland. Cities are represented by th ...