Search code examples
.nettcpclient

TcpClient ConnectAsync with multiple IP addresses - what's the behavior?


(context: a service provides monitoring capabilities to external web sites; customer provides a URL and we need to ping it; we're switching from HttpClient to TcpClient to provide visibility DNS/Connect/SSL/Send/Receive phases)

Here is a summary what code does:

IPHostEntry host = await Dns.GetHostEntryAsync(hostname).ConfigureAwait(false);
await this.client.ConnectAsync(host.AddressList, port).ConfigureAwait(false);

GetHostEntryAsync returns a list of IP addresses associated with a host.

What does TcpClient do with them? Does it try to establish a connection to the first, then if fails to the second, etc.?


Solution

  • It tries them one at a time, and returns as soon as the first one succeeded or all have failed.