IPUtils.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text.RegularExpressions;
  6. namespace LJLib.IP
  7. {
  8. public static class IPUtils
  9. {
  10. [Obsolete("建议改用方法GetURLIPv4")]
  11. public static string GetURLIP(string URL)
  12. {
  13. string ip = string.Empty;
  14. try
  15. {
  16. IPHostEntry ipHost = Dns.GetHostEntry(URL);
  17. bool haveip = false;
  18. for (int i = 0; i < ipHost.AddressList.Length; i++)
  19. {
  20. if (!haveip)
  21. {
  22. var tip = ipHost.AddressList[i].ToString();
  23. if (tip != "::1")
  24. {
  25. ip = tip;
  26. haveip = true;
  27. }
  28. }
  29. }
  30. }
  31. catch (System.Exception ex)
  32. {
  33. Debug.Write(string.Format("获取龙嘉网失败,URL:[{0}],{1}.", URL, ex.ToString()));
  34. ip = "获取龙嘉网失败:" + URL;
  35. }
  36. return ip;
  37. }
  38. /// <summary>
  39. /// 返回URL的IPv4的地址
  40. /// </summary>
  41. /// <param name="URL">URL地址</param>
  42. /// <returns>IPv4</returns>
  43. public static IPAddress GetURLIPv4(string URL)
  44. {
  45. IPAddress ip = null;
  46. if (IsIPv4Address(URL))
  47. {
  48. return IPAddress.Parse(URL);
  49. }
  50. try
  51. {
  52. var ipHost = Dns.GetHostEntry(URL);
  53. foreach (var ipAddress in ipHost.AddressList)
  54. {
  55. if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
  56. {
  57. if (!IPAddress.IsLoopback(ipAddress))
  58. {
  59. return ipAddress;
  60. }
  61. ip = ipAddress;
  62. }
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. Debug.Write(string.Format("获取龙嘉网失败,URL:[{0}],{1}.", URL, ex.ToString()));
  68. }
  69. return ip;
  70. }
  71. public static IPEndPoint GetIPEndPoint(string ipPortStr)
  72. {
  73. int index = ipPortStr.IndexOf(":");
  74. if (index < 0)
  75. {
  76. throw new Exception(string.Format("ip:port不合法,{0}", ipPortStr));
  77. }
  78. var ipStr = ipPortStr.Substring(0, index);
  79. var portStr = ipPortStr.Substring(index + 1);
  80. try
  81. {
  82. var address = IPAddress.Parse(ipStr);
  83. var port = int.Parse(portStr);
  84. return new IPEndPoint(address, port);
  85. }
  86. catch (Exception ex)
  87. {
  88. throw new Exception(string.Format("ip:port不合法,{0},异常:{1}", ipPortStr, ex));
  89. }
  90. }
  91. private static Regex r = null;
  92. public static bool IsIPv4Address(string ip)
  93. {
  94. if (r == null)
  95. {
  96. r = new Regex(@"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
  97. }
  98. return r.IsMatch(ip);
  99. }
  100. /// <summary>
  101. /// 判断TcpClient是否在连接
  102. /// </summary>
  103. public static bool IsConnecting(TcpClient tcpclient)
  104. {
  105. try
  106. {
  107. if (!tcpclient.Connected)
  108. {
  109. return false;
  110. }
  111. var ifSelectRead = tcpclient.Client.Poll(1000, SelectMode.SelectRead);
  112. if (!ifSelectRead) // 网络在连接无数据可读
  113. {
  114. return true;
  115. }
  116. var ns = tcpclient.GetStream();
  117. return ns.CanRead && ns.DataAvailable;
  118. }
  119. catch (Exception ex)
  120. {
  121. if (!(ex is ObjectDisposedException) && !(ex is InvalidOperationException))
  122. {
  123. Trace.Write(ex.ToString());
  124. }
  125. return false;
  126. }
  127. }
  128. }
  129. }