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

废话就不多说了,开始。。。

    

一、解决方法:

    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(将#更换为@)

文章结束给大家分享下程序员的一些笑话语录: 火车
一个年轻的程序员和一个项目经理登上了一列在山里行驶的火车,他们发现 列车上几乎都坐满了,只有两个在一起的空位,这个空位的对面是一个老奶 奶和一个年轻漂亮的姑娘。两个上前坐了下来。程序员和那个姑娘他们比较 暧昧地相互看对方。这时,火车进入山洞,车厢里一片漆黑。此时,只听见 一个亲嘴的声音,随后就听到一个响亮的巴掌声。很快火车出了山洞,他们 四个人都不说话。
那个老奶奶在喃喃道, “这个年轻小伙怎么这么无礼, 不过我很高兴我的孙女 扇了一个巴掌”。
项目经理在想,“没想到这个程序员居然这么大胆,敢去亲那姑娘,只可惜那 姑娘打错了人,居然给打了我。”
漂亮的姑娘想,“他亲了我真好,希望我的祖母没有打疼他”。
程序员坐在那里露出了笑容, “生活真好啊。 这一辈子能有几次机会可以在亲 一个美女的同时打项目经理一巴掌啊”

原文地址:https://www.cnblogs.com/jiangu66/p/3086057.html