码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-05-17 00:19:18      阅读:178      评论:0      收藏:0      [点我收藏+]

标签: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<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
版权声明:本文为博主原创文章,转载请附上博文链接!

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!