GetUdpTable获得UDP端口使用信息

函数原型参考https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa366033(v=vs.85)

本文主要使用windows系统提供的GetUdpTable函数来获得指定端口是否被UDP使用,TCP端口使用情况类似,GetTcpTable函数可以实现。


// PortMonitoring.h: interface for the CPortMonitoring class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PORTMONITORING_H__A8CBE1A3_0489_4DA0_862F_F3CDFFE4AB9C__INCLUDED_)
#define AFX_PORTMONITORING_H__A8CBE1A3_0489_4DA0_862F_F3CDFFE4AB9C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

typedef unsigned long DWORD;

class CPortMonitoring
{
public:
	CPortMonitoring();
	virtual ~CPortMonitoring();

	//检测端口是否被占用
	static bool CheckUdpPort(unsigned int nPort);
};

#endif // !defined(AFX_PORTMONITORING_H__A8CBE1A3_0489_4DA0_862F_F3CDFFE4AB9C__INCLUDED_)



// PortMonitoring.cpp: implementation of the CPortMonitoring class.
//
//////////////////////////////////////////////////////////////////////

#include 
#include   "windows.h"
#include "PortMonitoring.h"

#define		ANY_SIZE	 120

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
//#define new DEBUG_NEW     //DEBUG_NEW可以定位内存泄露
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPortMonitoring::CPortMonitoring()
{
}

CPortMonitoring::~CPortMonitoring()
{
}

////连接信息结构体
typedef struct _MIB_UDPROW
{
	DWORD dwLocalAddr;           // 本地计算机地址
	DWORD dwLocalPort;           // 本地计算机端口
	//int dwProcessId;             //进程id
} MIB_UDPROW, *PMIB_UDPROW;

////TCP连接表
typedef struct _MIB_UDPTABLE
{
	DWORD      dwNumEntries;				//当前包含MIB_UDPROW类型的总数
	MIB_UDPROW table[ANY_SIZE];             //存放每个UDP连接的端口和IP
} MIB_UDPTABLE, *PMIB_UDPTABLE;

//检测端口是否被占用
bool CPortMonitoring::CheckUdpPort(unsigned int nPort)
{
	if (nPort < 1024 || nPort > 65000)
		return false;

	PMIB_UDPTABLE pudptable = NULL;
	DWORD dwSize = 0;

	HINSTANCE hInst;		//动态链接库模块句柄
	hInst = LoadLibraryA("iphlpapi.dll");		//动态加载iphlpapi.dll

	//定义函数指针类型
	typedef long(__stdcall *ADDPROC) (PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder);

	//获取iphlpapi.dll导出函数
	ADDPROC GetUdpTable = (ADDPROC)GetProcAddress(hInst, "GetUdpTable");

	if (GetUdpTable(pudptable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)		//pTcpTable空间不足
	{
		pudptable = new MIB_UDPTABLE[dwSize];		//为pUdpTable申请足够的空间

		if (GetUdpTable(pudptable, &dwSize, TRUE) == NO_ERROR)     //GetTcpTable调用成功
		{
			//检测端口nPort是否在
			for (UINT i = 0; i < pudptable->dwNumEntries; i++)
			{
				if (nPort != ntohs(pudptable->table[i].dwLocalPort))
					continue;

				//释放资源
				delete[] pudptable;
				FreeLibrary((HMODULE)hInst);

				return FALSE;
			}
		}
	}

	//释放资源
	delete[] pudptable;
	FreeLibrary((HMODULE)hInst);

	return TRUE;
}

下面是加上TCP检测的:

#include 
#include "windows.h"
#include "port_detect_interface.h"

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

PortDetectImpl::PortDetectImpl()
{
}

PortDetectImpl::~PortDetectImpl()
{
}

/**
* 检测UDP端口是否被占用
*
* @param [in]  port    待检测端口号
*
* @return bool: 0 未占用,1 占用
*/
bool PortDetectImpl::CheckUdpPort(unsigned int port)
{
	if (port < 1024 || port > 65535)
		return TRUE;

	PMIB_UDPTABLE pudptable = NULL;
	DWORD dwSize = 0;

	HINSTANCE hInst;		//动态链接库模块句柄
	hInst = LoadLibraryA("iphlpapi.dll");		//动态加载iphlpapi.dll

	//定义函数指针类型
	typedef long(__stdcall *ADDPROC) (PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder);

	//获取iphlpapi.dll导出函数
	ADDPROC GetUdpTable = (ADDPROC)GetProcAddress(hInst, "GetUdpTable");

	if (GetUdpTable(pudptable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)		//pTcpTable空间不足
	{
		pudptable = new MIB_UDPTABLE[dwSize];		//为pTcpTable申请足够的空间

		if (GetUdpTable(pudptable, &dwSize, TRUE) == NO_ERROR)     //GetTcpTable调用成功
		{
			//检测端口nPort是否在
			for (UINT i = 0; i < pudptable->dwNumEntries; i++)
			{
				if (port != ntohs(pudptable->table[i].dwLocalPort))
					continue;

				//释放资源
				delete[] pudptable;
				FreeLibrary((HMODULE)hInst);

				return TRUE;
			}
		}
	}
	//释放资源
	delete[] pudptable;
	FreeLibrary((HMODULE)hInst);

	return FALSE;
}

/**
* 检测TCP端口是否被占用
*
* @param [in]  port    待检测端口号
*
* @return bool: 0 未占用,1 占用
*/
bool PortDetectImpl::CheckTcpPort(unsigned int port)
{
	if (port < 1024 || port > 65535)
	{
		return TRUE;
	}

	PMIB_TCPTABLE ptcptable = NULL;
	DWORD dwSize = 0;
	HINSTANCE hInst;		//动态链接库模块句柄
	hInst = LoadLibraryA("iphlpapi.dll");		//动态加载iphlpapi.dll

	//定义函数指针类型
	typedef long(__stdcall *ADDPROC) (PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder);
	
	//获取iphlpapi.dll导出函数
	ADDPROC GetTcpTable = (ADDPROC)GetProcAddress(hInst, "GetTcpTable");

	if (GetTcpTable(ptcptable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)		//pTcpTable空间不足
	{
		ptcptable = new MIB_TCPTABLE[dwSize];		//为pTcpTable申请足够的空间

		if (GetTcpTable(ptcptable, &dwSize, TRUE) == NO_ERROR)     //GetTcpTable调用成功
		{
			//检测端口nPort是否在
			for (UINT i = 0; i < ptcptable->dwNumEntries; i++)
			{
				if (port != ntohs(ptcptable->table[i].dwLocalPort))
					continue;

				//释放资源
				delete[] ptcptable;
				FreeLibrary((HMODULE)hInst);

				return TRUE;
			}
		}
	}
	//释放资源
	delete[] ptcptable;
	FreeLibrary((HMODULE)hInst);
	
	return FALSE;
}


你可能感兴趣的:(MyInterface)