Loading files
fbac32f57This file is pinned to the selected release. Download the original to open it in your design tool.
using System.Threading.Tasks;
namespace System.Net.Sockets
{
internal static class TcpClientExtensions
{
/// <summary>
/// Connects the client to a remote TCP host using the specified IP endpoint as an asynchronous operation with a timeout.
/// </summary>
/// <param name="endpoint">Endpoint where the client needs to connect to.</param>
/// <param name="connectionTimeoutMilliseconds">Timeout within the client needs to connect to the remote TCP host.</param>
/// <returns>Returns <c>true</c> if the connection was established before the timeout exceeded.</returns>
public static async Task<bool> ConnectAsync(this TcpClient client, IPEndPoint endpoint, int connectionTimeoutMilliseconds)
{
var connectTask = client.ConnectAsync(endpoint.Address, endpoint.Port);
return await Task.WhenAny(connectTask, Task.Delay(connectionTimeoutMilliseconds)) == connectTask;
}
}
}