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

2021-06-04 02:01

阅读:545

标签:format   blog   cps   tmp   where   object   poi   tac   active   

原文: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 ListSite()
{
List list = new List();
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
版权声明:本文为博主原创文章,转载请附上博文链接!

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

标签:format   blog   cps   tmp   where   object   poi   tac   active   

原文地址:https://www.cnblogs.com/lonelyxmas/p/10878858.html


评论


亲,登录后才可以留言!