Get Available Socket Port : IP Address « Network « C# / C Sharp






Get Available Socket Port

        
// /* **********************************************************************************
//  *
//  * Copyright (c) Sky Sanders. All rights reserved.
//  * 
//  * This source code is subject to terms and conditions of the Microsoft Public
//  * License (Ms-PL). A copy of the license can be found in the license.htm file
//  * included in this distribution.
//  *
//  * You must not remove this notice, or any other, from this software.
//  *
//  * **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;

namespace Salient.Net
{
    public static class IPUtility
    {
        /// <summary>
        /// Returns first available port on the specified IP address. The port scan excludes ports that are open on ANY loopback adapter. 
        /// If the address upon which a port is requested is an 'ANY' address all ports that are open on ANY IP are excluded.
        /// </summary>
        /// <param name="rangeStart"></param>
        /// <param name="rangeEnd"></param>
        /// <param name="ip">The IP address upon which to search for available port.</param>
        /// <param name="includeIdlePorts">If true includes ports in TIME_WAIT state in results. TIME_WAIT state is typically cool down period for recently released ports.</param>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public static ushort GetAvailablePort(UInt16 rangeStart, UInt16 rangeEnd, IPAddress ip, bool includeIdlePorts)
        {
            IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties();

            // if the ip we want a port on is an 'any' or loopback port we need to exclude all ports that are active on any IP

            Func<IPAddress, bool> isIpAnyOrLoopBack = i => IPAddress.Any.Equals(i) ||
                                                           IPAddress.IPv6Any.Equals(i) ||
                                                           IPAddress.Loopback.Equals(i) ||
                                                           IPAddress.IPv6Loopback.
                                                               Equals(i);
            // get all active ports on specified IP. 

            List<ushort> excludedPorts = new List<ushort>();


            // if a port is open on an 'any' or 'loopback' interface then include it in the excludedPorts

            excludedPorts.AddRange(from n in ipProps.GetActiveTcpConnections()
                                   where
                                       n.LocalEndPoint.Port >= rangeStart
                                       && n.LocalEndPoint.Port <= rangeEnd
                                       &&
                                       (isIpAnyOrLoopBack(ip) || n.LocalEndPoint.Address.Equals(ip) ||
                                        isIpAnyOrLoopBack(n.LocalEndPoint.Address))
                                       && (!includeIdlePorts || n.State != TcpState.TimeWait)
                                   select (ushort) n.LocalEndPoint.Port);

            excludedPorts.AddRange(from n in ipProps.GetActiveTcpListeners()
                                   where n.Port >= rangeStart && n.Port <= rangeEnd
                                         &&
                                         (isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
                                   select (ushort) n.Port);

            excludedPorts.AddRange(from n in ipProps.GetActiveUdpListeners()
                                   where n.Port >= rangeStart && n.Port <= rangeEnd
                                         &&
                                         (isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
                                   select (ushort) n.Port);

            excludedPorts.Sort();


            for (ushort port = rangeStart; port <= rangeEnd; port++)
            {
                if (!excludedPorts.Contains(port))
                {
                    return port;
                }
            }

            return 0;
        }

        public static NetworkInterface[] GetActiveNetworkInterfaces()
        {
            return
                (NetworkInterface.GetAllNetworkInterfaces().Where(p => p.OperationalStatus == OperationalStatus.Up)).
                    ToArray();
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.IPAddress AddressFamily
2.Parse an IPAddress
3.Loopback IPAddress
4.Broadcast IPAddress
5.Any IPAddress
6.None IPAddress
7.IsLoopback IPAddress
8.IP Address parse, lookup IP Address parse, lookup
9.IPEndPoint sampleIPEndPoint sample
10.Get Host By Name, Get Host Name
11.Get Host Entry
12.Parse Host String
13.Get Current Ip Address
14.Get Host IP Address
15.Get IP address by query whatismyip.com
16.Get Local IP Address
17.IP to value
18.Get user IP from HttpContext
19.IP to Uint
20.UInt32 To IP Address
21.IP Address To UInt 32
22.Get IP address from notation (xxx.xxx.xxx.xxx) or hostname
23.Gets my local IP address (not necessarily external) and subnet mask
24.Returns true if the IPEndPoint supplied is on the same subnet as this host
25.Is Ip Address By Regular Expression
26.Checks if a given string is a valid IP address.
27.IP Address To NumberIP Address To Number
28.Get the description of a Enum value.
29.Get Subnet Mask