命令端口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);//根据端口号查找进程IDWinExec(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(将#更换为@)
文章结束给大家分享下程序员的一些笑话语录: 火车
一个年轻的程序员和一个项目经理登上了一列在山里行驶的火车,他们发现 列车上几乎都坐满了,只有两个在一起的空位,这个空位的对面是一个老奶 奶和一个年轻漂亮的姑娘。两个上前坐了下来。程序员和那个姑娘他们比较 暧昧地相互看对方。这时,火车进入山洞,车厢里一片漆黑。此时,只听见 一个亲嘴的声音,随后就听到一个响亮的巴掌声。很快火车出了山洞,他们 四个人都不说话。
那个老奶奶在喃喃道, “这个年轻小伙怎么这么无礼, 不过我很高兴我的孙女 扇了一个巴掌”。
项目经理在想,“没想到这个程序员居然这么大胆,敢去亲那姑娘,只可惜那 姑娘打错了人,居然给打了我。”
漂亮的姑娘想,“他亲了我真好,希望我的祖母没有打疼他”。
程序员坐在那里露出了笑容, “生活真好啊。 这一辈子能有几次机会可以在亲 一个美女的同时打项目经理一巴掌啊”
命令端口C++检测本地网络端口占用相关推荐
- Linux系统弱口令检测和网络端口扫描方法(JR、NMAP)
Linux系统弱口令检测和网络端口扫描方法JR.NMAP 一.系统弱口令检测 1.Joth the Ripper ,简称JR 2.安装JR工具 3.检测弱口令账号 4.密码文件的暴力破解 5.基本步骤 ...
- 系统弱口令检测与网络端口扫描
文章目录 一.系统弱口令检测 1.命令代码 2.实际操作 二.网络端口扫描 1.nmap命令常用选项与对应扫描类型 一.系统弱口令检测 Joth the Ripper,简称JR ●一款开源的密码分析工 ...
- linux《十》——系统安全之弱口令检测与网络端口扫描
内容要点: 弱口令检测 网络端口扫描 一.弱口令检测 1.Joth the Ripper,简称为JR 一款密码分析工具,支持字典式的暴力破解 通过对shadow文件的口令分析,可以检测密码强度 官方网 ...
- Linux下利用nc命令来监控检测服务器的端口使用情况
检测端口还在用telnet?太落伍把 有没有批量检测的方法?有的.我们用nc就可以快速检测端口的开放性. nc检测端口的用法 nc -z -w 10 %IP% %PORT% -z表示检测或者 ...
- 交换机端口及常见的网络端口
1.交换机接口-[字母简写涵义] S口 Serial接口的意思,也叫高速异步串口,主要是连接广域网的V.35线缆用的,说白了就是路由器和路由器连接时候用的,可以用命令设置带宽,一般也就在10M.8M左 ...
- python监听udp端口_python检测远程udp端口是否打开
python检测远程udp端口是否打开 import socket import threading import time import struct import Queue queue = Qu ...
- 如何检测本地网络是否稳定
今天用SVN检出项目的时候,感觉非常缓慢,就查了一下原因,可能是本地网络不稳定,那么如何检测呢? 第一步 在键盘上同时按下Windows+R,然后输入cmd 第二步 输入ping www.baidu. ...
- linux fuser 命令 查看文件/网络端口 被什么进程占用
linux环境下,当使用umount命令卸载挂载点时,会遇到"device is busy"提示,这时fuser就能查出谁在使用这个资源;当然umount –lf [挂载点] 也 ...
- Windows服务与网络端口
转:https://support.microsoft.com/zh-cn/help/832017/service-overview-and-network-port-requirements-for ...
- openstack关闭安全组(网络端口)的限制
1.查看用户的项目ID openstack project list | grep ucs_project 2.查看该用户的安全组ID openstack security group list | ...
最新文章
- 云服务器镜像麻烦吗_简单说说云服务器的镜像功能作用
- VTK:可视化算法之CutWithCutFunction
- 1、代码中设置编码、编辑器中设置Python的编码
- python读取一行数组_python 把文件中的每一行以数组的元素放入数组中的方法
- unique函数_C++核心准则C.35:基类的析构函数必须满足的条件
- 【Ant Design Vue】之layout布局
- SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口
- 基于T4模板的文档生成
- linux卸载mysql和myodbc_linux下卸载mysql rpm安装方式和源码安装方式的两种方法
- mybatis 原理_了解Mybatis的工作原理吗
- socket服务器和客户端的建立步骤
- HDU 2243(AC自动机+矩阵快速幂)
- Lecture 008-Heuristic algorithms
- 人生如逆旅,我亦是行人
- 如何在Android TV 桌面添加自定义频道/节目
- 安卓获取手机视频和图片
- PS在导出图片时提示无法加载扩展,未经正确签署
- 什么是电子邮件地址?
- 常用API、static、数组复制、双色球练习与酒店管理系统
- android百度地图行政区填充颜色