Browse Source

增加获取网络端口相关进程功能

yuzhengyang 6 years ago
parent
commit
83ba86cd36

+ 2 - 2
Azylee.Utils/Azylee.Core/NetUtils/NetFlowService.cs

@@ -220,8 +220,8 @@ namespace Azylee.Core.NetUtils
         /// </summary>
         void GetConnection()
         {
-            TcpConnection = NetProcessTool.GetTcpConnection();
-            UdpConnection = NetProcessTool.GetUdpConnection();
+            TcpConnection = NetProcessTool.GetTcps();
+            UdpConnection = NetProcessTool.GetUdps();
         }
         #endregion
         #region 设置程序流量及连接数统计列表

+ 49 - 3
Azylee.Utils/Azylee.Core/NetUtils/NetProcessTool.cs

@@ -1,4 +1,5 @@
-using System;
+using Azylee.Core.DataUtils.CollectionUtils;
+using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Linq;
@@ -129,7 +130,7 @@ namespace Azylee.Core.NetUtils
         [DllImport("iphlpapi.dll", SetLastError = true)]
         static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref int dwOutBufLen, bool sort, int ipVersion, UDP_TABLE_CLASS tblClass, uint reserved = 0u);
 
-        public static TcpRow[] GetTcpConnection()
+        private static TcpRow[] GetTcpConnections()
         {
             TcpRow[] array = null;
             int ipVersion = 2;
@@ -162,7 +163,7 @@ namespace Azylee.Core.NetUtils
             }
             return array;
         }
-        public static UdpRow[] GetUdpConnection()
+        private static UdpRow[] GetUdpConnections()
         {
             UdpRow[] array = null;
             int ipVersion = 2;
@@ -195,5 +196,50 @@ namespace Azylee.Core.NetUtils
             }
             return array;
         }
+
+        /// <summary>
+        /// 获取本机所有TCP连接
+        /// </summary>
+        /// <returns></returns>
+        public static TcpRow[] GetTcps()
+        {
+            return GetTcpConnections();
+        }
+        /// <summary>
+        /// 获取本机所有UDP连接
+        /// </summary>
+        /// <returns></returns>
+        public static UdpRow[] GetUdps()
+        {
+            return GetUdpConnections();
+        }
+
+        #region 便捷方法
+        /// <summary>
+        /// 根据端口号获取进程ID
+        /// </summary>
+        /// <param name="port"></param>
+        /// <returns></returns>
+        public static int GetPidByPort(int port)
+        {
+            try
+            {
+                TcpRow[] list = GetTcps();
+                if (Ls.Ok(list))
+                    foreach (var item in list)
+                        if (item.LocalPort == port) return item.ProcessId;
+            }
+            catch { }
+            try
+            {
+                UdpRow[] list = GetUdps();
+                if (Ls.Ok(list))
+                    foreach (var item in list)
+                        if (item.LocalPort == port) return item.ProcessId;
+            }
+            catch { }
+            return -1;
+        }
+        #endregion
     }
 }

+ 5 - 1
Azylee.Utils/Azylee.Core/Properties/AssemblyInfo.cs

@@ -32,4 +32,8 @@ using System.Runtime.InteropServices;
 // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
 //通过使用 "*",如下所示:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.9")]
+[assembly: AssemblyVersion("1.0.1.0")]
+//v*.*.*.*(年月日)//+添加 -删减 @优化 #修复
+
+// v 1.0.1.0(2019年7月4日)
+// + 添加通过本地网络端口获取进程ID方法

+ 48 - 17
Azylee.Utils/Tests/Test.ImageToolTest/Form1.cs

@@ -2,17 +2,22 @@
 using Azylee.Core.DataUtils.GuidUtils;
 using Azylee.Core.DrawingUtils.ImageUtils;
 using Azylee.Core.IOUtils.DirUtils;
+using Azylee.Core.NetUtils;
 using Azylee.Core.WindowsUtils.APIUtils.WallpaperUtils;
+using Azylee.Core.WindowsUtils.CMDUtils;
 using Azylee.YeahWeb.HttpUtils;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
+using System.Diagnostics;
 using System.Drawing;
 using System.IO;
 using System.Linq;
+using System.Net.NetworkInformation;
 using System.Text;
 using System.Windows.Forms;
+using static Azylee.Core.NetUtils.NetProcessTool;
 
 namespace Test.ImageToolTest
 {
@@ -25,29 +30,55 @@ namespace Test.ImageToolTest
 
         private void Form1_Load(object sender, EventArgs e)
         {
-            Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils.WallpaperTool.GetLast10Days();
+            var p = Process.GetProcessById(9090);
 
-           List<string> file_list = new List<string>();
-            var md = Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils.WallpaperTool.GetToday();
-            var md2 = Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils.WallpaperTool.GetYesterday();
+            DateTime f1clock = DateTime.Now;
+            List<TcpConnectionInformation> f1 = null;
+            for (int i = 0; i < 10; i++)
+            {
+                f1 = Fun1();
+            }
+            Console.WriteLine("fun1,time: " + (DateTime.Now - f1clock).TotalMilliseconds);
 
-            if (md != null && Ls.Ok(md.images))
+
+            DateTime f2clock = DateTime.Now;
+            List<Tuple<int, int>> f2 = null;
+            for (int i = 0; i < 10; i++)
             {
-                foreach (var item in md.images)
-                {
-                    string image_url = item.GetImageUrl();
-                    string file_path = DirTool.Combine(@"F:\imgs", item.hsh + ".jpg");
-                    bool down_result = HttpTool.Download(image_url, file_path);
-                    if (down_result) file_list.Add(file_path);
-                }
+                f2 = Fun2();
             }
-            //string a = WallpaperTool.Get();
+            Console.WriteLine("fun2,time: " + (DateTime.Now - f2clock).TotalMilliseconds);
 
-            //bool b = WallpaperTool.Set(@"C:\Users\yuzhengyang\Pictures\\cc.jpg");
+            DateTime f3clock = DateTime.Now;
+            TcpRow[] f3 = null;
+            for (int i = 0; i < 10; i++)
+            {
+                f3 = Fun3();
+            }
+            Console.WriteLine("fun3,time: " + (DateTime.Now - f3clock).TotalMilliseconds);
+
+        }
 
-            //Bitmap b1 = new Bitmap(@"F:\图片压缩测试\未标题-1.jpg");
-            //byte[] b1_byte = IMG.Compression(b1, 30);
-            //File.WriteAllBytes(@"F:\图片压缩测试\未标题-1(Compression).jpg", b1_byte);
+        private List<TcpConnectionInformation> Fun1()
+        {
+            List<TcpConnectionInformation> list = new List<TcpConnectionInformation>();
+            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
+            TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
+            foreach (TcpConnectionInformation t in connections)
+            {
+                list.Add(t);
+            }
+            return list;
+        }
+        private List<Tuple<int, int>> Fun2()
+        {
+            List<Tuple<int, int>> list = CMDNetstatTool.Find(".");
+            return list;
+        }
+        private TcpRow[] Fun3()
+        {
+            TcpRow[] list = NetProcessTool.GetTcps();
+            return list;
         }
     }
 }