c# 获取端口的连接数,网站的连接数

端口连接数:

复制代码
public static int PortTcpConnection(int port)
{
IPGlobalProperties properti = IPGlobalProperties.GetIPGlobalProperties();
var tcps = properti.GetActiveTcpConnections().ToList();

var list = tcps.Where(f => f.LocalEndPoint.Port == port);

var iplist = list.GroupBy(f => f.RemoteEndPoint.Address);
return iplist.Count();
}
复制代码

 

网站连接数:

复制代码
public List<WebSite> ListSite()
{
List<WebSite> list = new List<WebSite>();
using (ServerManager sm = new ServerManager())
{
foreach (var s in sm.Sites)
{
WebSite site = new WebSite();
site.ID = s.Id;
site.SiteName = s.Name;
site.ApplicationPoolName = s.Applications["/"].ApplicationPoolName;
site.PhysicalPath = s.Applications["/"].VirtualDirectories["/"].PhysicalPath;
site.State = s.State.ToString();

//System.Management.ManagementObject o = new ManagementObject("Win32_PerfFormattedData_W3SVC_WebService.Name='" + s.Name + "'"); 
//site.CurrentConnections = int.Parse(o.Properties["CurrentConnections"].Value.ToString());这个太慢

int tcps = 0;
foreach (var tmp in s.Bindings)
{
WebSite.Bind bind = new WebSite.Bind();
bind.IP = tmp.EndPoint.Address.ToString();
bind.Port = tmp.EndPoint.Port;
bind.Host = tmp.Host;
tcps += Util.SystemInfo.PortTcpConnection(bind.Port);
site.BindList.Add(bind);
}
site.CurrentConnections = tcps;//当前连接数
list.Add(site);
}
}
return list;
}
复制代码

 

---------------------

原文:https://blog.csdn.net/wyljz/article/details/79207507
版权声明:本文为博主原创文章,转载请附上博文链接!



如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击文章下方“推荐”按钮【精神支持】,您的“推荐”将是我最大的写作动力!
欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
posted @   三人成虎  阅读(1962)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
历史上的今天:
2017-10-18 多个自定义覆盖物注册点击事件,点击某个覆盖物后获得它的坐标
2014-10-18 通用权限管理系统基类中数据库的连接

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示