Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

C++检测本地网络端口占用

$
0
0

一、解决方法:

1、使用DOS netstat 命令查询所有端口使用情况
2、使用DOS findstr 命令辅助筛选符合要求的进程PID
3、使用DOS tasklist 命令查询PID对应的进程信息
4、使用DOS findstr 命令辅助筛选符合要求的进程名
5、在VC中执行DOS命令
WinExec 异步执行。不能等待命令结束,较简单
ShellExecute 麻烦
CreateProcess 麻烦
注:使用任何一种方法,都需要将结果输出到外部,然后再读取结果分析


二、DOS查询端口使用示例

比如要查看8080端口被哪个程序占用了,windows命令行窗口下执行:运行--cmd


C:\>netstat -aon|findstr ":8080 " ,输出
TCP 127.0.0.1:80 0.0.0.0:0 LISTENING 2448


端口被进程号为2448的进程占用,继续执行下面命令:


C:\>tasklist /fi "pid eq 2448" /nh
thread.exe 2016 Console 0 16,064 K


表示thread.exe程序占用了端口8080


三、windows下VC实现代码

#include <windows.h>
#include <string>
using namespace std;

//
//根据端口查询进程名,如果有多个进程,只返回第一个
//
bool GetProcNameByPort(int nPort, string &strResult)
{
	bool bSuc = false;
	char pszPort[16] = {0};
	itoa(nPort, pszPort, 10);
	char pResult[80] = {0};
	const char* pPortFilePath = "c:\\~vtmp";
	const char* pProcessFilePath = "c:\\~vvtmp";
	sprintf(pResult, "cmd /c netstat -ano|findstr \":%d \" > %s",  nPort, pPortFilePath);

	//WinExec 执行cmd命令
	WinExec(pResult, SW_HIDE);
	Sleep(450);

	//查找端口号
	FILE *pPortFile = fopen(pPortFilePath, "r");
	if ( pPortFile )
	{
		while ( !feof(pPortFile) )
		{
			memset(pResult, 0, sizeof(pResult));
			fread(pResult, sizeof(pResult), 1, pPortFile);
			pResult[sizeof(pResult)-1] = 0x00;

			string strPortTmp = pResult;
			int offset = (int)strPortTmp.find_last_of(0x0A);
			if ( offset > -1 )
			{
				pResult[offset] = 0x00;
				strPortTmp = strPortTmp.substr(0, offset);
				if ( !feof(pPortFile) )
				{
					fseek(pPortFile, (long)(strPortTmp.length()+1-sizeof(pResult)), SEEK_CUR);
				}

				offset = (int)strPortTmp.find_first_of(':');
				if ( offset > -1 )
				{
					strPortTmp = strPortTmp.substr(offset+1, 6);
					offset = (int)strPortTmp.find_last_not_of(' ');
					if ( offset > -1 )
					{
						strPortTmp = strPortTmp.substr(0, offset+1);
						if ( strPortTmp == pszPort )
						{
							strPortTmp = pResult;
							offset = (int)strPortTmp.find_last_of(' ');
							if ( offset > -1 )
							{
								strPortTmp = strPortTmp.substr(offset+1);
								sprintf(pResult, "cmd /c tasklist /fi \"pid eq %s\" /nh> %s", strPortTmp.c_str(), pProcessFilePath);
								//根据端口号查找进程ID
								WinExec(pResult, SW_HIDE);
								Sleep(450);

								FILE *pProcessFile = fopen(pProcessFilePath, "r");
								if ( pProcessFile )
								{
									while (!feof(pProcessFile))
									{
										memset(pResult, 0, sizeof(pResult));
										fread(pResult, sizeof(pResult), 1, pProcessFile);
										pResult[sizeof(pResult)-1] = 0x00;

										string strProcessTmp = pResult;
										int offset = (int)strProcessTmp.find_last_of(0x0A);
										if ( offset > -1 )
										{
											pResult[offset] = 0x00;
											strProcessTmp = strProcessTmp.substr(0, offset);
											if ( !feof(pProcessFile) )
											{
												fseek(pProcessFile, (long)(strProcessTmp.length()+1-sizeof(pResult)), SEEK_CUR);
											}

											if ( 0x0A == pResult[0] )		//首行只有一个字符 0x0A
											{
												strProcessTmp = pResult+1;
											}
											else
											{
												strProcessTmp = pResult;
											}
											offset = (int)strProcessTmp.find_first_of(' ');
											if ( offset > -1 )
											{
												{
													{
														{
															{
																strProcessTmp = strProcessTmp.substr(0, offset);
																if ( "" != strProcessTmp )
																{
																	//查找成功,结束
																	strResult += "[" + strProcessTmp + "]";
																	bSuc = true;
																}
																continue;
															}
														}
													}
												}
											}
										}
									}
									fclose(pProcessFile);
								}
								sprintf(pResult, "cmd /c del %s", pProcessFilePath);
								WinExec(pResult, SW_HIDE);
								if(bSuc){ continue; }
							}
						}
					}
				}
			}
		}
		fclose(pPortFile);
	}
	if(!bSuc){ strResult=""; };
	sprintf(pResult, "cmd /c del %s", pPortFilePath);
	WinExec(pResult, SW_HIDE);
	return bSuc;
}

int main()
{
	int count = 100;
	string str = "";

	while(count--)
	{
		str = "";
		GetProcNameByPort(843, str);
		if ( str != "" )
			printf("_%s_\n", str.c_str());
		Sleep(1000);
	}

	printf("____End____");


	getchar();
	return 0;
}

转载请注明来自Master.R(石硕)的CSDN博客:blog.csdn.net/shishuo365  如有疑问请发邮件shishuo365#126.com(将#更换为@)
作者:shishuo365 发表于2013-5-17 11:16:42 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>