tags:

views:

58

answers:

2

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?

A: 

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

Krzysztof Kowalczyk
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.
publicENEMY
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 http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net, 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).
Krzysztof Kowalczyk
A: 

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();
        }
    }
}
Preet Sangha
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.
publicENEMY