wasSharpNET – Rev 27

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  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    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Try to find an unused port.
        /// </summary>
        /// <param name="address">the address associated with the port</param>
        /// <param name="port">an integer to hold the port if found</param>
        /// <returns>true if an unused port could be found</returns>
        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;
            }
        }
    }
}

Generated by GNU Enscript 1.6.5.90.