SDF Sample: More Network Statistics than you could ever want

So, you want network statistics for your Windows CE or Windows Mobile device?  I think the new Smart Device Framework 2.2’s NetworkInformation namespace might just give you what you’re after.  It’s got almost everything the desktop gives, plus we even provide access to the ARP and routing tables (something you *don’t* get on the desktop).  Here’s a complete sample on getting the supported statistics:


IPGlobalProperties globalProps = IPGlobalProperties.GetIPGlobalProperties();
Debug.WriteLine(“rnGlobal IP Propertiesrn====================”);
Debug.WriteLine(string.Format(” Host Name: {0}”, globalProps.HostName));
Debug.WriteLine(string.Format(” Domain Name: {0}”, globalProps.DomainName));
Debug.WriteLine(string.Format(” DHCP Scope Name: {0}”, globalProps.DhcpScopeName));

IPGlobalStatistics globalStatsv4 = globalProps.GetIPv4GlobalStatistics();
Debug.WriteLine(“rnGlobal IPv4 Statisticsrn====================”);
Debug.WriteLine(string.Format(” Packets Received: {0}”, globalStatsv4.ReceivedPackets));
Debug.WriteLine(string.Format(” Packets Delivered: {0}”, globalStatsv4.ReceivedPacketsDelivered));
Debug.WriteLine(string.Format(” Packets Discarded: {0}”, globalStatsv4.ReceivedPacketsDiscarded));
Debug.WriteLine(string.Format(” Packets Forwarded: {0}”, globalStatsv4.ReceivedPacketsForwarded));
Debug.WriteLine(string.Format(” Packets Received w/ Address Errors: {0}”, globalStatsv4.ReceivedPacketsWithAddressErrors));
Debug.WriteLine(string.Format(” Packets Received w/ Header Errors: {0}”, globalStatsv4.ReceivedPacketsWithHeadersErrors));
Debug.WriteLine(string.Format(” Packets Received w/ Protocol Errors: {0}”, globalStatsv4.ReceivedPacketsWithUnknownProtocol));
Debug.WriteLine(string.Format(” Default TTL: {0}”, globalStatsv4.DefaultTtl));
Debug.WriteLine(string.Format(” Forwarding {0} Enabled”, globalStatsv4.ForwardingEnabled ? “IS” : “IS NOT”));
Debug.WriteLine(string.Format(” Interfaces: {0}”, globalStatsv4.NumberOfInterfaces));
Debug.WriteLine(string.Format(” IP Addresses: {0}”, globalStatsv4.NumberOfIPAddresses));
Debug.WriteLine(string.Format(” Routes: {0}”, globalStatsv4.NumberOfRoutes));
Debug.WriteLine(string.Format(” Output Packet Requests: {0}”, globalStatsv4.OutputPacketRequests));
Debug.WriteLine(string.Format(” Output Routing Discards: {0}”, globalStatsv4.OutputPacketRoutingDiscards));
Debug.WriteLine(string.Format(” Output Packet Discards: {0}”, globalStatsv4.OutputPacketsDiscarded));
Debug.WriteLine(string.Format(” Output Packets w/o Route: {0}”, globalStatsv4.OutputPacketsWithNoRoute));
Debug.WriteLine(string.Format(” Fragmented Packets: {0}”, globalStatsv4.PacketsFragmented));
Debug.WriteLine(string.Format(” Fragment Failures: {0}”, globalStatsv4.PacketFragmentFailures));
Debug.WriteLine(string.Format(” Reassemblies Required: {0}”, globalStatsv4.PacketReassembliesRequired));
Debug.WriteLine(string.Format(” Reassemblies Succeeded: {0}”, globalStatsv4.PacketsReassembled));
Debug.WriteLine(string.Format(” Reassembly Failures: {0}”, globalStatsv4.PacketReassemblyFailures));
Debug.WriteLine(string.Format(” Reassembly Timeouts: {0}”, globalStatsv4.PacketReassemblyTimeout));

#region — IPv6 stats —
try
{
    Debug.WriteLine(“rnGlobal IPv6 Statisticsrn====================”);
    IPGlobalStatistics globalStatsv6 = globalProps.GetIPv6GlobalStatistics();
    Debug.WriteLine(string.Format(” Packets Received: {0}”, globalStatsv6.ReceivedPackets));
    Debug.WriteLine(string.Format(” Packets Delivered: {0}”, globalStatsv6.ReceivedPacketsDelivered));
    Debug.WriteLine(string.Format(” Packets Discarded: {0}”, globalStatsv6.ReceivedPacketsDiscarded));
    Debug.WriteLine(string.Format(” Packets Forwarded: {0}”, globalStatsv6.ReceivedPacketsForwarded));
    Debug.WriteLine(string.Format(” Packets Received w/ Address Errors: {0}”, globalStatsv6.ReceivedPacketsWithAddressErrors));
    Debug.WriteLine(string.Format(” Packets Received w/ Header Errors: {0}”, globalStatsv6.ReceivedPacketsWithHeadersErrors));
    Debug.WriteLine(string.Format(” Packets Received w/ Protocol Errors: {0}”, globalStatsv6.ReceivedPacketsWithUnknownProtocol));
    Debug.WriteLine(string.Format(” Default TTL: {0}”, globalStatsv6.DefaultTtl));
    Debug.WriteLine(string.Format(” Forwarding {0} Enabled”, globalStatsv6.ForwardingEnabled ? “IS” : “IS NOT”));
    Debug.WriteLine(string.Format(” Interfaces: {0}”, globalStatsv6.NumberOfInterfaces));
    Debug.WriteLine(string.Format(” IP Addresses: {0}”, globalStatsv6.NumberOfIPAddresses));
    Debug.WriteLine(string.Format(” Routes: {0}”, globalStatsv6.NumberOfRoutes));
    Debug.WriteLine(string.Format(” Output Packet Requests: {0}”, globalStatsv6.OutputPacketRequests));
    Debug.WriteLine(string.Format(” Output Routing Discards: {0}”, globalStatsv6.OutputPacketRoutingDiscards));
    Debug.WriteLine(string.Format(” Output Packet Discards: {0}”, globalStatsv6.OutputPacketsDiscarded));
    Debug.WriteLine(string.Format(” Output Packets w/o Route: {0}”, globalStatsv6.OutputPacketsWithNoRoute));
    Debug.WriteLine(string.Format(” Fragmented Packets: {0}”, globalStatsv6.PacketsFragmented));
    Debug.WriteLine(string.Format(” Fragment Failures: {0}”, globalStatsv6.PacketFragmentFailures));
    Debug.WriteLine(string.Format(” Reassemblies Required: {0}”, globalStatsv6.PacketReassembliesRequired));
    Debug.WriteLine(string.Format(” Reassemblies Succeeded: {0}”, globalStatsv6.PacketsReassembled));
    Debug.WriteLine(string.Format(” Reassembly Failures: {0}”, globalStatsv6.PacketReassemblyFailures));
    Debug.WriteLine(string.Format(” Reassembly Timeouts: {0}”, globalStatsv6.PacketReassemblyTimeout));
    Debug.WriteLine(string.Format(” Packets Received: {0}”, globalStatsv6.ReceivedPackets));
}
catch (PlatformNotSupportedException)
{
    Debug.WriteLine(” IPv6 Not supported”);
}
#endregion

#region — Icmpv4 stats —
IcmpV4Statistics icmpstats = globalProps.GetIcmpV4Statistics();
Debug.WriteLine(“rnICMPv4 Statisticsrn====================”);
Debug.WriteLine(string.Format(” Address Mask Requests Received: {0}”, icmpstats.AddressMaskRequestsReceived));
Debug.WriteLine(string.Format(” Address Mask Requests Sent: {0}”, icmpstats.AddressMaskRequestsSent));
Debug.WriteLine(string.Format(” Address Mask Replies Received: {0}”, icmpstats.AddressMaskRepliesReceived));
Debug.WriteLine(string.Format(” Address Mask Replies Sent: {0}”, icmpstats.AddressMaskRepliesSent));
Debug.WriteLine(string.Format(” Dest Unreachable Messages Received: {0}”, icmpstats.DestinationUnreachableMessagesReceived));
Debug.WriteLine(string.Format(” Dest Unreachable Messages Sent: {0}”, icmpstats.DestinationUnreachableMessagesSent));
Debug.WriteLine(string.Format(” Echo Replies Received: {0}”, icmpstats.EchoRepliesReceived));
Debug.WriteLine(string.Format(” Echo Replies Sent: {0}”, icmpstats.EchoRepliesSent));
Debug.WriteLine(string.Format(” Echo Requests Received: {0}”, icmpstats.EchoRequestsReceived));
Debug.WriteLine(string.Format(” Echo Requests Sent: {0}”, icmpstats.EchoRequestsSent));
Debug.WriteLine(string.Format(” Errors Received: {0}”, icmpstats.ErrorsReceived));
Debug.WriteLine(string.Format(” Errors Sent: {0}”, icmpstats.ErrorsSent));
Debug.WriteLine(string.Format(” Messages Received: {0}”, icmpstats.MessagesReceived));
Debug.WriteLine(string.Format(” Messages Sent: {0}”, icmpstats.MessagesSent));
Debug.WriteLine(string.Format(” Parameter Problems Received: {0}”, icmpstats.ParameterProblemsReceived));
Debug.WriteLine(string.Format(” Parameter Problems Sent: {0}”, icmpstats.ParameterProblemsSent));
Debug.WriteLine(string.Format(” Redirects Received: {0}”, icmpstats.RedirectsReceived));
Debug.WriteLine(string.Format(” Redirects Sent: {0}”, icmpstats.RedirectsSent));
Debug.WriteLine(string.Format(” Source Quenches Received: {0}”, icmpstats.SourceQuenchesReceived));
Debug.WriteLine(string.Format(” Source Quenches Sent: {0}”, icmpstats.SourceQuenchesSent));
Debug.WriteLine(string.Format(” Time Exceeded Messages Received: {0}”, icmpstats.TimeExceededMessagesReceived));
Debug.WriteLine(string.Format(” Time Exceeded Messages Sent: {0}”, icmpstats.TimeExceededMessagesSent));
Debug.WriteLine(string.Format(” Timestamp Replies Received: {0}”, icmpstats.TimestampRepliesReceived));
Debug.WriteLine(string.Format(” Timestamp Replies Sent: {0}”, icmpstats.TimestampRepliesSent));
Debug.WriteLine(string.Format(” Timestamp Requests Received: {0}”, icmpstats.TimestampRequestsReceived));
Debug.WriteLine(string.Format(” Timestamp Requests Sent: {0}”, icmpstats.TimestampRequestsSent));
#endregion

#region — TCPIPv4 stats —
TcpStatistics tcpstats = globalProps.GetTcpIPv4Statistics();
Debug.WriteLine(“rnTCP/IP v4 Statisticsrn====================”);
Debug.WriteLine(string.Format(” Connections Accepted: {0}”, tcpstats.ConnectionsAccepted));
Debug.WriteLine(string.Format(” Connections Initiated: {0}”, tcpstats.ConnectionsInitiated));
Debug.WriteLine(string.Format(” Cumulative Connections: {0}”, tcpstats.CumulativeConnections));
Debug.WriteLine(string.Format(” Current Connections: {0}”, tcpstats.CurrentConnections));
Debug.WriteLine(string.Format(” Errors Received: {0}”, tcpstats.ErrorsReceived));
Debug.WriteLine(string.Format(” Failed Connection Attempts: {0}”, tcpstats.FailedConnectionAttempts));
Debug.WriteLine(string.Format(” Maximum Connections: {0}”, tcpstats.MaximumConnections));
Debug.WriteLine(string.Format(” Max Transmission Timeout: {0}”, tcpstats.MaximumTransmissionTimeout));
Debug.WriteLine(string.Format(” Min Transmission Timeout: {0}”, tcpstats.MinimumTransmissionTimeout));
Debug.WriteLine(string.Format(” Reset Connections: {0}”, tcpstats.ResetConnections));
Debug.WriteLine(string.Format(” Resets Sent: {0}”, tcpstats.ResetsSent));
Debug.WriteLine(string.Format(” Segments Received: {0}”, tcpstats.SegmentsReceived));
Debug.WriteLine(string.Format(” Segments Resent: {0}”, tcpstats.SegmentsResent));
Debug.WriteLine(string.Format(” Connections Sent: {0}”, tcpstats.SegmentsSent));
#endregion

#region — TCPIPv6 stats —
Debug.WriteLine(“rnTCP/IP v6 Statisticsrn====================”);
try
{
    tcpstats = globalProps.GetTcpIPv6Statistics();
    Debug.WriteLine(string.Format(” Connections Accepted: {0}”, tcpstats.ConnectionsAccepted));
    Debug.WriteLine(string.Format(” Connections Initiated: {0}”, tcpstats.ConnectionsInitiated));
    Debug.WriteLine(string.Format(” Cumulative Connections: {0}”, tcpstats.CumulativeConnections));
    Debug.WriteLine(string.Format(” Current Connections: {0}”, tcpstats.CurrentConnections));
    Debug.WriteLine(string.Format(” Errors Received: {0}”, tcpstats.ErrorsReceived));
    Debug.WriteLine(string.Format(” Failed Connection Attempts: {0}”, tcpstats.FailedConnectionAttempts));
    Debug.WriteLine(string.Format(” Maximum Connections: {0}”, tcpstats.MaximumConnections));
    Debug.WriteLine(string.Format(” Max Transmission Timeout: {0}”, tcpstats.MaximumTransmissionTimeout));
    Debug.WriteLine(string.Format(” Min Transmission Timeout: {0}”, tcpstats.MinimumTransmissionTimeout));
    Debug.WriteLine(string.Format(” Reset Connections: {0}”, tcpstats.ResetConnections));
    Debug.WriteLine(string.Format(” Resets Sent: {0}”, tcpstats.ResetsSent));
    Debug.WriteLine(string.Format(” Segments Received: {0}”, tcpstats.SegmentsReceived));
    Debug.WriteLine(string.Format(” Segments Resent: {0}”, tcpstats.SegmentsResent));
    Debug.WriteLine(string.Format(” Connections Sent: {0}”, tcpstats.SegmentsSent));
}
catch (PlatformNotSupportedException)
{
    Debug.WriteLine(” IPv6 Not supported”);
}
#endregion

#region — UDPv4 stats —
UdpStatistics udpstats = globalProps.GetUdpIPv4Statistics();
Debug.WriteLine(“rnUDP v4 Statisticsrn====================”);
Debug.WriteLine(string.Format(” UDP Listeners: {0}”, udpstats.UdpListeners));
Debug.WriteLine(string.Format(” Datagrams Received: {0}”, udpstats.DatagramsReceived));
Debug.WriteLine(string.Format(” Datagrams Sent: {0}”, udpstats.DatagramsSent));
Debug.WriteLine(string.Format(” Datagrams Discarded: {0}”, udpstats.IncomingDatagramsDiscarded));
Debug.WriteLine(string.Format(” Datagrams with Errors: {0}”, udpstats.IncomingDatagramsWithErrors));
#endregion

#region — UDPv6 stats —
Debug.WriteLine(“rnUDP v6 Statisticsrn====================”);
try
{
    udpstats = globalProps.GetUdpIPv6Statistics();
    Debug.WriteLine(string.Format(” UDP Listeners: {0}”, udpstats.UdpListeners));
    Debug.WriteLine(string.Format(” Datagrams Received: {0}”, udpstats.DatagramsReceived));
    Debug.WriteLine(string.Format(” Datagrams Sent: {0}”, udpstats.DatagramsSent));
    Debug.WriteLine(string.Format(” Datagrams Discarded: {0}”, udpstats.IncomingDatagramsDiscarded));
    Debug.WriteLine(string.Format(” Datagrams with Errors: {0}”, udpstats.IncomingDatagramsWithErrors));
}
catch (PlatformNotSupportedException)
{
    Debug.WriteLine(” IPv6 Not supported”);
}
#endregion

ArpTable table = ArpTable.GetArpTable();
if (table.Count > 0)
{
    Debug.WriteLine(“rn*ARP Tablern====================”);
    Debug.WriteLine(string.Format(“{0}{1}{2}{3}”,
   
“[Interface]”.PadRight(16),
    “[Type]”.PadLeft(10),
    “[IP Address]”.PadLeft(17),
    “[MAC Address]”.PadLeft(22)));
    foreach (ArpEntry entry in table)
    {
        Debug.WriteLine(string.Format(“{0}{1}{2}{3}”
            entry.NetworkInterface.Name.PadRight(16), 
            entry.ArpEntryType.ToString().PadLeft(10), 
            entry.IPAddress.ToString().PadLeft(17), 
            entry.PhysicalAddress.ToString().PadLeft(22)));
    }
}
else
{
    Debug.WriteLine(” No ARP table entries”);
}

IPRoutingTable routingTable = IPRoutingTable.GetRoutingTable();
if(routingTable.Count > 0)
{
    Debug.WriteLine(“rn*IP Routing Tablern====================”);
    Debug.WriteLine(string.Format(“{0}{1}{2}{3}{4}”,
        “[Interface]”.PadRight(16),
        “[Type]”.PadRight(12),
        “[Destination]”.PadLeft(17),
        “[NetMask]”.PadLeft(17),
        “[Next Hop]”.PadLeft(17)));
    foreach (IPForwardEntry entry in routingTable)
    {
        string intfname = entry.NetworkInterface == null ? intfname = “{Loopback}” : entry.NetworkInterface.Name;
        Debug.WriteLine(string.Format(“{0}{1}{2}{3}{4}”,
        intfname.PadRight(16),
        entry.RouteType.ToString().PadRight(12),
        entry.Destination.ToString().PadLeft(17),
        entry.SubnetMask.ToString().PadLeft(17),
        entry.NextHop.ToString().PadLeft(17))); 
    }
}
else
{
    Debug.WriteLine(” No IP Routing table entries”);
}

TcpConnectionInformation[] connections = globalProps.GetActiveTcpConnections();
if (connections.Length > 0)
{
    Debug.WriteLine(“rnActive TCP Connectionsrn====================”);
    Debug.WriteLine(string.Format(“{0}:{1}<—->{2}{3}[{4}]”,
        “[local IP]”.PadLeft(16),
        “[port]”.PadRight(6),
        “[remote IP]”.PadLeft(16),
        “[port]”.PadRight(6),
        “[state]”));
    foreach (TcpConnectionInformation info in connections)
    {
        Debug.WriteLine(string.Format(“{0}:{1}<—->{2}:{3}[{4}]”,
        info.LocalEndPoint.Address.ToString().PadLeft(16),
        info.LocalEndPoint.Port.ToString().PadRight(6),
        info.RemoteEndPoint.Address.ToString().PadLeft(16),
        info.RemoteEndPoint.Port.ToString().PadRight(6),
        info.State.ToString()));
    }
}
else
{
    Debug.WriteLine(” No active TCP connections”);
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s