本文整理汇总了C#中System.Net.Sockets.TcpClient.ConnectWithTimeout方法的典型用法代码示例。如果您正苦于以下问题:C# TcpClient.ConnectWithTimeout方法的具体用法?C# TcpClient.ConnectWithTimeout怎么用?C# TcpClient.ConnectWithTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.TcpClient
的用法示例。
在下文中一共展示了TcpClient.ConnectWithTimeout方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateConnection
/// <summary>
/// Creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <param name="timeout">Timeout duration for the Connect attempt.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public TcpClient CreateConnection(string destinationHost, int destinationPort, TimeSpan timeout)
{
try
{
// if we have no connection, create one
if (TcpClient == null)
{
if (string.IsNullOrEmpty(ProxyHost))
throw new ProxyException("ProxyHost property must contain a value.");
if (ProxyPort <= 0 || ProxyPort > 65535)
throw new ProxyException("ProxyPort value must be greater than zero and less than 65535");
// create new tcp client object to the proxy server
TcpClient = tcpClientFactory();
// attempt to open the connection
log.Write(EventType.Diagnostic, "Connecting to proxy at {0}:{1}", ProxyHost, ProxyPort);
TcpClient.ConnectWithTimeout(ProxyHost, ProxyPort, timeout);
}
// send connection command to proxy host for the specified destination host and port
SendConnectionCommand(destinationHost, destinationPort);
// return the open proxied tcp client object to the caller for normal use
return TcpClient;
}
catch (SocketException ex)
{
throw new ProxyException(string.Format(CultureInfo.InvariantCulture, "Connection to proxy host {0} on port {1} failed.", Utils.GetHost(TcpClient), Utils.GetPort(TcpClient)), ex);
}
}