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
{
///
/// TcpClientWithTimeout 用来设置一个带连接超时功能的类
/// 使用者可以设置毫秒级的等待超时时间 (1000=1second)
/// 例如:
/// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
///
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;
///
/// 当前连接时的本地终端IP地址
///
public System.Net.IPAddress currentIPEndPoint = null;
///
/// 连接方式
///
int ConnectionMode = 0;
public TcpClientWithTimeout(string hostname, int port, int timeout_milliseconds)
{
_hostname = hostname;
_port = port;
_timeout_milliseconds = timeout_milliseconds;
ConnectionMode = 0;
}
///
/// TcpClientWithTimeout 用来设置一个带连接超时功能的类
/// 使用者可以设置毫秒级的等待超时时间 (1000=1second)
/// 例如:
/// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
///
///
///
///
///
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;
}
}*/
}
}