1

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?

2
  • 1
    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. Commented Oct 18, 2010 at 2:50
  • You were right. I should have named it ListUsedTCPPort. Something must got into my head while im writing it. Renaming it now. Commented Oct 18, 2010 at 5:30

2 Answers 2

6

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

2 Comments

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.
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).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.