windows网络连接表 根据连接远程的ip端口判断本地程序



#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <winsock.h>
#include <iphlpapi.h>


#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")


#ifdef NTDDI_VERSION
#if(NTDDI_VERSION>=0x0600)

typedef enum {
	TcpConnectionOffloadStateInHost,
	TcpConnectionOffloadStateOffloading,
	TcpConnectionOffloadStateOffloaded,
	TcpConnectionOffloadStateUploading,
	TcpConnectionOffloadStateMax
} TCP_CONNECTION_OFFLOAD_STATE, *PTCP_CONNECTION_OFFLOAD_STATE;



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;



typedef struct _MIB_TCPTABLE2 {
	DWORD dwNumEntries;
	MIB_TCPROW2 table[ANY_SIZE];
} MIB_TCPTABLE2, *PMIB_TCPTABLE2;



typedef DWORD(WINAPI *_InternalGetTcpTable2)(
	PMIB_TCPTABLE2 pTcpTable_Vista,
	PULONG SizePointer,
	BOOL Order
	);
static _InternalGetTcpTable2 pGetTcpTable = NULL;

#endif
#endif
typedef struct tagMIB_TCPEXROW{
	DWORD dwState;              // 连接状态.
	DWORD dwLocalAddr;             // 本地地址.
	DWORD dwLocalPort;           // 本地端口.
	DWORD dwRemoteAddr;            // 远程地址.
	DWORD dwRemotePort;         // 远程端口.
	int dwProcessId;            //进程pid
} MIB_TCPEXROW, *PMIB_TCPEXROW;

typedef struct tagMIB_TCPEXTABLE{
	DWORD dwNumEntries;
	MIB_TCPEXROW table[100];
} MIB_TCPEXTABLE, *PMIB_TCPEXTABLE;


typedef DWORD(WINAPI *PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)(
	PMIB_TCPEXTABLE *pTcpTable,
	BOOL bOrder,
	HANDLE heap,
	DWORD zero,
	DWORD flags
	);
static PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK pAllocateAndGetTcpExTableFromStack = NULL;




#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int filter(char *ip, DWORD port)
{
	DWORD i;
#ifdef NTDDI_VERSION
#if(NTDDI_VERSION>=0x0600)
	PMIB_TCPTABLE2 TCPTable2ForWin7;
#endif
#endif

	ULONG ulSize = 0;
	DWORD dwRetVal = 0;
	char szLocalAddr[128];
	char szRemoteAddr[128];
	PMIB_TCPEXTABLE TCPExTable;
	struct in_addr IpAddr;
	HMODULE hIpDLL = LoadLibraryA("iphlpapi.dll");
	if (!hIpDLL)
	{
		printf("LoadLibrary error!\n");
		return 0;
	}
	int return_code = 0;
	pAllocateAndGetTcpExTableFromStack =
		(PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)
		GetProcAddress(hIpDLL, "AllocateAndGetTcpExTableFromStack");

	//vista
	if (pAllocateAndGetTcpExTableFromStack == NULL)
	{
#ifdef NTDDI_VERSION
#if(NTDDI_VERSION>=0x0600)

		pGetTcpTable = (_InternalGetTcpTable2)GetProcAddress(hIpDLL, "GetTcpTable2");
		if (pGetTcpTable != NULL)
		{
			TCPTable2ForWin7 = (MIB_TCPTABLE2 *)HeapAlloc(GetProcessHeap(), 0, sizeof (MIB_TCPTABLE2));
			ulSize = sizeof (MIB_TCPTABLE);
			if (NULL == TCPTable2ForWin7)
			{
				printf("allocating memory Error\n");
				FreeLibrary(hIpDLL);
				return 0;
			}
			if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) ==
				ERROR_INSUFFICIENT_BUFFER) {
				FREE(TCPTable2ForWin7);
				TCPTable2ForWin7 = (MIB_TCPTABLE2 *)MALLOC(ulSize);
				if (TCPTable2ForWin7 == NULL) {
					printf("Error allocating memory\n");
					FreeLibrary(hIpDLL);
					return 0;
				}
			}

			if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) == NO_ERROR) {
				for (i = 0; i < (int)TCPTable2ForWin7->dwNumEntries; i++) {
					IpAddr.S_un.S_addr = (u_long)TCPTable2ForWin7->table[i].dwRemoteAddr;
					strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));

					if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPTable2ForWin7->table[i].dwRemotePort) == port)
					{
						return_code = TCPTable2ForWin7->table[i].dwOwningPid;
					}
				}
			}
		}
		FREE(TCPTable2ForWin7);
#endif
#endif

	}
	else
	{
		//FALSE or TRUE 表明数据是否排序
		if (pAllocateAndGetTcpExTableFromStack(&TCPExTable, FALSE, GetProcessHeap(), 2, AF_INET))
		{
			printf("AllocateAndGetTcpExTableFromStack Error!\n");
			FreeLibrary(hIpDLL);
			return 0;
		}
		int i;
		for (i = 0; i < TCPExTable->dwNumEntries; i++)
		{
			IpAddr.S_un.S_addr = (u_long)TCPExTable->table[i].dwRemoteAddr;
			strcpy(szRemoteAddr, inet_ntoa(IpAddr));
			if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPExTable->table[i].dwRemotePort) == port)
			{
				return_code = TCPExTable->table[i].dwProcessId;
			}
		}
		FREE(TCPExTable);
	}
	FreeLibrary(hIpDLL);
	return return_code;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char *ip = "47.88.12.92";
	DWORD port = 9528;
	int a = filter(ip, port);
	printf("%d\n", a);
	system("pause");
	return 0;
}




gcc test.c -o test -lws2_32 -liphlpapi



windows编译出现重定义


#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <winsock.h>
#include <iphlpapi.h>

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")

typedef struct tagMIB_TCPEXROW{
	DWORD dwState;              // 连接状态.
	DWORD dwLocalAddr;             // 本地地址.
	DWORD dwLocalPort;           // 本地端口.
	DWORD dwRemoteAddr;            // 远程地址.
	DWORD dwRemotePort;         // 远程端口.
	int dwProcessId;            //进程pid
} MIB_TCPEXROW, *PMIB_TCPEXROW;

typedef struct tagMIB_TCPEXTABLE{
	DWORD dwNumEntries;
	MIB_TCPEXROW table[100];
} MIB_TCPEXTABLE, *PMIB_TCPEXTABLE;


typedef DWORD(WINAPI *PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)(
	PMIB_TCPEXTABLE *pTcpTable,
	BOOL bOrder,
	HANDLE heap,
	DWORD zero,
	DWORD flags
	);

#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
typedef DWORD(WINAPI *_InternalGetTcpTable2)(
	PMIB_TCPTABLE2 pTcpTable_Vista,
	PULONG SizePointer,
	BOOL Order
	);

#endif
#endif
//xp
static PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK pAllocateAndGetTcpExTableFromStack = NULL;
//vista
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
static _InternalGetTcpTable2 pGetTcpTable = NULL;
#endif
#endif
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int filter(char *ip, DWORD port)
{
	DWORD i;
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
	PMIB_TCPTABLE2 TCPTable2ForWin7;
#endif
#endif
	ULONG ulSize = 0;
	DWORD dwRetVal = 0;
	char szLocalAddr[128];
	char szRemoteAddr[128];
	PMIB_TCPEXTABLE TCPExTable;
	struct in_addr IpAddr;
	HMODULE hIpDLL = LoadLibraryA("iphlpapi.dll");
	if (!hIpDLL)
	{
		printf("LoadLibrary error!\n");
		return 0;
	}
	    int return_code = 0;
		pAllocateAndGetTcpExTableFromStack =
		(PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)
		GetProcAddress(hIpDLL, "AllocateAndGetTcpExTableFromStack");

	//vista
	if (pAllocateAndGetTcpExTableFromStack == NULL)
	{
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
		pGetTcpTable = (_InternalGetTcpTable2)GetProcAddress(hIpDLL, "GetTcpTable2");
		if (pGetTcpTable != NULL)
		{
			TCPTable2ForWin7 = (MIB_TCPTABLE2 *)HeapAlloc(GetProcessHeap(), 0, sizeof (MIB_TCPTABLE2));
			ulSize = sizeof (MIB_TCPTABLE);
			if (NULL == TCPTable2ForWin7)
			{
				printf("allocating memory Error\n");
				FreeLibrary(hIpDLL);
				return 0;
			}
			if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) ==
				ERROR_INSUFFICIENT_BUFFER) {
				FREE(TCPTable2ForWin7);
				TCPTable2ForWin7 = (MIB_TCPTABLE2 *)MALLOC(ulSize);
				if (TCPTable2ForWin7 == NULL) {
					printf("Error allocating memory\n");
					FreeLibrary(hIpDLL);
					return 0;
				}
			}

			if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) == NO_ERROR) {
				for (i = 0; i < (int)TCPTable2ForWin7->dwNumEntries; i++) {
					IpAddr.S_un.S_addr = (u_long)TCPTable2ForWin7->table[i].dwRemoteAddr;
					strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));

					if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPTable2ForWin7->table[i].dwRemotePort) == port)
					{
						return_code = TCPTable2ForWin7->table[i].dwOwningPid;
					}
				}
			}
		}
		FREE(TCPTable2ForWin7);
#endif
#endif
	}
	else
	{
		//FALSE or TRUE 表明数据是否排序
		if (pAllocateAndGetTcpExTableFromStack(&TCPExTable, FALSE, GetProcessHeap(), 2, AF_INET))
		{
			printf("AllocateAndGetTcpExTableFromStack Error!\n");
			FreeLibrary(hIpDLL);
			return 0;
		}

		for (int i = 0; i < TCPExTable->dwNumEntries; i++)
		{
			IpAddr.S_un.S_addr = (u_long)TCPExTable->table[i].dwRemoteAddr;
			strcpy(szRemoteAddr, inet_ntoa(IpAddr));
			if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPExTable->table[i].dwRemotePort) == port)
			{
				return_code = TCPExTable->table[i].dwProcessId;
			}
		}
		FREE(TCPExTable);
	}
	FreeLibrary(hIpDLL);
	return return_code;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char *ip = "47.88.12.92";
	DWORD port =9528;
	int a = filter(ip, port);
	printf("%d\n", a);
	system("pause");
	return 0;
}

只有几个结构体的问题


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的微信小程序客户端代码示例,它连接远程服务器并从MySQL数据库中读取数据。 ```javascript //index.js Page({ data: { items: [] }, onLoad: function () { var that = this wx.request({ url: 'http://<server_ip>:<port>/getData', success: function (res) { that.setData({ items: res.data }) } }) } }) ``` 在这个代码中,我们使用了 `wx.request` 方法来向远程服务器发送请求。我们需要将 `<server_ip>` 和 `<port>` 替换为实际的服务器 IP 地址和端口号。 在服务器端,我们可以使用 Node.js 和 Express 框架来创建一个路由,以便客户端可以获取 MySQL 数据。以下是一个示例代码: ```javascript const express = require('express') const mysql = require('mysql') const app = express() const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'my_database' }) connection.connect() app.get('/getData', (req, res) => { const sql = 'SELECT * FROM my_table;' connection.query(sql, (err, result) => { if (err) throw err res.send(result) }) }) app.listen(3000, () => console.log('Server started on port 3000')) ``` 在这个代码中,我们使用了 `mysql` 模块来连接到 MySQL 数据库,并执行一个简单的询来获取中的所有数据。我们还创建了一个 Express 路由,它将在客户端请求 `/getData` 时执行询并返回结果。 请注意,这只是一个简单的示例代码,您需要根据实际情况进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值