March 27, 2008, 4:45 a.m.
posted by bert
Check out Your Network ConnectivityProblemYou need to determine the characteristics of the network adapters currently on the machine. SolutionUse the DisplayNICInfo method shown in Figure 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. Setting the SMTP server properties
Allowing an IP address to access the SMTP server
Allowing access to local machine (IP address 127.0.0.1)
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). DisplayNICInfo methodThe DisplayInterfaceProperties method shown in Figure 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. DisplayInterfaceProperties methodThe .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:
DiscussionKnowing 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 AlsoSee the "System.Net.NetworkInformation Namespace," "Network Interface Class," and "IPInterfaceProperties Class" topics in the MSDN documentation. |
- Comment