TcpClient.ConnectAsync(EndPoint) (#44110)
authorMartin Vseticka <203266+MartyIX@users.noreply.github.com>
Tue, 3 Nov 2020 18:33:23 +0000 (19:33 +0100)
committerGitHub <noreply@github.com>
Tue, 3 Nov 2020 18:33:23 +0000 (10:33 -0800)
Implement TcpClient.ConnectAsync(IPEndPoint)

src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs
src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpClientTest.cs

index 9e96d15..781b667 100644 (file)
@@ -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) { }
index 937d0d0..0149040 100644 (file)
@@ -268,6 +268,14 @@ namespace System.Net.Sockets
         public Task ConnectAsync(IPAddress[] addresses, int port) =>
             CompleteConnectAsync(Client.ConnectAsync(addresses, port));
 
+        /// <summary>
+        /// Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.
+        /// </summary>
+        /// <param name="remoteEP">The <see cref="IPEndPoint"/> to which you intend to connect.</param>
+        /// <returns>A task representing the asynchronous operation.</returns>
+        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));
 
+        /// <summary>
+        /// Connects the client to a remote TCP host using the specified endpoint as an asynchronous operation.
+        /// </summary>
+        /// <param name="remoteEP">The <see cref="IPEndPoint"/> to which you intend to connect.</param>
+        /// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
+        /// <returns>A task representing the asynchronous operation.</returns>
+        public ValueTask ConnectAsync(IPEndPoint remoteEP, CancellationToken cancellationToken) =>
+            CompleteConnectAsync(Client.ConnectAsync(remoteEP, cancellationToken));
+
         private async ValueTask CompleteConnectAsync(ValueTask task)
         {
             await task.ConfigureAwait(false);
index a2ce1a2..458e7f7 100644 (file)
@@ -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;
                 }