<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; network</title>
	<atom:link href="http://www.dijksterhuis.org/tag/network/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Querying System.Net.NetworkInformation in C#</title>
		<link>http://www.dijksterhuis.org/querying-systemnetnetworkinformation-in-c/</link>
		<comments>http://www.dijksterhuis.org/querying-systemnetnetworkinformation-in-c/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 07:24:09 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[network]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=329</guid>
		<description><![CDATA[.NET 2.0 added the NetworkInformation namespace which allow you to discover and query all the network interfaces in the local machine. The information this provides is very similar to what you can find by opening the &#8220;Network Connections&#8221; in Windows. In this post I have a look into the kinds information it provides &#8212; and [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>.NET 2.0 added the NetworkInformation namespace which allow you to discover and query all the network interfaces in the local machine. The information this provides is very similar to what you can find by opening the &#8220;Network Connections&#8221; in Windows. In this post I have a look into the kinds information it provides &#8212; and what to look out for if your code has to run on Mono as well.</em></p>
<p><span id="more-329"></span></p>
<p>In <a href="http://www.dijksterhuis.org/finding-the-local-ip-addresses-in-c/">a previous post</a> we looked at how we could use <em>System.Net.NetworkInformation</em> to determine the machines local IP addresses, but there is much more stored in here. </p>
<p>The code below steps through each network interface on my machine: </p>
<blockquote><p>Interface information for mymachine.mydomain.com<br />
NetBIOS node type: Unknown<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Name: Local Area Connection<br />
Description: ASUSTeK/Broadcom 440x 10/100 Integrated Controller &#8211; Packet Scheduler Miniport<br />
Network Interface Type: Ethernet<br />
Physical Address: 000EA99F2D04<br />
Adapter ID: {34F1A99A-3449-4290-8FFF-C1C62211696A}<br />
Receive only: False<br />
Status: Up<br />
Speed: 100000000<br />
Properties:<br />
|DNS Addresses: 255.255.255.255<br />
|DNS Suffic:<br />
|DNS Enabled: False<br />
|Dynamic DNS Enabled: True<br />
|DHPC Server Addresses: 255.255.255.255<br />
|UniCast Addresses: 10.0.0.6 192.168.1.6<br />
|AnyCast Addresses:<br />
|Supports multi-cast: True<br />
|Multicast Addresses: 224.0.0.1 239.255.255.250<br />
|Gateway Addresses: 10.0.0.253<br />
+IPV4 Properties:<br />
 |Interface Index: 2<br />
 |Automatic Private Addressing Active: False<br />
 |Automatic Private Addressing Enabled: True<br />
 |DHCP Enabled: False<br />
 |Forwarding Enabled: False<br />
 |MTU Size: 1500<br />
 \Uses Wins: False<br />
+IPV6 is not implemented
</p></blockquote>
<p>For brevity I have omitted the local loopback adaptor.</p>
<p>The Mono implementation of <em>System.Net.NetworkInformation</em> is spotty at best. This has much to do with the fact that on Linux much information isn&#8217;t easily stored in one central location. As can be seen from the comments in the Mono 2.01 source code:</p>
<pre class="brush: c#">
               [MonoTODO (&quot;Always returns an empty collection.&quot;)]
                public override IPAddressCollection DhcpServerAddresses {
                        get {
                                // There are lots of different DHCP clients
                                // that all store their configuration differently.
                                // I&#039;m not sure what to do here.
                                return new IPAddressCollection ();
                        }
                }

                [MonoTODO (&quot;Always returns an empty collection.&quot;)]
                public override IPAddressCollection DnsAddresses {
                        get {
                                // XXX: Parse /etc/resolv.conf, I suppose.
                                return new IPAddressCollection ();
                        }
                }
</pre>
<p>The following code runs under both .NET &#038; Mono and gives you a quick overview of the kinds of adapter information provided by <em>System.Net.NetworkInformation</em>. It is also possible to query each adapter for usage statistics &#8212; if you would like to know how many bytes it has send, but we won&#8217;t go there.</p>
<pre class="brush: c#">
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Text;

namespace Examples.System.Net.NetworkInformation.IfConfig
{
    public class IfConfig
    {
        public static bool DetectMono()
        {
            int p = (int)Environment.OSVersion.Platform;
            return (p == 4) || (p == 128);
        }

        public static void Main(string[] args)
        {
            // Mono&#039;s implementation of NetworkInformation is spotty, many values just return hard coded defaults
            // or throw an NotImplementedExceptions
            bool IsMono = DetectMono();

            // Global IP settings
            IPGlobalProperties global_properties = IPGlobalProperties.GetIPGlobalProperties();
            Console.WriteLine(&quot;Interface information for {0}.{1}&quot;, global_properties.HostName, global_properties.DomainName);
            Console.WriteLine(&quot;NetBIOS node type: {0}&quot;, global_properties.NodeType);

            //Gateway IP
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                Console.WriteLine(&quot;------------------------------------------------------&quot;);
                Console.WriteLine(&quot;Name: {0}&quot;, adapter.Name);
                Console.WriteLine(&quot;Description: {0}&quot;, adapter.Description);
                Console.WriteLine(&quot;Network Interface Type: {0}&quot;, adapter.NetworkInterfaceType);
                Console.WriteLine(&quot;Physical Address: {0}&quot;, adapter.GetPhysicalAddress());
                Console.WriteLine(&quot;Adapter ID: {0}&quot;, adapter.Id);
                Console.WriteLine(&quot;Receive only: {0}&quot;, adapter.IsReceiveOnly);
                Console.WriteLine(&quot;Status: {0}&quot;, adapter.OperationalStatus);

                if (!IsMono) Console.WriteLine(&quot;Speed: {0}&quot;, adapter.Speed); // 100 Mb is really a lot of zeros

                IPInterfaceProperties properties = adapter.GetIPProperties();
                Console.WriteLine(&quot;Properties: &quot;);

                Console.Write(&quot;|DNS Addresses: &quot;);
                foreach (IPAddress DnsServer in properties.DhcpServerAddresses)
                    Console.Write(&quot;{0} &quot;, DnsServer);
                Console.WriteLine();

                Console.WriteLine(&quot;|DNS Suffic: {0}&quot;, properties.DnsSuffix);

                Console.WriteLine(&quot;|DNS Enabled: {0}&quot;, properties.IsDnsEnabled);
                Console.WriteLine(&quot;|Dynamic DNS Enabled: {0}&quot;, properties.IsDynamicDnsEnabled);

                Console.Write(&quot;|DHPC Server Addresses: &quot;);
                foreach (IPAddress DhcpServer in properties.DhcpServerAddresses)
                    Console.Write(&quot;{0} &quot;, DhcpServer);
                Console.WriteLine();

                Console.Write(&quot;|UniCast Addresses: &quot;);
                foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
                    Console.Write(&quot;{0} &quot;, uniCast.Address);
                Console.WriteLine();

                Console.Write(&quot;|AnyCast Addresses: &quot;);
                foreach (IPAddressInformation anyCast in properties.AnycastAddresses)
                    Console.Write(&quot;{0} &quot;, anyCast.Address);
                Console.WriteLine();

                Console.WriteLine(&quot;|Supports multi-cast: {0}&quot;, adapter.SupportsMulticast);
                Console.Write(&quot;|Multicast Addresses: &quot;);
                foreach (IPAddressInformation multiCast in properties.MulticastAddresses)
                    Console.Write(&quot;{0} &quot;, multiCast.Address);
                Console.WriteLine();

                Console.Write(&quot;|Gateway Addresses: &quot;);
                foreach (GatewayIPAddressInformation GateWay in properties.GatewayAddresses)
                    Console.Write(&quot;{0} &quot;, GateWay.Address);
                Console.WriteLine();

                if (adapter.Supports(NetworkInterfaceComponent.IPv4) == true)
                {
                    IPv4InterfaceProperties IPV4Props = properties.GetIPv4Properties();
                    // Loopback device does not have any property information associated
                    Console.WriteLine(&quot;+IPV4 Properties: &quot;);
                    if (IPV4Props != null)
                    {
                        Console.WriteLine(&quot; |Interface Index: {0}&quot;, IPV4Props.Index);
                        Console.WriteLine(&quot; |Automatic Private Addressing Active: {0}&quot;, IPV4Props.IsAutomaticPrivateAddressingActive);
                        Console.WriteLine(&quot; |Automatic Private Addressing Enabled: {0}&quot;, IPV4Props.IsAutomaticPrivateAddressingEnabled);
                        Console.WriteLine(&quot; |DHCP Enabled: {0}&quot;, IPV4Props.IsDhcpEnabled);
                        Console.WriteLine(&quot; |Forwarding Enabled: {0}&quot;, IPV4Props.IsForwardingEnabled);
                        Console.WriteLine(&quot; |MTU Size: {0}&quot;, IPV4Props.Mtu);
                        Console.WriteLine(&quot; \\Uses Wins: {0}&quot;, IPV4Props.UsesWins);
                    }
                    else
                    {
                        Console.WriteLine(&quot; |Device has no IPv4 properties&quot;);
                    }
                }
                else
                {
                    Console.WriteLine(&quot;+IPV4 is not implemented&quot;);
                }

                if (!IsMono &amp;&amp; adapter.Supports(NetworkInterfaceComponent.IPv6) == true)
                {
                    IPv6InterfaceProperties IPV6Props = properties.GetIPv6Properties();
                    Console.WriteLine(&quot;+IPV6 Properties: &quot;);
                    if (IPV6Props != null)
                    {
                        Console.WriteLine(&quot;+IPV6 Properties: &quot;);
                        Console.WriteLine(&quot; |Interface Index: {0}&quot;, IPV6Props.Index);
                        Console.WriteLine(&quot; \\MTU Size: {0}&quot;, IPV6Props.Mtu);
                    }
                    else
                    {
                        Console.WriteLine(&quot; |Device has no IPv6 properties&quot;);
                    }
                }
                else
                {
                    Console.WriteLine(&quot;+IPV6 is not implemented&quot;);
                }

            }

        }
    }
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/querying-systemnetnetworkinformation-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
