-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
discrepancy between TcpClient constructor and Connect method #67879
Comments
Tagging subscribers to this area: @dotnet/ncl Issue Details
using System.Net.Sockets;
string host = "localhost";
var client1 = new TcpClient();
client1.Connect(host, 8080);
Console.WriteLine(client1.Client.LocalEndPoint);
var client2 = new TcpClient();
client2.ConnectAsync(host, 8080).GetAwaiter().GetResult();
Console.WriteLine(client2.Client.LocalEndPoint);
var client3 = new TcpClient(host, 8080);
Console.WriteLine(client3.Client.LocalEndPoint); will produce something like
So in some cases use dual mode socket and sometimes we use socket for specific address family. Also documentation (https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.-ctor?view=net-6.0#system-net-sockets-tcpclient-ctor(system-string-system-int32)) claims that IPv6 will be preferred when available but that is not what the code really does: runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs Lines 144 to 150 in a074a2d
While somebody may depend on this particular behavior, it seems like it would make sense to decease number of related to #63162
|
Triage: Makes sense to make the behavior consistent (and predictable) - we should remove the cruft in |
TcpClient
has public constructor that takes host name and port and when used, it connects underlying Socket. However the behavior is different when creatingTcpClient
and callingConnect
method to the same host.will produce something like
So in some cases use dual mode socket and sometimes we use socket for specific address family.
Connect
andConnectAsync
produce same result in example above (localhost resolves to single IPv4 address) butConnectAsync
useSocket.ConnectAsync
while the synchronous implementation use implementation fromTcpClient
and that can produce different results.Also documentation (https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.-ctor?view=net-6.0#system-net-sockets-tcpclient-ctor(system-string-system-int32)) claims that IPv6 will be preferred when available but that is not what the code really does:
runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs
Lines 144 to 150 in a074a2d
While somebody may depend on this particular behavior, it seems like it would make sense to decease number of
Connect
implementations and overall behavior variations.related to #63162
The text was updated successfully, but these errors were encountered: