PassPrevious notesWe can know:To initiate a communication, a client must know that the server is running.ProgramWhat is the IP address and port number of the host?. Then we can send information to a specific application on the server through this address. For the two computers on the network, the Computer Operated by the user is called a local host, and the other computer that communicates with the computer is called a remote host. The identification of remote hosts is composed of two parts: one is the host ID, which is used to identify the remote host that communicates with the local host; the other is the port number, which is used to identify the process that communicates with the remote host.
Reading directory:
1. Obtain host address information
2. Obtain Nic information and network detection
2.1 obtain Nic Information
2.2 network traffic detection
2.3 network connection Detection
3. References
Sample download
1. Obtain host address information
In C # DevelopmentSystem. netThe namespace isVarious protocols used on the network provide simple programming interfaces. We can use the classes in this namespace to compile applications based on network standard protocols,You do not have to consider the details of different protocols. When obtaining host (local and remote) address information, we need to use and learn these basic classes (for more details about the API, refer to msdn, and the corresponding links will be given below ), use them to implement related functions.
- IP addresses that provide Internet protocolsIPaddressClass
- Contains IP addresses and port numbersIpendpointClass
- Information containerIphostentryClass
- Provides simple domain name resolution FunctionsDNSClass
It is better to do it over a thousand times. The best way to learn programming is to practice it by yourself. Next we will create a simple Windows Forms sample program (at the end of the blog) to learn how to obtain the address information of the network host. The following is an example.CodeMain Code:
1 // Obtain the local host name 2 String Localhostname = DNS. gethostname (); 3 4 // The container that obtains all IP address information under the host using the host name. 5 Iphostentry local = DNS. gethostentry (hostname ); 6 7 // Obtain all IP addresses of the associated host through the Addresslist attribute of the iphostentry object. 8 IPaddress [] iplist = Local. Addresslist; 9 10 // Get local loopback address 11 IPaddress loopbackip = IPaddress. loopback; 12 13 // Construct an IPaddress object using its parse function 14 IPaddress localip = IPaddress. parse ( " 192.168.1.101 " ); 15 16 // Construct an ipendpoint object through the IPaddress object and port number 17 Ipendpoint IEP = New Ipendpoint (localip, 80 );
View the effect of running the sample program:
2. Obtain Nic information and network detection
Network AdapterIt is also known as a NIC or network interface card (NIC). It is a hardware device that connects a computer to a network. The main working principle of the network adapter is to organize the data sent from the computer to the channel, and divide the data into appropriate data packets and then send them to the network. In. NET development, we use System. net. networkinformationNamespace acquisition: network traffic data, network address information, and local computer address change notifications. The namespace also contains the class that implements the Ping utility. AvailablePingAnd related classes to check whether the computer can be connected through the network.
2.1 obtain Nic Information
To obtain Network Interface Information of network adapter, network connection, network speed, and network protocol version (including IPv4 and IPv6), we use the following two classes:
- NetworkinterfaceClass: provides the ability to access all interfaces of the host. Using this class, we can easily detect how many NICs are available on the machine, which network connections are available, and obtain the model, MAC address, and speed of a nic.
- IpinterfacepropertiesClass:It can be used to access the configuration and address information of network interfaces that support IPv4 or IPv6. This class is an abstract class and cannot be directly created.Networkinterface. getipproperties ()Return instance.
The following code uses a simple Windows Forms sample program to learn how to obtain the configuration and statistics of Network Interfaces:
1//Obtains an array of all network adapter objects on the host.2Networkinterface [] adapters =Networkinterface. getallnetworkinterfaces ();3 4//Obtains the configuration object of the network adapter.5Ipinterfaceproperties adapterproperties =Adapters [I]. getipproperties ();6 7//Obtain and update the DNS server address of the network adapter.8Ipaddresscollection dnsservers = adapterproperties. dnsaddresses;
Running the instance program:
2.2 network traffic detection
We can useSystem. net. networkinformationTheIpglobalpropertiesClass to obtain the number of packets received, forwarded, discarded, and sent by the network adapter.Provides network connection information for the local computer. To detect network traffic, we useIpglobalproperties classGetipglobalproperties ()Method acquisition recordThe object instance of the network connection and Communication statistics of the Local Computer. The object instance obtains the relevant information through the instance attributes to detect network traffic:
1//Objects that contain network connection and Communication statistics of the Local Machine2Ipglobalproperties properties =Ipglobalproperties. getipglobalproperties ();3 4//Obtain local IPv4 statistics5Ipglobalstatistics ipstate = properties. getipv4globalstatistics ();
The following code is used to create a small example to learn how to detect network traffic. The program running effect is as follows:
2.3 Network Connection Detection
we know that the Ping command entered in the CMD command line can be used, you can quickly detect network faults by calling the ping.exe command line program. In. in the. NET development environment, you can use system. net. networkinformation Ping class, pingoptions class and pingreply class to implement the command line functions similar to ping.exe .
- PingClass can be determinedUse a program to determine whether the remote computer can be accessed through the network
- PingoptionsClassUsed to control how data is transmittedPingData Packets
- PingreplyClassProvideSendOrSendasyncOperation Status and generated data.
The main code of the sample program is as follows:
1 // Get host address 2 String Hostaddress = This . Txt_hostaddress.text.trim (); 3 // Construct a ping instance 4 Ping pingsender = New Ping (); 5 // Ping option settings 6 Pingoptions Options = New Pingoptions (); 7 Options. dontfragment =True ; 8 // Test Data 9 String Testdata = " Test Data " ; 10 Byte [] Buffer = Encoding. ASCII. getbytes (testdata ); 11 // Set timeout 12 Int Timeout = 120 ; 13 // Call the synchronous send method to send data and save the result to the pingreply instance. 14 Pingreply reply = pingsender. Send (hostaddress, timeout, buffer, options );
Sample program running effect:
Download example: Click Download
References:
Msdn. NET Framework 4 class library
C # network application programming 2
author: sunny pig
Source: http://www.cnblogs.com/IPrograming
the copyright of this article is shared by the author and the blog. You are welcome to repost it and indicate the source.