在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#实时检测端口占用情况的更多相关文章

  1. ubuntu系统检测端口占用情况

    参考链接: https://blog.csdn.net/qwfys200/article/details/80837036 命令: $ sudo netstat -tupln

  2. 26. linux查看端口占用情况

    linux系统下,查看端口占用情况的命令:lsof -i[root@www ~]# lsof -i

  3. windows 如何查看端口占用情况?

    原文来自:http://www.iteye.com/topic/1117270 开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这 ...

  4. 在windows和linux下如何查看80端口占用情况?是被哪个进程占用?如何终止等

    一.在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等 这里主要是用到windows下的DOS工具,点击"开始"--"运行",输入&quo ...

  5. RC-50221 问题解决 - netstat 查看端口占用情况

    查看端口占用情况   netstat -an|grep LIST|grep 15     数据库监听占用情况. netstat -an|grep 1521                  1521为 ...

  6. Windows系统端口占用情况检查脚本

    写了一段检查Windows下端口占用情况的脚本,代码如下: function checkPid($result,$port){ $port = $port.split(":")[1 ...

  7. 查看Linux下端口占用情况的命令

    在使用Linux系统的过程中,有时候会遇到端口被占用而导致服务无法启动的情况.比如HTTP使用80端口,但当启动Apache时,却发现此端口正在使用. 这种情况大多数是由于软件冲突.或者默认端口设置不 ...

  8. 【转】windows 如何查看端口占用情况?

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...

  9. Mac查看端口占用情况

    Mac下使用lsof(list open files)来查看端口占用情况,lsof 是一个列出当前系统打开文件的工具. 使用 lsof 会列举所有占用的端口列表: $ lsof 使用less可以用于分 ...

随机推荐

  1. BS架构如何实现即时消息提醒

    转载地址:https://blog.csdn.net/cyjch/article/details/51506434

  2. mybatis学习 十 动态 SQL

    1.  根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...

  3. sql相同项求和

    select (SELECT O2.ORG_NAME           FROM OUTSOURCE_ORG O2          where o2.org_id = oo.parent_id) ...

  4. 【转】mysql 解事务锁

    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 原创 2014年07月31日 10:59:43 5 ...

  5. Bayes’s formula for Conditional Probability

    Conditional Probability Example:In a batch, there are 80% C programmers, and 40% are Java and C prog ...

  6. EF对应null的处理

    原来的代码是 if (string.IsNullOrWhiteSpace(seal)) seal = null; ctx.Terminal.FirstOrDefault(ent=>ent.Sea ...

  7. 开启Greenplum DataBase报错:could not bind IPv4 socket: Address already in use

    在运行gpstart时无法开启服务,查看日志看到下图所示错误: 查看日志错误大概是端口已被占用,所以无法重启. 解决方法: (1)利用ipcs查看数据库占用的共享内存.(如下图所示) (2)查看数据库 ...

  8. MySql Cast与Convert函数

    两者具体的语法如下: Cast(value as type): Convert(value ,type): type不是都可以滴,可以转换的type如下: 二进制,同带binary前缀的效果 : BI ...

  9. POJ 3110 Jenny's First Exam (贪心)

    题意:告诉你n 个科目的考试日期,在考试当天不能复习,每一个科目的最早复习时间不能早于考试时间的t天,每一天你可以复习完一科,也只能复习一科,求最晚的复习时间!. 析:由于题目给定的时间都在1900 ...

  10. java编程IO简单回顾和学习

    java编程IO操作必不可少的,很久不玩IO,回顾一下,写了几个小程序,记录一下,方便查阅和学习. 1.给出一个整数数组,将其写入一个文件,再从文件中读出,并按整数大小逆序打印. package co ...