123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using HTEX.ScreenClient.Services;
- using System.Diagnostics;
- using System.Management;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- using System.Text.RegularExpressions;
- namespace HTEX.ScreenClient
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- long CpuCoreCount = 0;
- long MenemorySize = 0;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ) {
-
- // 获取CPU核心数
- int processorCount = Environment.ProcessorCount;
-
- Console.WriteLine("CPU 核心数: " + processorCount);
- using (ManagementClass managementClass = new ManagementClass("Win32_Processor"))
- {
- using (ManagementObjectCollection managementObjectCollection = managementClass.GetInstances())
- {
- foreach (ManagementObject managementObject in managementObjectCollection)
- {
- CpuCoreCount += Convert.ToInt32(managementObject.Properties["NumberOfLogicalProcessors"].Value);
- }
- }
- }
- using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
- {
- using (ManagementObjectCollection moc = mc.GetInstances())
- {
- foreach (ManagementObject mo in moc)
- {
- if (mo["TotalPhysicalMemory"]!= null)
- {
- MenemorySize = Convert.ToInt64(mo["TotalPhysicalMemory"]);
- }
- }
- }
- }
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
- int processorCount = Environment.ProcessorCount;
- Console.WriteLine("CPU 核心数: " + processorCount);
- string[] cpu_lines = File.ReadAllLines("/proc/cpuinfo");
- CpuCoreCount= cpu_lines.Count(line => line.StartsWith("processor", StringComparison.OrdinalIgnoreCase));
- string[] mem_lines = File.ReadAllLines("/proc/meminfo");
- var match = mem_lines.FirstOrDefault(line => line.StartsWith("MemTotal:"));
- if (match != null)
- {
- var matchResult = Regex.Match(match, @"\d+");
- if (matchResult.Success)
- {
- MenemorySize= long.Parse(matchResult.Value);
- }
- }
- } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
- using (var process = new Process())
- {
- process.StartInfo.FileName = "/usr/sbin/sysctl";
- process.StartInfo.Arguments = "-n hw.ncpu";
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- string output = process.StandardOutput.ReadToEnd().Trim();
- int coreCount;
- if (int.TryParse(output, out coreCount))
- {
- CpuCoreCount= coreCount;
- }
- }
- using (var process = new Process())
- {
- process.StartInfo.FileName = "/usr/sbin/sysctl";
- process.StartInfo.Arguments = "-n hw.memsize";
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- string output = process.StandardOutput.ReadToEnd().Trim();
- long memorySize;
- if (long.TryParse(output, out memorySize))
- {
- MenemorySize= memorySize;
- }
- }
- }
- Console.WriteLine("CPU 核心数: " + CpuCoreCount+",RAM 大小:"+MenemorySize);
- var builder = WebApplication.CreateBuilder(args);
-
- builder.Services.AddControllers();
- builder.Services.AddHttpClient();
- builder.Services.AddHttpContextAccessor();
- //CheckOrNewPort(1883)
- //CheckOrNewPort(5000)
- builder.WebHost.UseKestrel(options => {
- //options.ListenAnyIP(4001, options => {
- // // options.UseHttps("Crt/iteden.pfx", "iteden");
- //});
- options.ListenAnyIP(1883, options => {/*options.UseHttps("Crt/iteden.pfx", "iteden");*/ });
- options.ListenAnyIP(5000, options => {/* options.UseHttps("Configs/Crt/iteden.pfx", "iteden"); */}); // Default HTTP pipeline
- });
- builder.Services.AddHostedService<SignalRScreenClientHub>();
- var app = builder.Build();
- // Configure the HTTP request pipeline.
- app.UseHttpsRedirection();
- app.UseAuthorization();
- app.MapControllers();
- app.Run();
- }
- /// <summary>
- /// 检测端口是否可用,如果不可用,则递归调用本方法,直到可用为止
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public static int CheckOrNewPort(int port)
- {
- if (IsPortAvailable(port))
- {
- return port;
- }
- else
- {
- return CheckOrNewPort(port + 1);
- }
- }
- /// <summary>
- /// 探测端口是否可用
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public static bool IsPortAvailable(int port)
- {
- bool isPortAvailable = false;
- IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // 本地地址
- try
- {
- using (Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
- {
- // 尝试连接到指定的IP地址和端口
- IAsyncResult result = socket.BeginConnect(ipAddress, port, null, null);
- // 等待连接尝试完成(这里简单地使用Socket的ConnectTimeout,但更常见的做法是使用超时等待或异步回调)
- bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1), true);
- if (success)
- {
- // 如果连接成功,则端口被占用
- socket.EndConnect(result);
- isPortAvailable = false;
- // 可选:如果需要,可以在这里关闭连接
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- }
- else
- {
- isPortAvailable = true;
- }
- }
- }
- catch (SocketException ex)
- {
- Console.WriteLine(ex.Message);
- // 如果错误代码为10048,则表示地址已被使用
- if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
- {
- isPortAvailable = false;
- }
- }
- catch (Exception ex)
- {
- isPortAvailable = false;
- Console.WriteLine(ex.Message);
- }
- return isPortAvailable;
- }
- }
- }
|