C # obtain the local name, IP address, domain name, and physical location through network programming

Source: Internet
Author: User

In C # network programming, host domain names and IP addresses can be converted to each other. At the same time, DNS classes, iphostentry classes, IPaddress classes, and dnspermission classes can be used to implement some simple DNS functions.

The following describes a C # windows application to implement the following functions:

(1). Obtain the host name;

(2). Obtain the IP address of the local computer;

(3) enter the remote domain name www.xxx.com to query the IP address of the website;

(4). Find its physical location based on the IP address of its remote website;

(5). Find the weather forecast for the Local Location Based on the physical location (not implemented ).

The procedure is as follows:

1. Create a C # project

Click "file"-> "new project"-> "C # windows application", select the storage location and project name, and create an IPaddress project.

2. Add controls

In solution, right-click form1.cs to view its code designer. Set the control as shown in. The specific properties of the interface design are as follows:

The text properties of the six label controls correspond to "Host Name", "ip address", and "Weather Forecast ";

6 textbox controls, except "query remote domain name (input)" is the input shape such as: www.xxx.com, the other are the output of the corresponding get results, the corresponding TextBox1-TextBox6;

One RichTextBox Control provides the advanced input and edit text function control. The result displayed in the textbox on the left displays the summary result at the same time;

The two button controls are "Start query" and "clear records" to clear the contents of RichTextBox.

3. Add a new naming Control

// Add a new namespace

Usingsystem. net;

Usingsystem. IO;

Usingsystem. xml;

In. the naming control for the DNS class on the net platform is system. net: call its function method to obtain the conversion between local names, IP addresses, and domain name IP addresses. system. io can provide operations for encoding Io streams (it seems useless), system. XML is used to obtain XML Web pages from the actual physical locations by accessing the website "youdao" based on the IP address.

4. Double-click the start scan button to add the corresponding Event code.

Privatevoidbutton#click (objectsender, eventargse)

{

// Add code to implement specific functions, as shown below

}

To add the "clear records" function, double-click the "clear records" button and add the following code:

Privatevoidbutton2_click (objectsender, eventargse)

{

Richtextbox1.clear (); // clear records

}

The Code is as follows:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; // Add a new namespace using system. net; using system. io; using system. XML; namespace IPaddress {public partial class form1: FORM {public form1 () {initializecomponent () ;}// click "Start query" to create the Click Event private void Bu Ttondomainclick (Object sender, eventargs e) {// construct two common attributes of a host object iphostentry (Addresslist address list | hostname host name) iphostentry myhost = new iphostentry (); try {// 1. obtain the host information myhost = DNS by obtaining the host name. gethostbyname (DNS. gethostname (); // textbox1 edit box: displays the host name textbox1.text = myhost. hostname. tostring (); // richtextbox1 advanced text input editing control: appendtext function append local host information richtextbox1.appendtext ("1. local host name --> "+ myhost. hostname. tostring () + "\ R"); // 2. obtain the local IP address for (INT I = 0; I <myhost. addresslist. length; I ++) {textbox2.text = myhost. addresslist [I]. tostring (); richtextbox1.appendtext ("2. local host IP Address --> "+ myhost. addresslist [I]. tostring () + "\ r");} // 3. enter the remote IP address \ domain name to query // construct a remote host object and string variable strip storage IP address iphostentry otherhost = new iphostentry (); Int J; // resolve function: convert the domain name to an IP address www.baidu.com otherhost = DNS. resolve (textbox3.text); // obtain the remote Query IP address for (J = 0; j <otherhost. addresslist. length; j ++) {textbox4.text = otherhost. addresslist [J]. tostring (); richtextbox1.appendtext ("3. remote domain name --> "+ textbox3.text +" \ r * IP Address --> "+ otherhost. addresslist [J]. tostring () + "\ r");} // 4. IP address to find the corresponding physical location // by accessing a Web site to query the physical location of the IP address (Method 2: download an IP library, find the content in the IP library) string Surl = "http://www.youdao.com/smartresult-xml/search.s? Type = IP & Q = "+ otherhost. addresslist [J-1]. tostring () + ""; // youdao URL // defines the physical location of the string variable storage string stringipaddress = ""; // obtain the content of the XML file returned by youdao: Using (xmlreader READ = xmlreader. create (Surl) {While (read. read () // read the next byte from the stream {Switch (read. nodetype) {Case xmlnodetype. text: // retrieve the text content in the XML format file to query the last IP address Addresslist [J-1] If (string. format ("{0}", read. value ). tostring (). trim ()! = Otherhost. addresslist [J-1]. tostring () {stringipaddress = string. format ("{0}", read. value ). tostring (). trim (); // assign value} break; }}// textbox5 dialog box: assign a value to the physical location and append it to textbox5.text = stringipaddress at the end of richtextbox1; richtextbox1.appendtext ("4. physical location --> "+ stringipaddress +" \ r ");} catch (exception MSG) {MessageBox. show (MSG. message); // error message} private void button2_click (Object sender, eventargs e) {richtextbox1.clear (); // clear records }}}

5. Running result

Enter www.google.com and click "Start query"

 

Shows the output result:

As you can see, my host name is delldell and the local IP address. at the same time, there are many corresponding IP addresses of Google, and the IP address of the remote domain name is displayed in RichTextBox. The physical address is the physical location of the last IP address 74.125.31.104, which is "Google Inc., Mountain View, California, USA"

(Note: I didn't do this function for weather forecast, but it works the same way by visiting a weather forecast website and displaying the corresponding weather according to the physical location. It works the same way)

6. Summary

This is an example I recently learned about C # network programming. You can do a lot by getting an IP address, for example, QQ and other websites display the weather forecast of your location, remote login judgment, hacker attacks, etc. This is a very basic system. net \ system. use of XML-related methods and functions.

Popping_dancer's blog knowledge is used in determining the physical location based on the IP address output. The above code "4. the IP address to find the corresponding physical location "is also a reference to repost his article, I hope you can also look at his article, very useful. I am very grateful to the blogger for his article. The website is as follows:

Http://blog.csdn.net/popping_dancer/article/details/8297038

The method is to access the "youdao" website to view the corresponding physical location by obtaining the IP address. The access process for Google is shown in:

The above is just my understanding of C # network programming, DNS, domain name, IP address, and physical location conversion. It is only for you to learn and understand. If there are errors or deficiencies, please forgive me! Hope to help you.

(2013-7-8 by: eastmount)

 

Related Article

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.