Program.cs 3.6 KB

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