The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,816 other followers

C#/.NET/LINQ: DotNetStat: netstat.exe with TCP ports grouped by host

Posted by jpluimers on 2013/06/18

A lot of people have written .NET equivalents of netstat code. Basically there are two starting points:

I adapted the first, made the output very much like the built-in Windows netstat, and added some LINQ code to demonstrate grouping and ordering.

Now you get grouped output like this:

Distinct Remote Address:Port pairs by Remote Address:   443    443        6421, 19872     17500, 61678   443   80   443    443     443

The code below is part of the DotNetStat example.

It demonstrates a few important LINQ aspects beyond the LINQ Query Expressions (C# Programming Guide) intro and 101 LINQ Samples in C#.:

When you really want to dig deeply into LINQ, then Reimplementing LINQ to Objects: Part 45 – Conclusion and List of Posts – Jon Skeet: Coding Blog and LINQ – Jon Skeet: Coding Blog are.recommended reading.

Now a short table of C# query keywords and members in the framework:

keyword Member
from clause IEnumerable Interface (System.Collections) IEnumerable(T) Interface (System.Collections.Generic)
where clause Enumerable.Where Method (System.Linq)
select clause Type Relationships in LINQ Query Operations (C#) IEnumerable(T) Interface (System.Collections.Generic) IQueryable(T) Interface (System.Linq)
group clause IGrouping(TKey, TElement) Interface (System.Linq) Enumerable.GroupBy Method (System.Linq)
into Enumerable.GroupJoin Method (System.Linq)
orderby clause
Sorting Data.
Enumerable.OrderBy Method (System.Linq)
Enumerable.OrderByDescending Method (System.Linq)
Enumerable.ThenBy Method (System.Linq)
Enumerable.ThenByDescending Method (System.Linq)
 IComparable Interface (System)
IComparable(T) Interface (System)
join clause Enumerable.Join Method (System.Linq) Enumerable.GroupJoin Method (System.Linq)
let clause
ascending Enumerable.OrderBy Method (System.Linq) Enumerable.ThenBy Method (System.Linq)
descending Enumerable.OrderByDescending Method (System.Linq) Enumerable.ThenByDescending Method (System.Linq)
on Enumerable.Join Method (System.Linq) Enumerable.GroupJoin Method (System.Linq)
by Enumerable.GroupBy Method (System.Linq)
in Enumerable.Join Method (System.Linq) Enumerable.GroupJoin Method (System.Linq)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;

namespace DotNetStat
    class Program
        static void Main()
            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();

            if (tcpConnections.Length != 0)
                Console.WriteLine("Distinct Remote Address:Port pairs by Remote Address:");

                IEnumerable<IGrouping<IPAddress, TcpConnectionInformation>> tcpConnectionsByRemoteaddressGroups =
                    from tcpConnection in tcpConnections
                    //orderby tcpConnection.RemoteEndPoint.Address // "At least one object must implement IComparable." because Address has no IComparable
                    orderby tcpConnection.RemoteEndPoint.Address.ToString()
                    group tcpConnection by tcpConnection.RemoteEndPoint.Address;

                foreach (IGrouping<IPAddress, TcpConnectionInformation> tcpConnectionsByRemoteaddressGroup in tcpConnectionsByRemoteaddressGroups)
                    Console.Write("{0,-15}  ", tcpConnectionsByRemoteaddressGroup.Key);

                    IEnumerable allRemotePorts =
                        from tcpConnectionsByRemoteaddress in tcpConnectionsByRemoteaddressGroup
                        orderby tcpConnectionsByRemoteaddress.RemoteEndPoint.Port
                        select tcpConnectionsByRemoteaddress.RemoteEndPoint.Port;
                    IEnumerable distinctRemotePorts = allRemotePorts.Distinct();

                    bool first = true;
                    foreach (int distinctRemotePort in distinctRemotePorts)
                        if (!first)
                            Console.Write(", ");
                        first = false;


via: Build your own netstat.exe with c# | Tim Van Wassenhove.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: