From 860482402a7dc62766fc7c95dfb9b952cb401f1b Mon Sep 17 00:00:00 2001 From: Bruce Wayne Date: Sat, 14 Nov 2020 16:22:10 +0800 Subject: [PATCH] More accurate TCPing --- Netch/Utils/Utils.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Netch/Utils/Utils.cs b/Netch/Utils/Utils.cs index 386816aa..8062057c 100644 --- a/Netch/Utils/Utils.cs +++ b/Netch/Utils/Utils.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; @@ -39,12 +39,12 @@ namespace Netch.Utils public static async Task TCPingAsync(IPAddress ip, int port, int timeout = 1000, CancellationToken ct = default) { using var client = new TcpClient(ip.AddressFamily); + + var stopwatch = Stopwatch.StartNew(); + var task = client.ConnectAsync(ip, port); - var stopwatch = new Stopwatch(); - stopwatch.Start(); - - var resTask = await Task.WhenAny(Task.Delay(timeout, ct), task); + var resTask = await Task.WhenAny(task, Task.Delay(timeout, ct)); stopwatch.Stop(); if (resTask == task && client.Connected)