Skip to content

Commit

Permalink
增加获取网络端口相关进程功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhengyang committed Jul 4, 2019
1 parent fc0844c commit 83ba86c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Azylee.Utils/Azylee.Core/NetUtils/NetFlowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ private void NewPacketEvent(NetPacketTool tool, Packet packet)
/// </summary>
void GetConnection()
{
TcpConnection = NetProcessTool.GetTcpConnection();
UdpConnection = NetProcessTool.GetUdpConnection();
TcpConnection = NetProcessTool.GetTcps();
UdpConnection = NetProcessTool.GetUdps();
}
#endregion
#region 设置程序流量及连接数统计列表
Expand Down
52 changes: 49 additions & 3 deletions Azylee.Utils/Azylee.Core/NetUtils/NetProcessTool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Azylee.Core.DataUtils.CollectionUtils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
Expand Down Expand Up @@ -129,7 +130,7 @@ public ushort RemotePort
[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;
Expand Down Expand Up @@ -162,7 +163,7 @@ public static TcpRow[] GetTcpConnection()
}
return array;
}
public static UdpRow[] GetUdpConnection()
private static UdpRow[] GetUdpConnections()
{
UdpRow[] array = null;
int ipVersion = 2;
Expand Down Expand Up @@ -195,5 +196,50 @@ public static UdpRow[] GetUdpConnection()
}
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
}
}
6 changes: 5 additions & 1 deletion Azylee.Utils/Azylee.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [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方法
65 changes: 48 additions & 17 deletions Azylee.Utils/Tests/Test.ImageToolTest/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,29 +30,55 @@ public Form1()

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;
}
}
}

0 comments on commit 83ba86c

Please sign in to comment.