Tải bản đầy đủ - 0 (trang)
10-1. Obtain Information About the Local Network Interface

10-1. Obtain Information About the Local Network Interface

Tải bản đầy đủ - 0trang

CHAPTER 10 ■ NETWORKING



■ Tip The System.Net.NetworkInformation.IPGlobalProperties class also provides access to useful

information about the network configuration of the local computer.



Table 10-1. Members of the NetworkInterface Class



Member



Description



Properties

Description



Gets a string that provides a general description of the interface.



Id



Gets a string that contains the identifier of the interface.



IsReceiveOnly



Gets a bool indicating whether the interface can only receive or can both send

and receive data.



Name



Gets a string containing the name of the interface.



NetworkInterfaceType



Gets a value from the System.Net.NetworkInformation.NetworkInterfaceType

enumeration that identifies the type of interface. Common values include

Ethernet, FastEthernetT, and Loopback.



OperationalStatus



Gets a value from the System.Net.NetworkInformation.OperationalStatus

enumeration that identifies the status of the interface. Common values include

Down and Up.



Speed



Gets a long that identifies the speed (in bits per second) of the interface as

reported by the adapter, not based on dynamic calculation.



SupportsMulticast



Gets a bool indicating whether the interface is enabled to receive multicast

packets.



Methods

GetIPProperties



Returns a System.Net.NetworkInformation.IPInterfaceProperties object that

provides access to the TCP/IP configuration information for the interface.

Properties of the IPInterfaceProperties object provide access to WINS, DNS,

gateway, and IP address configuration.



481



www.it-ebooks.info



CHAPTER 10 ■ NETWORKING



Member



Description



GetIPv4Statistics



Returns a System.Net.NetworkInformation.IPv4InterfaceStatistics object

that provides access to the TCP/IP v4 statistics for the interface. The properties

of the IPv4InterfaceStatistics object provide access to information about

bytes sent and received, packets sent and received, discarded packets, and

packets with errors.



GetPhysicalAddress



Returns a System.Net.NetworkInformation.PhysicalAddress object that

provides access to the physical address of the interface. You can obtain the

physical address as a byte array using the method

PhysicalAddress.GetAddressBytes or as a string using

PhysicalAddress.ToString.



Supports



Returns a bool indicating whether the interface supports a specified protocol.

You specify the protocol using a value from the

System.Net.NetworkInformation.NetworkInterfaceComponent enumeration.

Possible values include IPv4 and IPv6.



The NetworkInterface class also provides two other static members that you will find useful:





The static property LoopbackInterfaceIndex returns an int identifying the index

of the loopback interface within the NetworkInterface array returned by

GetAllNetworkInterfaces.







The static method GetIsNetworkAvailable returns a bool indicating whether any

network connection is available—that is, has an OperationalStatus value of Up.



The Code

The following example uses the members of the NetworkInterface class to display information about all

the network interfaces on the local machine:

using System;

using System.Net.NetworkInformation;

namespace Apress.VisualCSharpRecipes.Chapter10

{

class Recipe10_01

{

static void Main()

{

// Only proceed if there is a network available.

if (NetworkInterface.GetIsNetworkAvailable())

{

// Get the set of all NetworkInterface objects for the local

// machine.

NetworkInterface[] interfaces =

NetworkInterface.GetAllNetworkInterfaces();



482



www.it-ebooks.info



CHAPTER 10 ■ NETWORKING



// Iterate through the interfaces and display information.

foreach (NetworkInterface ni in interfaces)

{

// Report basic interface information.

Console.WriteLine("Interface Name: {0}", ni.Name);

Console.WriteLine("

Description: {0}", ni.Description);

Console.WriteLine("

ID: {0}", ni.Id);

Console.WriteLine("

Type: {0}", ni.NetworkInterfaceType);

Console.WriteLine("

Speed: {0}", ni.Speed);

Console.WriteLine("

Status: {0}", ni.OperationalStatus);

// Report physical address.

Console.WriteLine("

Physical Address: {0}",

ni.GetPhysicalAddress().ToString());

// Report network statistics for the interface.

Console.WriteLine("

Bytes Sent: {0}",

ni.GetIPv4Statistics().BytesSent);

Console.WriteLine("

Bytes Received: {0}",

ni.GetIPv4Statistics().BytesReceived);

// Report IP configuration.

Console.WriteLine("

IP Addresses:");

foreach (UnicastIPAddressInformation addr

in ni.GetIPProperties().UnicastAddresses)

{

Console.WriteLine("

- {0} (lease expires {1})",

addr.Address,

DateTime.Now +

new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));

}

Console.WriteLine(Environment.NewLine);

}

}

else

{

Console.WriteLine("No network available.");

}

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter");

Console.ReadLine();

}

}

}



483



www.it-ebooks.info



CHAPTER 10 ■ NETWORKING



10-2. Detect Changes in Network Connectivity

Problem

You need a mechanism to check whether changes to the network occur during the life of your

application.



Solution

Add handlers to the static NetworkAddressChanged and NetworkAvailabilityChanged events implemented

by the System.Net.NetworkInformation.NetworkChange class.



How It Works

The NetworkChange class provides an easy-to-use mechanism that allows applications to be aware of

changes to network addresses and general network availability. This allows your applications to adapt

dynamically to the availability and configuration of the network.

The NetworkAvailabilityChanged event fires when a change occurs to general network availability.

An instance of the NetworkAvailabilityChangedEventHandler delegate is needed to handle this event and

is passed a NetworkAvailabilityEventArgs object when the event fires. The

NetworkAvailabilityEventArgs.IsAvailable property returns a bool indicating whether the network is

available or unavailable following the change.

The NetworkAddressChanged event fires when the IP address of a network interface changes.

An instance of the NetworkAddressChangedEventHandler delegate is required to handle these events.

No event-specific arguments are passed to the event handler, which must call NetworkInterface.

GetAllNetworkInterfaces (discussed in recipe 10-1) to determine what has changed and to take

appropriate action.



■ Note The NetworkAddressChanged and NetworkAvailabilityChanged events work on Windows 2000 and

later operating systems.



The Code

The following example demonstrates how to use handlers that catch NetworkAddressChanged and

NetworkAvailabilityChanged events and then display status information to the console. To observe how

the code handles changing network conditions, unplug your network cable while the example is

running, wait a few seconds, and then plug the cable back in.



484



www.it-ebooks.info



CHAPTER 10 ■ NETWORKING



using System;

using System.Net.NetworkInformation;

namespace Apress.VisualCSharpRecipes.Chapter10

{

class Recipe10_02

{

// Declare a method to handle NetworkAvailabilityChanged events.

private static void NetworkAvailabilityChanged(

object sender, NetworkAvailabilityEventArgs e)

{

// Report whether the network is now available or unavailable.

if (e.IsAvailable)

{

Console.WriteLine("Network Available");

}

else

{

Console.WriteLine("Network Unavailable");

}

}

// Declare a method to handle NetworkAdressChanged events.

private static void NetworkAddressChanged(object sender, EventArgs e)

{

Console.WriteLine("Current IP Addresses:");

// Iterate through the interfaces and display information.

foreach (NetworkInterface ni in

NetworkInterface.GetAllNetworkInterfaces())

{

foreach (UnicastIPAddressInformation addr

in ni.GetIPProperties().UnicastAddresses)

{

Console.WriteLine("

- {0} (lease expires {1})",

addr.Address, DateTime.Now +

new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));

}

}

}

static void Main(string[] args)

{

// Add the handlers to the NetworkChange events.

NetworkChange.NetworkAvailabilityChanged +=

NetworkAvailabilityChanged;

NetworkChange.NetworkAddressChanged +=

NetworkAddressChanged;



485



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

10-1. Obtain Information About the Local Network Interface

Tải bản đầy đủ ngay(0 tr)

×