Skip to content

Commit 42c8498

Browse files
authored
Resolve IP addresses in ConnectCallback example (#10931)
1 parent dbcb811 commit 42c8498

File tree

1 file changed

+5
-3
lines changed
  • snippets/csharp/System.Net.Http/SocketsHttpHandler/ConnectCallback

1 file changed

+5
-3
lines changed

snippets/csharp/System.Net.Http/SocketsHttpHandler/ConnectCallback/program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.IO;
1+
using System;
32
using System.Net;
43
using System.Net.Http;
54
using System.Net.Sockets;
@@ -14,14 +13,17 @@ static async Task Main()
1413

1514
handler.ConnectCallback = async (ctx, ct) =>
1615
{
16+
DnsEndPoint dnsEndPoint = ctx.DnsEndPoint;
17+
IPAddress[] addresses = await Dns.GetHostAddressesAsync(dnsEndPoint.Host, dnsEndPoint.AddressFamily, ct);
1718
var s = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
1819
try
1920
{
2021
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
2122
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 5);
2223
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 5);
2324
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 5);
24-
await s.ConnectAsync(ctx.DnsEndPoint, ct);
25+
26+
await s.ConnectAsync(addresses, dnsEndPoint.Port, ct);
2527
return new NetworkStream(s, ownsSocket: true);
2628
}
2729
catch

0 commit comments

Comments
 (0)