NetUtil.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.NetworkInformation;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. namespace CommonNetwork_ICE.Util
  9. {
  10. /// <summary>
  11. /// 网络工具类
  12. /// </summary>
  13. public class NetUtil
  14. {
  15. /// <summary>
  16. /// 获取本机IP
  17. /// </summary>
  18. /// <returns>本机IP</returns>
  19. public static IPAddress getLocalIp()
  20. {
  21. IPHostEntry host;
  22. host = Dns.GetHostEntry(Dns.GetHostName());
  23. foreach (IPAddress ip in host.AddressList)
  24. {
  25. if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  26. {
  27. return ip;
  28. }
  29. }
  30. return null;
  31. }
  32. /// <summary>
  33. /// 检测端口是否被占用
  34. /// </summary>
  35. /// <param name="port">端口</param>
  36. /// <returns>是否被占用</returns>
  37. public static bool CheckPortInUse(IPAddress ip, int port)
  38. {
  39. bool inUse = CheckTcpPortInUse(ip, port);
  40. if (inUse)
  41. {
  42. return true;
  43. }
  44. return CheckUdpPortInUse(ip, port);
  45. }
  46. /// <summary>
  47. /// 检测TCP端口是否被占用
  48. /// </summary>
  49. /// <param name="port">端口</param>
  50. /// <returns>是否被占用</returns>
  51. public static bool CheckTcpPortInUse(IPAddress ip, int port)
  52. {
  53. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  54. IPEndPoint ipAddress = new IPEndPoint(ip, port);
  55. try
  56. {
  57. socket.Bind(ipAddress);
  58. }
  59. catch (SocketException se)
  60. {
  61. return true;
  62. }
  63. finally
  64. {
  65. if (socket != null)
  66. {
  67. socket.Close();
  68. }
  69. }
  70. return false;
  71. }
  72. /// <summary>
  73. /// 检测UDP端口是否被占用
  74. /// </summary>
  75. /// <param name="port">端口</param>
  76. /// <returns>是否被占用</returns>
  77. public static bool CheckUdpPortInUse(IPAddress ip, int port)
  78. {
  79. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  80. IPEndPoint ipAddress = new IPEndPoint(ip, port);
  81. try
  82. {
  83. socket.Bind(ipAddress);
  84. }
  85. catch (SocketException se)
  86. {
  87. return true;
  88. }
  89. finally
  90. {
  91. if (socket != null)
  92. {
  93. socket.Close();
  94. }
  95. }
  96. return false;
  97. }
  98. /// <summary>
  99. /// 检测可用端口,通过基准端口(每次+1)查找可用端口
  100. /// </summary>
  101. /// <param name="port">基准端口</param>
  102. /// <returns>可用端口,无可用端口返回-1</returns>
  103. public static int GetUsablePort(int port)
  104. {
  105. IPAddress ip = getLocalIp();
  106. for (int i = port; i < 65535; i++)
  107. {
  108. if (!CheckPortInUse(ip, port))
  109. {
  110. return i;
  111. }
  112. }
  113. return -1;
  114. }
  115. }
  116. }