Created
June 2, 2022 10:19
-
-
Save mrpmorris/70531aeb3cdab6bcae5e7ea985b4c96b to your computer and use it in GitHub Desktop.
Slow TcpClient DNS resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Sockets; | |
int iterations = 10; | |
long totalElapsed = 0; | |
for (int i = 0; i < iterations; i++) | |
{ | |
var tcpClient = new TcpClient(); | |
var sw = System.Diagnostics.Stopwatch.StartNew(); | |
await tcpClient.ConnectAsync("localhost", 6510); | |
long elapsed = sw.ElapsedMilliseconds; | |
totalElapsed += elapsed; | |
Console.WriteLine("Connected"); | |
tcpClient.Dispose(); | |
} | |
Console.WriteLine($"Average {totalElapsed / iterations}"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.Net.Sockets; | |
var endPoint = new IPEndPoint(IPAddress.Any, 6510); | |
var listener = new TcpListener(endPoint); | |
listener.Start(); | |
_ = Task.Run(() => ListenAsync(listener)); | |
Console.WriteLine("Listening"); | |
Console.ReadLine(); | |
async Task ListenAsync(TcpListener listener) | |
{ | |
while (true) | |
{ | |
Socket clientSocket = await listener.AcceptSocketAsync(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment