123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using System.Net;
- namespace LYFZ.Network
- {
- /// <summary>
- /// TcpClientWithTimeout 用来设置一个带连接超时功能的类
- /// 使用者可以设置毫秒级的等待超时时间 (1000=1second)
- /// 例如:
- /// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
- /// </summary>
- public class TcpClientWithTimeout
- {
- protected string _hostname;
- protected int _port;
- protected int _timeout_milliseconds;
- protected TcpClient connection;
- protected bool connected;
- protected Exception exception;
- System.Net.IPAddress ipaddress;
- IPAddress[] IPAddressList=null;
- /// <summary>
- /// 当前连接时的本地终端IP地址
- /// </summary>
- public System.Net.IPAddress currentIPEndPoint = null;
- /// <summary>
- /// 连接方式
- /// </summary>
- int ConnectionMode = 0;
- public TcpClientWithTimeout(string hostname, int port, int timeout_milliseconds)
- {
- _hostname = hostname;
- _port = port;
- _timeout_milliseconds = timeout_milliseconds;
- ConnectionMode = 0;
- }
- /// <summary>
- /// TcpClientWithTimeout 用来设置一个带连接超时功能的类
- /// 使用者可以设置毫秒级的等待超时时间 (1000=1second)
- /// 例如:
- /// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
- /// </summary>
- /// <param name="_ipaddress"></param>
- /// <param name="_IPAddressList"></param>
- /// <param name="port"></param>
- /// <param name="timeout_milliseconds"></param>
- public TcpClientWithTimeout(System.Net.IPAddress _ipaddress, IPAddress[] _IPAddressList, int port, int timeout_milliseconds)
- {
- ipaddress = _ipaddress;
- _port = port;
- _timeout_milliseconds = timeout_milliseconds;
- IPAddressList = _IPAddressList;
- ConnectionMode = 1;
- }
- public TcpClient Connect()
- {
- // kick off the thread that tries to connect
- connected = false;
- exception = null;
- Thread thread = new Thread(new ThreadStart(BeginConnect));
- thread.IsBackground = true; // 作为后台线程处理
- // 不会占用机器太长的时间
- thread.Start();
- // 等待如下的时间
- thread.Join(_timeout_milliseconds);
- if (connected == true)
- {
- // 如果成功就返回TcpClient对象
- thread.Abort();
- return connection;
- }
- if (exception != null)
- {
- // 如果失败就抛出错误
- thread.Abort();
- throw exception;
- }
- else
- {
- // 同样地抛出错误
- thread.Abort();
- string message = string.Format("TcpClient的连接{0}:{1}超时",
- _hostname, _port);
- throw new TimeoutException(message);
- }
- }
- protected void BeginConnect()
- {
- try
- {
- if (ConnectionMode == 1)
- {
- connection = new TcpClient(new IPEndPoint(ipaddress, 0));
- connection.Connect(IPAddressList, _port); // 与服务器连接
- if (connection.Connected)
- {
- currentIPEndPoint = ipaddress;
- // 标记成功,返回调用者
- connected = true;
- }
- }
- else {
- connection = new TcpClient(_hostname, _port);
- // 标记成功,返回调用者
- connected = true;
- }
-
- }
- catch (Exception ex)
- {
- // 标记失败
- exception = ex;
- }
- }
- /* protected void BeginConnect()
- {
- try
- {
- connection = new TcpClient(_hostname, _port);
- // 标记成功,返回调用者
- connected = true;
- }
- catch (Exception ex)
- {
- // 标记失败
- exception = ex;
- }
- }*/
- }
- }
|