Note:
This is a series of articles for beginners in the field of network development 《. the expanded reading of the book "NET 4.0 Object-Oriented Programming mantalk". During the writing process, I assume that readers can read the relevant sections of this book without wasting any effort on repeatedly introducing relevant content.
For other types of readers, unless you have the corresponding. NET technical background and certain development experience, you may encounter difficulties in reading.
I hope this series of articles will give readers a taste of the charm of network development!
In addition, these articles are original articles. Please respect the work of the author. I allow everyone to freely repost these articles and related examples for the purpose of knowledge sharing, but without my permission, please do not use it for commercial profit purposes.
If any error occurs in this article, please reply and correct it.
Thank you!
Jin xuliang
========================================================== ==========
Click the following link to read the previous articles in this series:
The article ". NET 4.0 getting started with network development series"-No network wins
Introduction to. NET 4.0 network development-IP address knowledge
1 Guide
I believe almost everyone knows that the Windows operating system can automatically detect whether the local computer is connected to the network, and the corresponding icon is displayed in the lower right corner of the taskbar (Figure 1 ):
Figure 1
This function is useful for software developers. For example, when a user connects to the Internet, you can automatically check whether your software has been updated.
Next we will talk about how to clone Windows in. NET applications.
2 "How many sisters do you have "?
Don't get me wrong. I didn't sing Meng Tingyi's song in the school, but I just got an active atmosphere. :)
The purpose of referencing the lyrics here is to raise the following question:
How do I know how many NICs are installed on the local computer?
Why should I know the answer to this question?
It's easy because you want to know if the computer is connected to the Internet.
Many Computers currently have more than two NICs, the most typical of which is laptops. We usually have two ways to connect to the Internet: directly inserting a network cable and using a wireless router to access the Internet. In actual scenarios, there are also frequent mixed use of the two methods (the local lan network cable is plugged in, and the wireless network adapter is not disabled ). Windows is very smart. Whether or not these two methods are used at the same time, as long as one network adapter can connect to the Internet, it considers the Internet to be "accessible.
Here we will talk about the concept of "network card.
"Network Interface" is a popular term. The term is "Network Interface". The following is a description on Wikipedia:
Network interface may refer to: Network interface controller (NIC), the device a computer uses to connect to a computer network.
In network application development, the term "Network Interface" is usually used. Of course, you think of it as "Network Interface", and the problem is not big. You do not need to write Paper but write programs.
The System. NET. NetworkInformation namespace of the. Net base class library provides a set of classes to conveniently obtain various network information.
The following code Retrieves all network interfaces of the local machine:
NetworkInterface [] interfaces = NetworkInterface. GetAllNetworkInterfaces ();
Each NetworkInterface object represents a "real" network interface of the local machine.
After obtaining the NetworkInterface object, you can obtain rich network information through its attributes.
The following method displays the basic information of a network interface (see the sample program GetLocalhostInformation ):
Private static void ShowNetworkInterfaceBasicInfo (NetworkInterface nic)
{
Console. WriteLine ("Name (Name): {0}", nic. Name );
Console. WriteLine ("Description): {0}", nic. Description );
Console. WriteLine ("id: {0}", nic. Id );
Console. WriteLine ("physical address: {0}", BitConverter. ToString (
Nic. GetPhysicalAddress (). GetAddressBytes ()));
Console. WriteLine ("whether to only receive data packets (IsReceiveOnly): {0 }",
Nic. IsReceiveOnly );
Console. WriteLine ("type (NetworkInterfaceType): {0 }",
Nic. NetworkInterfaceType );
Console. WriteLine ("whether multicast data packets can be received:" + nic. SupportsMulticast );
Console. WriteLine ("current operation status:" + nic. OperationalStatus );
}
The above is the output of the above method on the author's computer:
Name: Local Connection
Description: Broadcom NetLink (TM) Gigabit Ethernet
Id: {E0DE0334-1A45-45DE-8259-7A3AD2F7B8DA}
Physical address: F0-4D-A2-DA-70-D7
Whether to only receive data packets (IsReceiveOnly): False
Type (NetworkInterfaceType): Ethernet
Whether multicast data packets can be received: True
Current operation status: Up
Pay special attention to the OperationalStatus attribute of the network interface (the current operation status). The network interface with "Down" is unavailable.
The GetIPProperties method of the NetworkInterface object can be used to obtain the IPInterfaceProperties object. It contains all IP-related information of a network interface. The most important information related to the network connection is the gateway and DNS server address:
Private static void ShowNetworkInterfaceIPProperties (NetworkInterface nic)
{
IPInterfaceProperties ipProperties = nic. GetIPProperties ();
//...... More code
GatewayIPAddressInformationCollection gatewayInfo = ipProperties. GatewayAddresses;
Foreach (GatewayIPAddressInformation info in gatewayInfo)
Console. WriteLine ("Gateway Address: {0}", info. Address );
IpAddresses = ipProperties. DnsAddresses;
Foreach (IPAddress info in ipAddresses)
Console. WriteLine ("DNS Server address: {0}", info );
}
The following is the information about the local network interface in the author's computer:
DNS suffix:
Unicast (Unicast) Address: fe80: b0f5: 7eaa: 3b79: 102e % 11
Unicast (Unicast) Address: 192.168. 1.100
Multicast address: ff01: 1% 11
......
Multicast address: 224.0. 0.1
Multicast address: 224.0. 0.252
Multicast address: 239.255. 255.250
Gateway address: 192.168. 1.1
DHCP Server address: 192.168. 1.1
DNS Server address: 192.168. 1.1
Because the author uses wireless routers and ADSL Broadband Internet access, the gateway address and DNS server address are both "192.168.1.1", which is the default address of the wireless router. In a large LAN, the gateway and DNS are usually not the same, and there are usually two DNS servers.
In addition, note that IP addresses are classified into two categories: unicast and multicast. In the following article, we will introduce some typical network applications developed based on the multicast feature.
3 Ping
The Ping Command is the most common command used to check network connectivity.