. Net 4.0 getting started with network development-I am in the center of "net"

Source: Internet
Author: User
. Net 4.0 getting started with network development -- I am in the "network" center (on)

Note:

This isBeginners in the Network Development FieldSeriesArticle, Can be used 《. Net 4.0Object-Oriented Programming The expanded reading of this book. During the writing process, I assume that the reader can read the relevant chapters of this book without wasting any effort to introduce the 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:

". Net 4.0 A series of articles on getting started with network development" Opening Speech -- Invincible

《. Net 4.0 getting started with 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.
"Nic" is a popular term. The real term is"Network Interface)", The following is a text description found 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 a "Network Interface", and the problem is not big. You do not need to write paper.Program.
The system. net. networkinformation namespace of the. net base class library provides a set of classes to conveniently obtain various network information.
The following sentenceCodeObtain all network interfaces of the local machine:

Networkinterface [] networking = 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 ):

Obtain Network Interface Information Private   Static   Void Shownetworkinterfacebasicinfo (networkinterface Nic)
{
Console. writeline ( " 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:

Displays the IP address of a network interface. 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 commonly used command to check network connectivity. the. NET base class library provides a ping component to complete this operation. The Ping component is easy to use. First prepare a data block to be tested (not too big, 32 bytes is enough), and then call the send method to send the data block to the remote host:

Public Pingreply send ( String Hostnameoraddress, // Host to be pinged
Int Timeout, // Wait for the host to send back a response for a few seconds at most
Byte [] Buffer, // Data to be sent
Pingoptions options // Some configurable additional parameters. For more information, see msdn.
);

The send method returns a pingreply object and checks whether its status attribute is ipstatus. succes to check whether the ping operation is successful.
I have compiled a usepingcomponent sample program to ping the remote host (Figure 2 ):

 

Figure 2

The Ping component supports the EAP asynchronous programming mode. Microsoft provides a pingclient sample program to demonstrate its usage. In 《. chapter 4.0 of net 10th Object-Oriented Programming gives a detailed introduction to EAP. Readers only need to carefully read Section 10.6 "Event-based Asynchronous call mode". it is easy to understand the pingclient sample program.

======================================

Please click to read the second part of this article:

. Net 4.0 getting started with network development-I am in the center of "net" (below)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.