Use NetworkInformation classes to get the list of listening ports on your machine

Published 14 March 05 12:01 PM | adarshk 

.Net frameworks 2.0 have new addition of Namespace, System.Net.NetworkInformation, it provide a number of interesting classes to extract the network related statistics and state of the machine, it pretty much provide most of the functionality which is exposed by native IPHelper APIs.

Earlier I had shown a simple example for getting network availbility event notification. Here is another simple 4 line example, where your application could check all the listening ports on the machine.

using System;
using System.Net;
using System.Net.NetworkInformation;
public class Test
{
      public static void Main()
      {
            IPGlobalProperties ipGlobal = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] connections = ipGlobal.GetActiveTcpListeners ( ) ;
            foreach(IPEndPoint ipe in connections)
           Console.WriteLine("Listening IPEndPoint = "+ipe.Port);
      }

}

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Lionel Fourquaux said on March 14, 2005 12:59 PM:
Interesting! It would be even better if it provided a way to get the process associated to each end point (as netstat -o does).
# John Schroedl said on March 22, 2005 11:23 AM:
Very nice!

Can you comment or blog on whether we'll be able to connect via HttpWebRequest through a proxy configured with an automatic configuration script? I cannot write managed code to go beyond our firewall without this.

John
# adarshk said on March 22, 2005 12:35 PM:
Hi Lionel,
Thanks for pointing this out, current 2.0 would not allow to get process assicated to the endpoint, we have this feature in our to do list for future version.
Thanks
# adarshk said on March 22, 2005 12:37 PM:
Hi John,
Yes Autoproxy support was one of the highly requested feature, it is supported in Whidbey (v2.0) version.

Thanks
adarsh
# Feed Search Engine - All Fresh Articles And News Are Here said on November 25, 2007 9:52 AM:

PingBack from http://feeds.maxblog.eu/item_106773.html

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker