/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System.Net; using System.Net.Sockets; namespace wasSharpNET.Network.TCP { public static class Utilities { /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Try to find an unused port. /// /// the address associated with the port /// an integer to hold the port if found /// true if an unused port could be found public static bool TryGetUnusedPort(IPAddress address, out int port) { try { var l = new TcpListener(address, 0); l.Start(); port = ((IPEndPoint) l.LocalEndpoint).Port; l.Stop(); return true; } catch { port = default(int); return false; } } } }