Recipe16.8.Check out Your Network Connectivity


Recipe 16.8. Check out Your Network Connectivity

Problem

You need to determine the characteristics of the network adapters currently on the machine.

Solution

Use the DisplayNICInfo method shown in Example 16-9 to display all of the characteristics of the currently existing network adapters using the System.Net.NetworkInformation. NetworkInterface class. Calling the GetAllNetworkInterfaces method will get the list of current adapters as NetworkInterface instances. Information such as the adapter ID, MAC address, status, and NIC type is available on the NetworkInterface class.

Figure 16-3. Setting the SMTP server properties


Figure 16-4. Allowing an IP address to access the SMTP server


Figure 16-5. Allowing access to local machine (IP address 127.0.0.1)


Figure 16-6. Local machine granted access to SMTP server


To see all of the IP information for an adapter, call the GetIPProperties method on the NetworkAdapter instance and pass the IPInterfaceProperties collection to the DisplayInterfaceProperties method (implemented shortly).

Example 16-9. DisplayNICInfo method

 private static void DisplayNICInfo() {      //Display current network adapter states     NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();     Console.WriteLine("Network Adapter Information:");     foreach (NetworkInterface n in adapters)     {         Console.WriteLine("\tId: {0}", n.Id);         Console.WriteLine("\tPhysical (MAC) Address: {0}",             n.GetPhysicalAddress().ToString());         Console.WriteLine("\tDescription: {0}", n.Description);         Console.WriteLine("\tName: {0}", n.Name);         Console.WriteLine("\tOperationalStatus: {0}",                 n.OperationalStatus.ToString());         Console.WriteLine("\tInterface type: {0}",                 n.NetworkInterfaceType.ToString());         Console.WriteLine("\tSpeed: {0}", n.Speed);         IPInterfaceProperties ipProps = n.GetIPProperties();         DisplayInterfaceProperties(ipProps);     }     Console.WriteLine(""); } 

The DisplayInterfaceProperties method shown in Example 16-10 breaks down and displays all of the IP configuration information, such as DHCP and WINS addresses, gateway and DNS addresses, assigned IP addresses for the adapter, as well as multicast and unicast information.

Example 16-10. DisplayInterfaceProperties method

 private static void DisplayInterfaceProperties(IPInterfaceProperties props) {     Console.WriteLine("\t\tDns Suffix : {0}", props.DnsSuffix);     Console.WriteLine("\t\tAnycast Addresses:");     foreach (IPAddressInformation ipInfo in props.AnycastAddresses)     {         Console.WriteLine("\t\t\t{0}", ipInfo.Address.ToString());         Console.WriteLine("\t\t\tIsDnsEligible: {0}", ipInfo.IsDnsEligible);         Console.WriteLine("\t\t\tIsTransient: {0}", ipInfo.IsTransient);     }     Console.WriteLine("\t\tDHCP Server Addresses:");     foreach (IPAddress ipAddr in props.DhcpServerAddresses)     {         Console.WriteLine("\t\t\t{0}", ipAddr.ToString());     }     Console.WriteLine("\t\tDNS Addresses:");     foreach (IPAddress ipAddr in props.DnsAddresses)     {         Console.WriteLine("\t\t\t{0}", ipAddr.ToString());     }     Console.WriteLine("\t\tGateway Addresses:");     foreach (GatewayIPAddressInformation gatewayIPInfo in props.GatewayAddresses)     {         Console.WriteLine("\t\t\t{0}", gatewayIPInfo.Address.ToString());     }     Console.WriteLine("\t\tUnicast Addresses:");     foreach (UnicastIPAddressInformation uniIPInfo in props.UnicastAddresses)     {         Console.WriteLine("\t\t\tAddress: {0}",                 uniIPInfo.Address.ToString());         Console.WriteLine("\t\t\tPreferred Lifetime: {0}",                 uniIPInfo.AddressPreferredLifetime);         Console.WriteLine("\t\t\tValid Lifetime: {0}",                 uniIPInfo.AddressValidLifetime);         Console.WriteLine("\t\t\tDHCP Lease Lifetime: {0}",                 uniIPInfo.DhcpLeaseLifetime);         Console.WriteLine("\t\t\tPrefix Origin: {0}",                 uniIPInfo.PrefixOrigin.ToString());         Console.WriteLine("\t\t\tSuffix Origin: {0}",                 uniIPInfo.SuffixOrigin.ToString());     }     Console.WriteLine("\t\tMulticast Addresses:");     foreach (MulticastIPAddressInformation multiIPInfo in props.MulticastAddresses)     {         Console.WriteLine("\t\t\tAddress: {0}", multiIPInfo.Address.ToString());         Console.WriteLine("\t\t\tPreferred Lifetime: {0}",                      multiIPInfo.AddressPreferredLifetime);         Console.WriteLine("\t\t\tValid Lifetime: {0}",                     multiIPInfo.AddressValidLifetime);         Console.WriteLine("\t\t\tDHCP Lease Lifetime: {0}",                     multiIPInfo.DhcpLeaseLifetime);         Console.WriteLine("\t\t\tPrefix Origin: {0}",                     multiIPInfo.PrefixOrigin.ToString());         Console.WriteLine("\t\t\tSuffix Origin: {0}",                     multiIPInfo.SuffixOrigin.ToString());     }     Console.WriteLine("\t\tWINS Server Addresses:");     foreach (IPAddress ipAddr in props.WinsServersAddresses)     {         Console.WriteLine("\t\t\t{0}", ipAddr.ToString());     }     Console.WriteLine(""); } 

The .NET runtime also provides event notifications when the network address changes for an adapter or the network availability state changes, through the NetworkChange. NetworkAddressChanged and NetworkChange.NetworkAvailabilityChanged events. In the TestNetInfo method, you hook up for these events, then handle them in the NetworkChange_NetworkAddressChanged and NetworkChange_NetworkAvailabilityChanged methods. When the availability event fires, the NetworkAvailabilityEventArgs object can be accessed to see if the network is available through the IsAvailable property. The network address event does not supply information about what address changed, so you simply call DisplayNICInfo again.

 public static void TestNetInfo() {     // Hook up for network events     NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);     NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);     DisplayNICInfo(); } static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) {     // A network address changed; redisplay the info     Console.WriteLine("*** NEW NETWORK INFORMATION IS AVAILABLE *** ");     DisplayNICInfo(); } static void NetworkChange_NetworkAvailabilityChanged(object sender,     NetworkAvailabilityEventArgs e) {     if(e.IsAvailable)         Console.WriteLine("Network is now available");     else         Console.WriteLine("Network is no longer available"); } 

The output is shown here:

 Network Adapter Information:     Id: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}     Physical (MAC) Address: XXXXXXXXXXXX     Description: Broadcom NetXtreme 57xx Gigabit Controller - Packet Scheduler Miniport     Name: Local Area Connection     OperationalStatus: Up     Interface type: Ethernet     Speed: 100000000         Dns Suffix :         Anycast Addresses:         DHCP Server Addresses:             255.0.0.1         DNS Addresses:             255.0.0.1         Gateway Addresses:             255.0.0.1         Unicast Addresses:             Address: 255.0.0.101             Preferred Lifetime: 52434             Valid Lifetime: 52434             DHCP Lease Lifetime: 52434             Prefix Origin: Dhcp             Suffix Origin: OriginDhcp         Multicast Addresses:             Address: 224.0.0.1             Preferred Lifetime: 0             Valid Lifetime: 0              DHCP Lease Lifetime: 24             Prefix Origin: 48             Suffix Origin: WellKnown             Address: 255.255.255.250             Preferred Lifetime: 0              Valid Lifetime: 0              DHCP Lease Lifetime: 24             Prefix Origin: Other              Suffix Origin: WellKnown         WINS Server Addresses:             0.0.0.0             0.0.0.0     Id: MS TCP Loopback interface     Physical (MAC) Address:     Description: MS TCP Loopback interface      Name: MS TCP Loopback interface     OperationalStatus: Up      Interface type: Loopback     Speed: 10000000         Dns Suffix :         Anycast Addresses:         DHCP Server Addresses:         DNS Addresses:         Gateway Addresses:         Unicast Addresses:             Address: 127.0.0.1             Preferred Lifetime: 3170812643             Valid Lifetime: 3170812643             DHCP Lease Lifetime: 3170812643             Prefix Origin: Manual             Suffix Origin: Manual         Multicast Addresses:             Address: 224.0.0.1             Preferred Lifetime: 0             Valid Lifetime: 0              DHCP Lease Lifetime: 7733284             Prefix Origin: 110             Suffix Origin: WellKnown         WINS Server Addresses: 

Discussion

Knowing the configuration of the network you are running on can come in handy when attempting to troubleshoot connectivity issues. Being able to get an event notification when connectivity is lost or an adapter changes its IP address is a great benefit, as it allows you to write code that can recover gracefully and instruct the user what has happened. However you want to use it, there is a lot of valuable information provided in the NetworkInformation namespace that can make life a bit better for developers working in a connected environment.

See Also

See the "System.Net.NetworkInformation Namespace," "Network Interface Class," and "IPInterfaceProperties Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net