C#实时检测端口占用情况
在TCP/IP协议中,服务端需要去监听客户端的端口,开始监听,我们需要检测使用的端口是否被占用,获取系统当前使用的所有端口号,用此端口进行匹配即可。
代码如下
internal static Boolean IsPortOccupedFun2(Int32 port)
{
System.Net.NetworkInformation.IPGlobalProperties iproperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
System.Net.IPEndPoint[] ipEndPoints = iproperties.GetActiveTcpListeners();
foreach (var item in ipEndPoints)
{
if (item.Port == port)
{
return true;
}
}
return false;
}
而如果需要实时的刷新端口占用情况,并用其进行TCP连接,希望自己占用的端口在呈现给自己时显示不被占用,例如当服务端UI界面显示监听192.168.0.162IP的客户端时用的9922端口,而不希望在同一DataGridView表中,此行的后面“端口是否被占用“”列中显示已被占用的红色X号图标 , 即在检测端口状态时如果是与此IP通讯占用,排除此IP,如下图

代码如下
static List<IpAndPort> IpAndPortList = new List<IpAndPort>();
static List<string> PortList = new List<string>(); internal static Boolean IsPortOccupedFun2(Int32 port, IpAndPort m_PortList)
{
System.Net.NetworkInformation.IPGlobalProperties iproperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
System.Net.IPEndPoint[] ipEndPoints = iproperties.GetActiveTcpListeners(); if (!PortList.Contains(m_PortList.port))
{
PortList.Add(m_PortList.port);
IpAndPortList.Add(m_PortList);
}
foreach (var item in ipEndPoints)
{ if (item.Port == Convert.ToInt32(m_PortList.port))
{
foreach (IpAndPort ipPort in IpAndPortList)
{
if (ipPort.ip.Equals(m_PortList.ip) && ipPort.port.Equals(m_PortList.port))
{
return false;
}
}
return true;
}
}
return false;
} public class IpAndPort
{
public string ip;
public string port;
}
C#实时检测端口占用情况的更多相关文章
- ubuntu系统检测端口占用情况
参考链接: https://blog.csdn.net/qwfys200/article/details/80837036 命令: $ sudo netstat -tupln
- 26. linux查看端口占用情况
linux系统下,查看端口占用情况的命令:lsof -i[root@www ~]# lsof -i
- windows 如何查看端口占用情况?
原文来自:http://www.iteye.com/topic/1117270 开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这 ...
- 在windows和linux下如何查看80端口占用情况?是被哪个进程占用?如何终止等
一.在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等 这里主要是用到windows下的DOS工具,点击"开始"--"运行",输入&quo ...
- RC-50221 问题解决 - netstat 查看端口占用情况
查看端口占用情况 netstat -an|grep LIST|grep 15 数据库监听占用情况. netstat -an|grep 1521 1521为 ...
- Windows系统端口占用情况检查脚本
写了一段检查Windows下端口占用情况的脚本,代码如下: function checkPid($result,$port){ $port = $port.split(":")[1 ...
- 查看Linux下端口占用情况的命令
在使用Linux系统的过程中,有时候会遇到端口被占用而导致服务无法启动的情况.比如HTTP使用80端口,但当启动Apache时,却发现此端口正在使用. 这种情况大多数是由于软件冲突.或者默认端口设置不 ...
- 【转】windows 如何查看端口占用情况?
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- Mac查看端口占用情况
Mac下使用lsof(list open files)来查看端口占用情况,lsof 是一个列出当前系统打开文件的工具. 使用 lsof 会列举所有占用的端口列表: $ lsof 使用less可以用于分 ...
随机推荐
- 利用PHPExcel读取excel文件
$filePath = "7788.xls"; $PHPExcel = new PHPExcel(); $PHPReader = new PHPExcel_Reader_Excel ...
- vue实现左侧滑动删除
不是很完美,无法做到第一个左滑其他的隐藏删除: 代码来源于 https://segmentfault.com/a/1190000011062124 自己做了写改动,添加父组件点击触发子组件 引入组件 ...
- 用百度AI的OCR文字识别结合JAVA实现了图片的文字识别功能
第一步可定要获取百度的三个东西 要到百度AI网站(http://ai.baidu.com/)去注册 然后获得 -const APP_ID = '请填写你的appid'; -const API_KEY ...
- (10)The secret to great opportunities? The person you haven't met yet
https://www.ted.com/talks/tanya_menon_the_secret_to_great_opportunities_the_person_you_haven_t_met_y ...
- 如何在MYSQL下所有指定数据库名下执行SQL
mysql下用户库比较多,都有统一的命名格式,希望在这些所有用户库执行脚本,更新数据,或者查询数据 可以采用以下存储过程实现 DROP PROCEDURE IF EXISTS `sp_execalld ...
- 简单MVC实现增删改查
反射工具类RelfectionUtils package Utils; import java.lang.reflect.Field; import java.lang.reflect.Invocat ...
- 20155205 2016-2017-2 《Java程序设计》第1周学习总结
20155205 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 下载了娄老师推介的xmind,试着自己总结了一下. 为了要运行Java程序,必须安装 ...
- Remote Debugging (2)
use Eclipse| a Java application 创建一个简单的maven项目 Main.java package cn.zno; public class Main { public ...
- chrome 字体太浅,如何设置
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-directwrite-for-ui
- MIT Molecular Biology 笔记3 DNA同源重组
视频 https://www.bilibili.com/video/av7973580?from=search&seid=16993146754254492690 教材 Molecular ...