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.
This commit is contained in:
yankejustin 2015-03-17 18:30:12 -04:00
parent 7fd83c87be
commit 79f9ca0cba
1 changed files with 16 additions and 5 deletions

View File

@ -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;