Browse Source

add ConectionStat

HOME 1 year ago
parent
commit
90c6cfa71a
3 changed files with 114 additions and 1 deletions
  1. 44 0
      ConnectionStat/ConnectionStat.csproj
  2. 63 0
      ConnectionStat/Program.cs
  3. 7 1
      StrangeTools.sln

+ 44 - 0
ConnectionStat/ConnectionStat.csproj

@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{158AC9F8-3798-4F08-8028-3F261180BB0B}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>ConnectionStat</RootNamespace>
+    <AssemblyName>ConnectionStat</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <Deterministic>true</Deterministic>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Properties\" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>

+ 63 - 0
ConnectionStat/Program.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.NetworkInformation;
+using System.Reflection;
+
+[assembly: AssemblyTitle("ConnectionStat")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("ConnectionStat")]
+[assembly: AssemblyCopyright("Copyright ©  2022")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
+
+namespace ConnectionStat
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var all = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
+
+            if (all.Length == 0)
+            {
+                Console.WriteLine("GetActiveTcpConnections return 0 items.");
+                return;
+            }
+
+            var allDic = all.GroupBy(p => p.State).Select(p => new { State = p.Key, Count = p.Count() }).Where(p => p.Count != 0).ToDictionary(p => p.State, p => p.Count);
+
+            if (args.Length == 0)
+            {
+                PrintConnectionStat(allDic, "All");
+            }
+            else
+            {
+                foreach (var argString in args)
+                {
+                    if (!int.TryParse(argString, out var port)) continue;
+
+                    var filterByPort = all.Where(p => p.LocalEndPoint.Port == port);
+                    var portDic = filterByPort.GroupBy(p => p.State).Select(p => new { State = p.Key, Count = p.Count() }).Where(p => p.Count != 0).ToDictionary(p => p.State, p => p.Count);
+
+                    PrintConnectionStat(portDic, "Port " + port);
+                }
+            }
+        }
+
+        private static void PrintConnectionStat(Dictionary<TcpState, int> allDic, string label)
+        {
+            Console.WriteLine($" * Connections of {label}");
+            foreach (var kvp in allDic)
+            {
+                Console.Write($"    {kvp.Key}={kvp.Value}");
+            }
+            Console.WriteLine($"   TOTAL={allDic.Sum(p => p.Value)}");
+            Console.WriteLine();
+        }
+    }
+}

+ 7 - 1
StrangeTools.sln

@@ -34,7 +34,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AudioNTR", "AudioNTR\AudioN
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirEx", "DirEx\DirEx.csproj", "{62A1F458-B9ED-4443-8B94-E24ECF57AC9B}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SevenRepacker", "SevenRepacker\SevenRepacker.csproj", "{9670672F-A051-46D4-AF50-B71E3E9815E2}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SevenRepacker", "SevenRepacker\SevenRepacker.csproj", "{9670672F-A051-46D4-AF50-B71E3E9815E2}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectionStat", "ConnectionStat\ConnectionStat.csproj", "{158AC9F8-3798-4F08-8028-3F261180BB0B}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -98,6 +100,10 @@ Global
 		{9670672F-A051-46D4-AF50-B71E3E9815E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9670672F-A051-46D4-AF50-B71E3E9815E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9670672F-A051-46D4-AF50-B71E3E9815E2}.Release|Any CPU.Build.0 = Release|Any CPU
+		{158AC9F8-3798-4F08-8028-3F261180BB0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{158AC9F8-3798-4F08-8028-3F261180BB0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{158AC9F8-3798-4F08-8028-3F261180BB0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{158AC9F8-3798-4F08-8028-3F261180BB0B}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE