Click here to Skip to main content
12,952,273 members (63,235 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 13 Oct 2011

Find an open port on a machine using C#

, 13 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to find an open port in a machine using C#.
Include these namespaces in your class:
using System.Net.NetworkInformation;
using System.Net;

The actual method to get the first open port from the system is as follows:
private string GetOpenPort()
  int PortStartIndex = 1000;
  int PortEndIndex = 2000;
  IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
  IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();
  List<int> usedPorts = tcpEndPoints.Select(p => p.Port).ToList<int>();
  int unusedPort = 0;
  for (int port = PortStartIndex; port < PortEndIndex; port++)
     if (!usedPorts.Contains(port))
        unusedPort = port;
  return unusedPort.ToString();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Balaji Birajdar (India)
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.

Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.

I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .

I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

You may also be interested in...


Comments and Discussions

SuggestionTitle is a bit misleading Pin
Topheman6-Jul-14 12:37
memberTopheman6-Jul-14 12:37 
GeneralMy vote of 4 Pin
shaijujanardhanan19-Nov-12 20:37
membershaijujanardhanan19-Nov-12 20:37 
GeneralReason for my vote of 5 Good one Pin
Emanuel DSouza28-Feb-12 19:29
memberEmanuel DSouza28-Feb-12 19:29 
GeneralI would like to point out that a new connection could be mad... Pin
Daniel.Grondal17-Oct-11 20:31
memberDaniel.Grondal17-Oct-11 20:31 
GeneralReason for my vote of 5 Good trick. Pin
Sergio Andrés Gutiérrez Rojas17-Oct-11 19:25
memberSergio Andrés Gutiérrez Rojas17-Oct-11 19:25 
GeneralI think the functions name is wrong. It's not returning an o... Pin
Reto Ravasio13-Oct-11 13:19
memberReto Ravasio13-Oct-11 13:19 
GeneralReason for my vote of 4 I suggest you included the necessary... Pin
USABebopKid13-Oct-11 5:52
memberUSABebopKid13-Oct-11 5:52 
GeneralRe: Thank you USABebopKid. Implemented your suggestion Pin
Balaji Birajdar13-Oct-11 5:55
memberBalaji Birajdar13-Oct-11 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 13 Oct 2011
Article Copyright 2011 by Balaji Birajdar (India)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid