(RPM) implements a simple Ping function in C # to test whether the network is connected

Source: Internet
Author: User
Tags get ip

This article was reprinted from: http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx

Implement a simple Ping function in C # to test whether the network is connected

1. Get host name based on IP address

 /// <summary>        ///get host name based on IP address/// </summary>        /// <param name= "IP" >IP address of the host</param>        /// <returns>Host name</returns>         Public stringGethostnamebyip (stringIP) {IP=IP.            Trim (); if(IP = =string. Empty)return string.            Empty; Try            {                //whether the Ping is a pass                if( This. Ping (IP)) {System.Net.IPHostEntry host=System.Net.Dns.GetHostEntry (IP); returnhost.                HostName; }                Else                    return string.            Empty; }            Catch(Exception) {return string.            Empty; }        }

Note: If your computer can access the Internet you can even query to: the IP Address "64.233.189.104" is a Google named "hk-in-f104.google.com" the IP address of the host.

About this in the code. After the Ping (IP) method.

Now that you've said how to "get the hostname based on the IP address," say how to "get the host's IP address based on the hostname".

2. Obtain the host's IP address according to the host name

 /// <summary>        ///Obtain the host's IP address based on the hostname (domain name)/// </summary>        /// <param name= "HostName" >host name or domain name</param>        /// <example>Getipbydomain ("www.google.com");</example>        /// <returns>IP address of the host</returns>         Public stringGetipbyhostname (stringhostName) {HostName=Hostname.trim (); if(HostName = =string. Empty)return string.            Empty; Try{System.Net.IPHostEntry host=System.Net.Dns.GetHostEntry (hostName); returnHost. Addresslist.getvalue (0).            ToString (); }            Catch(Exception) {return string.            Empty; }        }

Note: If your computer can surf the Internet you can even query to: "www.google.com" IP address is "64.233.189.104".

Finally, say that C # implements a simple Ping function to test whether the network is connected.

3. C # implements a simple Ping function to test whether the network is connected

/// <summary>        ///whether the specified host can be Ping/// </summary>        /// <param name= "IP" >IP address or host name or domain name</param>        /// <returns>true Pass, false not pass</returns>         Public BOOLPing (stringIP) {System.Net.NetworkInformation.Ping P=NewSystem.Net.NetworkInformation.Ping (); System.Net.NetworkInformation.PingOptions Options=NewSystem.Net.NetworkInformation.PingOptions (); Options. Dontfragment=true; stringdata ="Test data!"; byte[] buffer =Encoding.ASCII.GetBytes (data); intTimeout = +;//timeout time, unit: millisecondsSystem.Net.NetworkInformation.PingReply reply =p.send (IP, timeout, buffer, options); if(reply. Status = =System.Net.NetworkInformation.IPStatus.Success)return true; Else                return false; }
Private voidButton1_Click (Objectsender, EventArgs e)//Get IP{textbox2.text=Getipbyhostname (TextBox1.Text); }  Private voidButton2_Click (Objectsender, EventArgs e)//Get host name{textbox1.text=Gethostnamebyip (TextBox2.Text); }   Private voidButton3_Click (Objectsender, EventArgs e)//get the native IP        {            stringHostName =System.Net.Dns.GetHostName (); Iphostentry Ipentry=System.Net.Dns.GetHostEntry (hostName); //Dns.gethostbyname (myName); obsoleteipaddress[] Addr =ipentry.addresslist; TextBox2.Text= addr[0].        ToString (); }        /// <summary>        ///LAN Search Events/// </summary>        Private voidLansearch () {stringlocalhost =(Dns.gethostentry (Dns.gethostname ())). addresslist[0]. ToString ();//Local Host IP address            stringstr = localhost. Substring (0, localhost. LastIndexOf ("."));  for(inti =0; I <255; i++)//set up 255 threads to scan an IP            {                stringIP = str +"."+i.tostring ();  This. Text =IP; if(Ping (IP)) {LISTBOX1.ITEMS.ADD (IP);                 Application.doevents (); } progressbarsearch.value= Progressbarsearch.value +1; }        }        Private voidButton4_Click (Objectsender, EventArgs e)//Online Hosting{lansearch (); }

(RPM) implements a simple Ping function in C # to test whether the network is connected

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.