浏览代码

统计网卡流量信息

yuzhengyang 8 年之前
父节点
当前提交
a0bafd08c7

+ 2 - 1
Fork.Net/Oreo.NetMonitor/Services/NetWorkService.cs

@@ -13,6 +13,7 @@ using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using Y.Utils.DataUtils.Collections;
 using Y.Utils.DataUtils.Collections;
 using Y.Utils.DataUtils.JsonUtils;
 using Y.Utils.DataUtils.JsonUtils;
+using Y.Utils.NetUtils.NetInfoUtils;
 using Y.Utils.WindowsUtils.InfoUtils;
 using Y.Utils.WindowsUtils.InfoUtils;
 
 
 namespace Oreo.NetMonitor.Services
 namespace Oreo.NetMonitor.Services
@@ -151,7 +152,7 @@ namespace Oreo.NetMonitor.Services
                 {
                 {
                     //获取实时数据包
                     //获取实时数据包
                     #region 设置IP
                     #region 设置IP
-                    var networkInfo = ComputerInfoTool.GetNetworkCardInfo();
+                    var networkInfo = NetcardInfoTool.GetNetworkCardInfo();
                     if (!ListTool.IsNullOrEmpty(networkInfo))
                     if (!ListTool.IsNullOrEmpty(networkInfo))
                     {
                     {
                         IP = networkInfo[0].Item3;
                         IP = networkInfo[0].Item3;

+ 1 - 1
Fork.Net/Oreo.NetMonitor/Services/NetflowService.cs

@@ -7,6 +7,6 @@ namespace Oreo.NetMonitor.Services
 {
 {
     public class NetflowService
     public class NetflowService
     {
     {
-
+       
     }
     }
 }
 }

+ 32 - 4
Fork.Net/Oreo.NetMonitor/Views/TestForm.cs

@@ -1,19 +1,47 @@
-using System;
+using Oreo.NetMonitor.Commons;
+using System;
+using System.Diagnostics;
+using System.Threading;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Windows.Forms;
-using Y.Utils.WindowsUtils.ProcessUtils;
+using Y.Utils.NetUtils.NetInfoUtils;
 
 
 namespace Oreo.NetMonitor.Views
 namespace Oreo.NetMonitor.Views
 {
 {
     public partial class TestForm : Form
     public partial class TestForm : Form
     {
     {
+        NetflowTool nft = new NetflowTool();
         public TestForm()
         public TestForm()
         {
         {
             InitializeComponent();
             InitializeComponent();
         }
         }
         private void TestForm_Load(object sender, EventArgs e)
         private void TestForm_Load(object sender, EventArgs e)
         {
         {
-            var a = NetProcessTool.GetTcpConn();
-            var b = NetProcessTool.GetUdpConn();
+            PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
+            string[] instances = performanceCounterCategory.GetInstanceNames();
+
+            var info = NetcardInfoTool.GetNetworkCardInfo();
+
+
+            
+
+            Task.Factory.StartNew(() =>
+            {
+                nft.InitNetcard("Intel[R] Dual Band Wireless-AC 3165");//Intel(R) Dual Band Wireless-AC 3165
+                nft.InitSettings(1000, 1000);
+                nft.StartDataMonitor();
+
+                while (true)
+                {
+                    R.Log.v("upload data: " + nft.UploadData + " download data: " + nft.DownloadData);
+                    Thread.Sleep(1000);
+                }
+            });
+
+
+
+            //var a = NetProcessTool.GetTcpConn();
+            //var b = NetProcessTool.GetUdpConn();
         }
         }
     }
     }
 }
 }

+ 0 - 14
Fork.Net/Y.Utils/NetUtils/NetDiagnoseUtils/NetflowTool.cs

@@ -1,14 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Text;
-
-namespace Y.Utils.NetUtils.NetDiagnoseUtils
-{
-    public class NetflowTool
-    {
-        public static PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
-        public static string[] instances = performanceCounterCategory.GetInstanceNames();
-    }
-}

+ 1 - 1
Fork.Net/Y.Utils/WindowsUtils/ProcessUtils/NetProcessTool.cs

@@ -6,7 +6,7 @@ using System.Net;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Text;
 
 
-namespace Y.Utils.WindowsUtils.ProcessUtils
+namespace Y.Utils.NetUtils.NetInfoUtils
 {
 {
     public class NetProcessTool
     public class NetProcessTool
     {
     {

+ 46 - 0
Fork.Net/Y.Utils/NetUtils/NetInfoUtils/NetcardInfoTool.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Net.NetworkInformation;
+using System.Text;
+
+namespace Y.Utils.NetUtils.NetInfoUtils
+{
+    public class NetcardInfoTool
+    {
+        #region 获取网卡信息
+        /// <summary>
+        /// 获取网卡信息
+        /// Item1:描述,Item2:物理地址(Mac),Item3:Ip地址,Item4:网关地址
+        /// </summary>
+        /// <returns></returns>
+        public static List<Tuple<string, string, string, string>> GetNetworkCardInfo()
+        {
+            try
+            {
+                List<Tuple<string, string, string, string>> result = new List<Tuple<string, string, string, string>>();
+                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
+                foreach (var item in adapters)
+                {
+                    if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
+                    {
+                        string _desc = item.Description;
+                        string _mac = item.GetPhysicalAddress().ToString();
+                        string _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
+                            item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
+                        string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
+                            item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
+                        result.Add(new Tuple<string, string, string, string>(_desc, _mac, _ip, _gateway));
+                    }
+                }
+                return result;
+            }
+            catch (NetworkInformationException e)
+            {
+                return null;
+            }
+        }
+        #endregion
+    }
+}

+ 124 - 0
Fork.Net/Y.Utils/NetUtils/NetInfoUtils/NetflowTool.cs

@@ -0,0 +1,124 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Y.Utils.NetUtils.NetInfoUtils
+{
+    public class NetflowTool
+    {
+
+        /// <summary>
+        /// 网卡名称
+        /// </summary>
+        public string NetcardName { get { return _NetcardName; } }
+        private string _NetcardName;
+
+        /// <summary>
+        /// 上行数据流量
+        /// </summary>
+        public int UploadData { get { return _UploadData; } }
+        private int _UploadData;
+
+        /// <summary>
+        /// 上行数据包数
+        /// </summary>
+        public int UploadBag { get { return _UploadBag; } }
+        private int _UploadBag;
+
+        /// <summary>
+        /// 上行数据总流量
+        /// </summary>
+        public long UploadDataCount { get { return _UploadDataCount; } }
+        private long _UploadDataCount;
+
+        /// <summary>
+        /// 上行数据包总数
+        /// </summary>
+        public long UploadBagCount { get { return _UploadBagCount; } }
+        private long _UploadBagCount;
+
+        /// <summary>
+        /// 下行数据流量
+        /// </summary>
+        public int DownloadData { get { return _DownloadData; } }
+        private int _DownloadData;
+
+        /// <summary>
+        /// 下行数据包数
+        /// </summary>
+        public int DownloadBag { get { return _DownloadBag; } }
+        private int _DownloadBag;
+
+        /// <summary>
+        /// 下行数据总流量
+        /// </summary>
+        public long DownloadDataCount { get { return _DownloadDataCount; } }
+        private long _DownloadDataCount;
+
+        /// <summary>
+        /// 下行数据包总数
+        /// </summary>
+        public long DownloadBagCount { get { return _DownloadBagCount; } }
+        private long _DownloadBagCount;
+
+        private PerformanceCounter UploadCounter;//上行流量计数器
+        private PerformanceCounter DownloadCounter;//下行流量计数器
+        private int DataCounterInterval = 1000;//数据流量计数器计数周期
+        private int BagCounterInterval = 1000;//数据包计数器计数周期
+        private bool DataMonitorStarted = false;
+        private bool BagMonitorStarted = false;
+
+        /// <summary>
+        /// 初始化网卡
+        /// </summary>
+        public void InitNetcard(string name)
+        {
+            UploadCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", name);
+            DownloadCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", name);
+        }
+
+        /// <summary>
+        /// 初始化配置
+        /// </summary>
+        /// <param name="dataItv">数据流量计数器计数周期</param>
+        /// <param name="bagItv">数据包计数器计数周期</param>
+        public void InitSettings(int dataItv, int bagItv)
+        {
+            DataCounterInterval = dataItv;
+            BagCounterInterval = bagItv;
+        }
+
+        public bool StartDataMonitor()
+        {
+            if (!DataMonitorStarted)
+            {
+                DataMonitorStarted = true;
+
+                Task.Factory.StartNew(() =>
+                {
+                    while (DataMonitorStarted)
+                    {
+                        try
+                        {
+                            _UploadDataCount += _UploadData;
+                            float x1 = UploadCounter.NextValue();
+                            _UploadData = (int)x1;
+
+                            _DownloadDataCount += _DownloadData;
+                            float x2 = DownloadCounter.NextValue();
+                            _DownloadData = (int)x2;
+                        }
+                        catch { }
+                        Thread.Sleep(DataCounterInterval);
+                    }
+                });
+                return true;
+            }
+            return false;
+        }
+    }
+}

+ 1 - 1
Fork.Net/Y.Utils/NetUtils/NetDiagnoseUtils/PingTool.cs

@@ -9,7 +9,7 @@ using System.Net.NetworkInformation;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 
 
-namespace Y.Utils.NetUtils.NetDiagnoseUtils
+namespace Y.Utils.NetUtils.NetInfoUtils
 {
 {
     public class PingTool
     public class PingTool
     {
     {

+ 1 - 33
Fork.Net/Y.Utils/WindowsUtils/InfoUtils/ComputerInfoTool.cs

@@ -55,38 +55,6 @@ namespace Y.Utils.WindowsUtils.InfoUtils
             { return "unknown"; }
             { return "unknown"; }
         }
         }
         #endregion
         #endregion
-        #region 获取网卡信息
-        /// <summary>
-        /// 获取网卡信息
-        /// Item1:描述,Item2:物理地址(Mac),Item3:Ip地址,Item4:网关地址
-        /// </summary>
-        /// <returns></returns>
-        public static List<Tuple<string, string, string, string>> GetNetworkCardInfo()
-        {
-            try
-            {
-                List<Tuple<string, string, string, string>> result = new List<Tuple<string, string, string, string>>();
-                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
-                foreach (var item in adapters)
-                {
-                    if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
-                    {
-                        string _desc = item.Description;
-                        string _mac = item.GetPhysicalAddress().ToString();
-                        string _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
-                            item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
-                        string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
-                            item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
-                        result.Add(new Tuple<string, string, string, string>(_desc, _mac, _ip, _gateway));
-                    }
-                }
-                return result;
-            }
-            catch (NetworkInformationException e)
-            {
-                return null;
-            }
-        }
-        #endregion
+        
     }
     }
 }
 }

+ 4 - 3
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -56,7 +56,8 @@
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
     <Compile Include="IOUtils\ImageUtils\IconTool.cs" />
     <Compile Include="IOUtils\ImageUtils\IconTool.cs" />
-    <Compile Include="NetUtils\NetDiagnoseUtils\NetflowTool.cs" />
+    <Compile Include="NetUtils\NetInfoUtils\NetcardInfoTool.cs" />
+    <Compile Include="NetUtils\NetInfoUtils\NetflowTool.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\AttributeTool.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\AttributeTool.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\CustomAttributeHelper.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\CustomAttributeHelper.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\ControlAttribute.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\ControlAttribute.cs" />
@@ -79,7 +80,7 @@
     <Compile Include="AppUtils\PermissionTool.cs" />
     <Compile Include="AppUtils\PermissionTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ComputerInfoTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ComputerInfoTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\DeviceInfoTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\DeviceInfoTool.cs" />
-    <Compile Include="WindowsUtils\ProcessUtils\NetProcessTool.cs" />
+    <Compile Include="NetUtils\NetInfoUtils\NetProcessTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\RegisterTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\RegisterTool.cs" />
     <Compile Include="IOUtils\ImageUtils\BarCodeToHTML.cs" />
     <Compile Include="IOUtils\ImageUtils\BarCodeToHTML.cs" />
     <Compile Include="IOUtils\LogUtils\LogLevel.cs" />
     <Compile Include="IOUtils\LogUtils\LogLevel.cs" />
@@ -91,7 +92,7 @@
     <Compile Include="IOUtils\ImageUtils\ExifHelper.cs" />
     <Compile Include="IOUtils\ImageUtils\ExifHelper.cs" />
     <Compile Include="IOUtils\ImageUtils\ImageHelper.cs" />
     <Compile Include="IOUtils\ImageUtils\ImageHelper.cs" />
     <Compile Include="DataUtils\CurrencyUtils\RMB.cs" />
     <Compile Include="DataUtils\CurrencyUtils\RMB.cs" />
-    <Compile Include="NetUtils\NetDiagnoseUtils\PingTool.cs" />
+    <Compile Include="NetUtils\NetInfoUtils\PingTool.cs" />
     <Compile Include="IOUtils\OfficeUtils\WordTool.cs" />
     <Compile Include="IOUtils\OfficeUtils\WordTool.cs" />
     <Compile Include="Test\WordToolTester.cs" />
     <Compile Include="Test\WordToolTester.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />