Skip to content

Instantly share code, notes, and snippets.

@jrusbatch
Created December 5, 2012 02:17
Show Gist options
  • Save jrusbatch/4211535 to your computer and use it in GitHub Desktop.
Save jrusbatch/4211535 to your computer and use it in GitHub Desktop.
Find an Available Port with C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;
namespace AvailablePort
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetAvailablePort(1000).ToString());
Console.ReadLine();
}
/// <summary>
/// checks for used ports and retrieves the first free port
/// </summary>
/// <returns>the free port or 0 if it did not find a free port</returns>
public static int GetAvailablePort(int startingPort)
{
IPEndPoint[] endPoints;
List<int> portArray = new List<int>();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
//getting active connections
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
portArray.AddRange(from n in connections
where n.LocalEndPoint.Port >= startingPort
select n.LocalEndPoint.Port);
//getting active tcp listners - WCF service listening in tcp
endPoints = properties.GetActiveTcpListeners();
portArray.AddRange(from n in endPoints
where n.Port >= startingPort
select n.Port);
//getting active udp listeners
endPoints = properties.GetActiveUdpListeners();
portArray.AddRange(from n in endPoints
where n.Port >= startingPort
select n.Port);
portArray.Sort();
for (int i = startingPort; i < UInt16.MaxValue; i++)
if (!portArray.Contains(i))
return i;
return 0;
}
}
}
@mniak
Copy link

mniak commented Nov 8, 2018

You could write the method like this

public static int GetAvailablePort(int startingPort)
{
    var properties = IPGlobalProperties.GetIPGlobalProperties();

    //getting active connections
    var tcpConnectionPorts = properties.GetActiveTcpConnections()
                        .Where(n => n.LocalEndPoint.Port >= startingPort)
                        .Select(n => n.LocalEndPoint.Port);

    //getting active tcp listners - WCF service listening in tcp
    var tcpListenerPorts = properties.GetActiveTcpListeners()
                        .Where(n => n.Port >= startingPort)
                        .Select(n => n.Port);

    //getting active udp listeners
    var udpListenerPorts = properties.GetActiveUdpListeners()
                        .Where(n => n.Port >= startingPort)
                        .Select(n => n.Port);

    var port = Enumerable.Range(startingPort, ushort.MaxValue)
        .Where(i => !tcpConnectionPorts.Contains(i))
        .Where(i => !tcpListenerPorts.Contains(i))
        .Where(i => !udpListenerPorts.Contains(i))
        .FirstOrDefault();

    return port;
}

@JobaDiniz
Copy link

JobaDiniz commented Nov 30, 2018

What about Find the next TCP port in .Net? What are the advantage and disadvantage about the two methods?

static int FreeTcpPort()
{
  TcpListener l = new TcpListener(IPAddress.Loopback, 0);
  l.Start();
  int port = ((IPEndPoint)l.LocalEndpoint).Port;
  l.Stop();
  return port;
}

@sellep
Copy link

sellep commented Mar 8, 2019

With the TcpListener approach I already got two Bluescreens:
Probably caused by : tcpip.sys ( tcpip!TcpDeliverLoopbackDataToClient+24c )

@marbel82
Copy link

marbel82 commented Aug 31, 2020

But... If you want to open empty port, you should let the system do the work for you:

var udp = new UdpClient(0, AddressFamily.InterNetwork);
int port = ((IPEndPoint)udp.Client.LocalEndPoint).Port;

Because... another process may open the port returning by GetAvailablePort() function, before you.

@alex-basiuk
Copy link

alex-basiuk commented Oct 26, 2020

A shorter version:

public static int GetAvailablePort(int startingPort)
{
    if (startingPort > ushort.MaxValue) throw new ArgumentException($"Can't be greater than {ushort.MaxValue}", nameof(startingPort));
    var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

    var connectionsEndpoints = ipGlobalProperties.GetActiveTcpConnections().Select(c => c.LocalEndPoint);
    var tcpListenersEndpoints = ipGlobalProperties.GetActiveTcpListeners();
    var udpListenersEndpoints = ipGlobalProperties.GetActiveUdpListeners();
    var portsInUse = connectionsEndpoints.Concat(tcpListenersEndpoints)
                                         .Concat(udpListenersEndpoints)
                                         .Select(e => e.Port);

    return Enumerable.Range(startingPort, ushort.MaxValue - startingPort + 1).Except(portsInUse).FirstOrDefault();
}

@Zhenia0
Copy link

Zhenia0 commented May 5, 2021

You could write the method like this

public static int GetAvailablePort(int startingPort)
{
    var properties = IPGlobalProperties.GetIPGlobalProperties();

    //getting active connections
    var tcpConnectionPorts = properties.GetActiveTcpConnections()
                        .Where(n => n.LocalEndPoint.Port >= startingPort)
                        .Select(n => n.LocalEndPoint.Port);

    //getting active tcp listners - WCF service listening in tcp
    var tcpListenerPorts = properties.GetActiveTcpListeners()
                        .Where(n => n.Port >= startingPort)
                        .Select(n => n.Port);

    //getting active udp listeners
    var udpListenerPorts = properties.GetActiveUdpListeners()
                        .Where(n => n.Port >= startingPort)
                        .Select(n => n.Port);

    var port = Enumerable.Range(startingPort, ushort.MaxValue)
        .Where(i => !tcpConnectionPorts.Contains(i))
        .Where(i => !tcpListenerPorts.Contains(i))
        .Where(i => !udpListenerPorts.Contains(i))
        .FirstOrDefault();

    return port;
}

not bad but didn't work

@HennyH
Copy link

HennyH commented May 15, 2021

public static class IpUtilities
{
    private const ushort MIN_PORT = 1;
    private const ushort MAX_PORT = UInt16.MaxValue;
    public static int? GetAvailablePort(ushort lowerPort = MIN_PORT, ushort upperPort = MAX_PORT)
    {
        var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
        var usedPorts = Enumerable.Empty<int>()
            .Concat(ipProperties.GetActiveTcpConnections().Select(c => c.LocalEndPoint.Port))
            .Concat(ipProperties.GetActiveTcpListeners().Select(l => l.Port))
            .Concat(ipProperties.GetActiveUdpListeners().Select(l => l.Port))
            .ToHashSet();
        for (int port = lowerPort; port <= upperPort; port++)
        {
            if (!usedPorts.Contains(port)) return port;
        }
        return null;
    }
}

@JVimes
Copy link

JVimes commented May 5, 2022

Just want to draw attention to @marbel82's comment. Finding an unused port number yourself has the problem that another process could steal the port before you bind to it. Suggest using your networking library's facility for letting the system pick an unused port and immediately bind it (e.g. specifying zero for the port number in the UdpClient constructor). Then read the port number it picked, if you still need to (UdpClient example).

@TamasSzalisznyi
Copy link

I'm using to get a free port as @marbel82 suggested and it is working just fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment