Program.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using HTEX.ScreenClient.Services;
  2. using System.Diagnostics;
  3. using System.Management;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Text.RegularExpressions;
  8. using TEAMModelOS.SDK.DI.Device;
  9. namespace HTEX.ScreenClient
  10. {
  11. public class Program
  12. {
  13. public static void Main(string[] args)
  14. {
  15. var builder = WebApplication.CreateBuilder(args);
  16. builder.WebHost.UseKestrel(options =>
  17. {
  18. // options.ListenAnyIP(CheckOrNewPort(1883), options => {/*options.UseHttps("Crt/iteden.pfx", "iteden");*/ });
  19. options.ListenAnyIP(CheckOrNewPort(5000), options => {/* options.UseHttps("Configs/Crt/iteden.pfx", "iteden"); */}); // Default HTTP pipeline
  20. });
  21. //写在端口配置之前,并且在用到的DI之前。否则会导致DI注入失败
  22. builder.Services.AddControllers();
  23. builder.Services.AddHttpClient();
  24. builder.Services.AddHttpContextAccessor();
  25. builder.Services.AddHostedService<SignalRScreenClientHub>();
  26. builder.Services.AddSingleton<CoreDevice>();
  27. var app = builder.Build();
  28. app.UseHttpsRedirection();
  29. app.UseAuthorization();
  30. app.MapControllers();
  31. app.Run();
  32. }
  33. /// <summary>
  34. /// 检测端口是否可用,如果不可用,则递归调用本方法,直到可用为止
  35. /// </summary>
  36. /// <param name="port"></param>
  37. /// <returns></returns>
  38. public static int CheckOrNewPort(int port)
  39. {
  40. if (IsPortAvailable(port))
  41. {
  42. return port;
  43. }
  44. else
  45. {
  46. return CheckOrNewPort(port + 1);
  47. }
  48. }
  49. /// <summary>
  50. /// 探测端口是否可用
  51. /// </summary>
  52. /// <param name="port"></param>
  53. /// <returns></returns>
  54. public static bool IsPortAvailable(int port)
  55. {
  56. bool isPortAvailable = false;
  57. IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // 本地地址
  58. try
  59. {
  60. using (Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  61. {
  62. // 尝试连接到指定的IP地址和端口
  63. IAsyncResult result = socket.BeginConnect(ipAddress, port, null, null);
  64. // 等待连接尝试完成(这里简单地使用Socket的ConnectTimeout,但更常见的做法是使用超时等待或异步回调)
  65. bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1), true);
  66. if (success)
  67. {
  68. // 如果连接成功,则端口被占用
  69. socket.EndConnect(result);
  70. isPortAvailable = false;
  71. // 可选:如果需要,可以在这里关闭连接
  72. //socket.Shutdown(SocketShutdown.Both);
  73. //socket.Close();
  74. }
  75. else
  76. {
  77. isPortAvailable = true;
  78. }
  79. }
  80. }
  81. catch (SocketException ex)
  82. {
  83. Console.WriteLine($"{ex.Message},{ex.SocketErrorCode}");
  84. // 如果错误代码为10048,则表示地址已被使用
  85. if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
  86. {
  87. isPortAvailable = false;
  88. }
  89. if (ex.SocketErrorCode==SocketError.ConnectionRefused)
  90. {
  91. isPortAvailable=true;
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. Console.WriteLine(ex.Message);
  97. isPortAvailable = false;
  98. }
  99. return isPortAvailable;
  100. }
  101. }
  102. }