From 3ae7366274d64c3f1062c78860f0ead5cc5e4465 Mon Sep 17 00:00:00 2001
From: Martin Vseticka <203266+MartyIX@users.noreply.github.com>
Date: Tue, 3 Nov 2020 19:33:23 +0100
Subject: [PATCH] TcpClient.ConnectAsync(EndPoint) (#44110)
Implement TcpClient.ConnectAsync(IPEndPoint)
---
.../System.Net.Sockets/ref/System.Net.Sockets.cs | 2 ++
.../src/System/Net/Sockets/TCPClient.cs | 17 +++++++++++++++++
.../tests/FunctionalTests/TcpClientTest.cs | 10 ++++++++++
3 files changed, 29 insertions(+)
diff --git a/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs b/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
index 9e96d15..781b667 100644
--- a/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
+++ b/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
@@ -610,9 +610,11 @@ namespace System.Net.Sockets
public System.Threading.Tasks.Task ConnectAsync(System.Net.IPAddress address, int port) { throw null; }
public System.Threading.Tasks.Task ConnectAsync(System.Net.IPAddress[] addresses, int port) { throw null; }
public System.Threading.Tasks.Task ConnectAsync(string host, int port) { throw null; }
+ public System.Threading.Tasks.Task ConnectAsync(System.Net.IPEndPoint remoteEP) { throw null; }
public System.Threading.Tasks.ValueTask ConnectAsync(System.Net.IPAddress address, int port, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.ValueTask ConnectAsync(System.Net.IPAddress[] addresses, int port, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.ValueTask ConnectAsync(string host, int port, System.Threading.CancellationToken cancellationToken) { throw null; }
+ public System.Threading.Tasks.ValueTask ConnectAsync(System.Net.IPEndPoint remoteEP, System.Threading.CancellationToken cancellationToken) { throw null; }
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
public void EndConnect(System.IAsyncResult asyncResult) { }
diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs
index 937d0d0..0149040 100644
--- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs
+++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs
@@ -268,6 +268,14 @@ namespace System.Net.Sockets
public Task ConnectAsync(IPAddress[] addresses, int port) =>
CompleteConnectAsync(Client.ConnectAsync(addresses, port));
+ ///
+ /// Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.
+ ///
+ /// The to which you intend to connect.
+ /// A task representing the asynchronous operation.
+ public Task ConnectAsync(IPEndPoint remoteEP) =>
+ CompleteConnectAsync(Client.ConnectAsync(remoteEP));
+
private async Task CompleteConnectAsync(Task task)
{
await task.ConfigureAwait(false);
@@ -283,6 +291,15 @@ namespace System.Net.Sockets
public ValueTask ConnectAsync(IPAddress[] addresses, int port, CancellationToken cancellationToken) =>
CompleteConnectAsync(Client.ConnectAsync(addresses, port, cancellationToken));
+ ///
+ /// Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.
+ ///
+ /// The to which you intend to connect.
+ /// A cancellation token used to propagate notification that this operation should be canceled.
+ /// A task representing the asynchronous operation.
+ public ValueTask ConnectAsync(IPEndPoint remoteEP, CancellationToken cancellationToken) =>
+ CompleteConnectAsync(Client.ConnectAsync(remoteEP, cancellationToken));
+
private async ValueTask CompleteConnectAsync(ValueTask task)
{
await task.ConfigureAwait(false);
diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpClientTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpClientTest.cs
index a2ce1a2..458e7f7 100644
--- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpClientTest.cs
+++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpClientTest.cs
@@ -123,6 +123,8 @@ namespace System.Net.Sockets.Tests
[InlineData(6)]
[InlineData(7)]
[InlineData(8)]
+ [InlineData(9)]
+ [InlineData(10)]
public async Task ConnectAsync_DnsEndPoint_Success(int mode)
{
using (var client = new DerivedTcpClient())
@@ -169,6 +171,14 @@ namespace System.Net.Sockets.Tests
break;
case 8:
addresses = await Dns.GetHostAddressesAsync(host);
+ await client.ConnectAsync(new IPEndPoint(addresses[0], port));
+ break;
+ case 9:
+ addresses = await Dns.GetHostAddressesAsync(host);
+ await client.ConnectAsync(new IPEndPoint(addresses[0], port), CancellationToken.None);
+ break;
+ case 10:
+ addresses = await Dns.GetHostAddressesAsync(host);
await client.ConnectAsync(addresses, port, CancellationToken.None);
break;
}
--
2.7.4