9

What is the best way to check if the UDP port is open or not on the same machine. I have got port number 7525UDP and if it's open I would like to bind to it. I am using this code:

while (true) 
{ 

  try {socket.bind()}

  catch (Exception ex) 

  {MessageBox.Show("socket probably in use");}
}

but is there a specified function that can check if the UDP port is open or not. Without sweeping the entire table set for UDP ports would be also good.

1
  • If you want to open empty port, you should let the system do the work for you: var udp = new UdpClient(0); int port = ((IPEndPoint)udp.Client.LocalEndPoint).Port;
    – marbel82
    Commented Aug 31, 2020 at 20:59

1 Answer 1

26
int myport = 7525;
bool alreadyinuse = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners().Any(p => p.Port == myport);

A comment below suggested a variation which would supply the first free UDP port... however, the suggested code is inefficient as it calls out to the external assembly multiple times (depending on how many ports are in use). Here's a more efficient variation which will only call the external assembly once (and is also more readable):

    var startingAtPort = 5000;
    var maxNumberOfPortsToCheck = 500;
    var range = Enumerable.Range(startingAtPort, maxNumberOfPortsToCheck);
    var portsInUse = 
        from p in range
            join used in System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()
                on p equals used.Port
                    select p;

    var FirstFreeUDPPortInRange = range.Except(portsInUse).FirstOrDefault();

    if(FirstFreeUDPPortInRange > 0)
    {
         // do stuff
         Console.WriteLine(FirstFreeUDPPortInRange);
    } else {
         // complain about lack of free ports?
    }
6
  • 1
    :-) nice touch although it isn't what i was looking for but it's the next best thing. Certainly does the job tested and it is working :-) thank you.
    – Thomas
    Commented May 9, 2011 at 14:14
  • Starting from .NET 4 this method works for both IP v4 and IP v6. However, it only works for IP v4 for all old .NET releases, .NET 2.0/3.0/3.5 if you are familiar with how to interpret MSDN documentation.
    – Lex Li
    Commented Mar 6, 2013 at 7:58
  • Think it should be a > 0 comparison in case of multiple readers. Commented Aug 11, 2014 at 17:47
  • 1
    here is an alteration that returns the first free port on a range Enumerable.Range(5000, 6000).First(p => !IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners().Any(l => l.Port == p))
    – aL3891
    Commented Jan 5, 2015 at 14:33
  • 1
    I just want to say tank you for your answer it was very helpful for me.
    – iman_sh
    Commented Apr 18, 2019 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.