List used TCP port using C#

13,081

Solution 1

I get the same result:

alt text

But it does also show listeners (ipGlobalProperties.GetActiveTcpListeners()) which may or may not be closed down.

using your example (with an extra Console.WriteLine in there

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Collections;

namespace ConsoleApplication1 {

    static class Program {
        //List used tcp port
        static void ListAvailableTCPPort(ref ArrayList usedPort) {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

            while (myEnum.MoveNext()) {
                TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
                Console.WriteLine("Port {0} {1} {2} ", TCPInfo.LocalEndPoint, TCPInfo.RemoteEndPoint, TCPInfo.State);
                usedPort.Add(TCPInfo.LocalEndPoint.Port);
            }
        }

        public static void Main(){
            ArrayList usedPorts = new ArrayList();
            ListAvailableTCPPort(ref usedPorts);
            Console.ReadKey();
        }
    }
}

Solution 2

It's a bit of a guess but TCPView probably also shows listener tcp ports (ipGlobalProperties.GetActiveTcpListeners())

Share:
13,081
Syaiful Nizam Yahya
Author by

Syaiful Nizam Yahya

Updated on November 20, 2022

Comments

  • Syaiful Nizam Yahya
    Syaiful Nizam Yahya over 1 year

    This is the answer to my questions.

    How to list binded/used TCP port in C#. Used modified code from jro

            static void ListUsedTCPPort(ref ArrayList usedPort)
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();
    
            while (myEnum.MoveNext())
            {
                IPEndPoint TCPInfo = (IPEndPoint)myEnum.Current;
                usedPort.Add(TCPInfo.Port);
            }
        }
    

    Original questions. This is how i list TCP port using C#. It is modified code i found in this forum(forgot exactly where i got it. If you are the original developer, notify me and ill put credits where due.)

        //List used tcp port
        static void ListUsedTCPPort(ref ArrayList usedPort)
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();
    
            while (myEnum.MoveNext())
            {
                TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
                usedPort.Add(TCPInfo.LocalEndPoint.Port);
            }
        }
    

    Problem is, the results is different from used tcp port listed in TCPview(Protocol-TCP, Local port). By the way, i do know that this list used TCP port at the EXACT time its called. What did i did wrong?

    • Xaqron
      Xaqron over 13 years
      Why the name of function is 'ListAvailableTCPPort' when it returns used ports ? and providing two different list from both of them will help to find the problem source.
    • Syaiful Nizam Yahya
      Syaiful Nizam Yahya over 13 years
      You were right. I should have named it ListUsedTCPPort. Something must got into my head while im writing it. Renaming it now.
  • Syaiful Nizam Yahya
    Syaiful Nizam Yahya over 13 years
    Thanks. Now I know what my problem is. What I really need is to see the port number whether it is currently connected or not. The method above does not list port number that are not currently connected.
  • Syaiful Nizam Yahya
    Syaiful Nizam Yahya over 13 years
    can i just use GetActiveTcpListeners() only? i mean, it also get the active/connected tcp port. My objective is to successfully create a server without knowing what port to use.
  • Krzysztof Kowalczyk
    Krzysztof Kowalczyk over 13 years
    Using neither GetActiveTcpListeners() nor GetActiveTcpConnections() is a good idea - another process can acquire the port between the time you check if port is available and the time you try to use it. As described at stackoverflow.com/questions/138043/…, you can start listening on port number 0 and system will assign you unused port. Another method is to start from a random port number and try keep opening it until you succeed (and go to the next number if you fail).