From 87587d1e7674442dab52576615fdad6b42eafa9b Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sat, 24 Jul 2021 20:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E6=97=8B=E7=AD=89=E5=BE=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/DnsServer.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/FastGithub.Dns/DnsServer.cs b/FastGithub.Dns/DnsServer.cs index a7c5650..6116aa3 100644 --- a/FastGithub.Dns/DnsServer.cs +++ b/FastGithub.Dns/DnsServer.cs @@ -50,9 +50,24 @@ namespace FastGithub.Dns const int SIO_UDP_CONNRESET = unchecked((int)0x9800000C); this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]); } - this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - this.socket.Bind(new IPEndPoint(address, port)); + + var maxSpinCount = 100; + var spinWait = new SpinWait(); + var localEndPoint = new IPEndPoint(address, port); + + for (var i = 0; i < maxSpinCount; i++) + { + try + { + this.socket.Bind(localEndPoint); + break; + } + catch (SocketException ex) when (ex.SocketErrorCode == SocketError.AccessDenied) + { + spinWait.SpinOnce(); + } + } } ///