From 79f9ca0cbab2559375523b0c8ac1ce6b0e77a3cc Mon Sep 17 00:00:00 2001 From: yankejustin Date: Tue, 17 Mar 2015 18:30:12 -0400 Subject: [PATCH] Made sure the TcpClient will always kill itself Placed TcpClient in a try-finally to make sure it will never linger if it (likely) cannot connect successfully. --- Server/Core/Helper/UPnP.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Server/Core/Helper/UPnP.cs b/Server/Core/Helper/UPnP.cs index d264abfe..079f93c0 100644 --- a/Server/Core/Helper/UPnP.cs +++ b/Server/Core/Helper/UPnP.cs @@ -19,10 +19,19 @@ public static void ForwardPort(ushort port) { try { - TcpClient c = new TcpClient(); - c.Connect("www.google.com", 80); - endPoint = c.Client.LocalEndPoint; - c.Close(); + TcpClient c = null; + try + { + c = new TcpClient(); + c.Connect("www.google.com", 80); + endPoint = c.Client.LocalEndPoint; + + } + finally + { + // Placed in here to make sure that a failed TcpClient will never linger! + c.Close(); + } if (endPoint != null) { @@ -30,7 +39,7 @@ public static void ForwardPort(ushort port) int index = ipAddr.IndexOf(":"); ipAddr = ipAddr.Remove(index); } - // We got through. We may exit the loop. + // We got through successfully. We may exit the loop. break; } catch @@ -39,6 +48,8 @@ public static void ForwardPort(ushort port) } } while (retry < 5); + // As by the original UPnP, if we can't successfully connect (above), + // shouldn't we just "return;"? try { IStaticPortMappingCollection portMap = new UPnPNAT().StaticPortMappingCollection;