TcpClientWithTimeout.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Net;
  8. namespace LYFZ.Network
  9. {
  10. /// <summary>
  11. /// TcpClientWithTimeout 用来设置一个带连接超时功能的类
  12. /// 使用者可以设置毫秒级的等待超时时间 (1000=1second)
  13. /// 例如:
  14. /// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
  15. /// </summary>
  16. public class TcpClientWithTimeout
  17. {
  18. protected string _hostname;
  19. protected int _port;
  20. protected int _timeout_milliseconds;
  21. protected TcpClient connection;
  22. protected bool connected;
  23. protected Exception exception;
  24. System.Net.IPAddress ipaddress;
  25. IPAddress[] IPAddressList=null;
  26. /// <summary>
  27. /// 当前连接时的本地终端IP地址
  28. /// </summary>
  29. public System.Net.IPAddress currentIPEndPoint = null;
  30. /// <summary>
  31. /// 连接方式
  32. /// </summary>
  33. int ConnectionMode = 0;
  34. public TcpClientWithTimeout(string hostname, int port, int timeout_milliseconds)
  35. {
  36. _hostname = hostname;
  37. _port = port;
  38. _timeout_milliseconds = timeout_milliseconds;
  39. ConnectionMode = 0;
  40. }
  41. /// <summary>
  42. /// TcpClientWithTimeout 用来设置一个带连接超时功能的类
  43. /// 使用者可以设置毫秒级的等待超时时间 (1000=1second)
  44. /// 例如:
  45. /// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
  46. /// </summary>
  47. /// <param name="_ipaddress"></param>
  48. /// <param name="_IPAddressList"></param>
  49. /// <param name="port"></param>
  50. /// <param name="timeout_milliseconds"></param>
  51. public TcpClientWithTimeout(System.Net.IPAddress _ipaddress, IPAddress[] _IPAddressList, int port, int timeout_milliseconds)
  52. {
  53. ipaddress = _ipaddress;
  54. _port = port;
  55. _timeout_milliseconds = timeout_milliseconds;
  56. IPAddressList = _IPAddressList;
  57. ConnectionMode = 1;
  58. }
  59. public TcpClient Connect()
  60. {
  61. // kick off the thread that tries to connect
  62. connected = false;
  63. exception = null;
  64. Thread thread = new Thread(new ThreadStart(BeginConnect));
  65. thread.IsBackground = true; // 作为后台线程处理
  66. // 不会占用机器太长的时间
  67. thread.Start();
  68. // 等待如下的时间
  69. thread.Join(_timeout_milliseconds);
  70. if (connected == true)
  71. {
  72. // 如果成功就返回TcpClient对象
  73. thread.Abort();
  74. return connection;
  75. }
  76. if (exception != null)
  77. {
  78. // 如果失败就抛出错误
  79. thread.Abort();
  80. throw exception;
  81. }
  82. else
  83. {
  84. // 同样地抛出错误
  85. thread.Abort();
  86. string message = string.Format("TcpClient的连接{0}:{1}超时",
  87. _hostname, _port);
  88. throw new TimeoutException(message);
  89. }
  90. }
  91. protected void BeginConnect()
  92. {
  93. try
  94. {
  95. if (ConnectionMode == 1)
  96. {
  97. connection = new TcpClient(new IPEndPoint(ipaddress, 0));
  98. connection.Connect(IPAddressList, _port); // 与服务器连接
  99. if (connection.Connected)
  100. {
  101. currentIPEndPoint = ipaddress;
  102. // 标记成功,返回调用者
  103. connected = true;
  104. }
  105. }
  106. else {
  107. connection = new TcpClient(_hostname, _port);
  108. // 标记成功,返回调用者
  109. connected = true;
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. // 标记失败
  115. exception = ex;
  116. }
  117. }
  118. /* protected void BeginConnect()
  119. {
  120. try
  121. {
  122. connection = new TcpClient(_hostname, _port);
  123. // 标记成功,返回调用者
  124. connected = true;
  125. }
  126. catch (Exception ex)
  127. {
  128. // 标记失败
  129. exception = ex;
  130. }
  131. }*/
  132. }
  133. }