Use C # To implement the simple Ping function, which is used to test whether the network is connected

Source: Internet
Author: User

Use C # To implement the simple Ping function, which is used to test whether the network is connected

1. Obtain the host name based on the IP address

/// <Summary>
/// Obtain the host name based on the IP address
/// </Summary>
/// <Param name = "ip"> Host IP address </param>
/// <Returns> host name </returns>
Public String gethostnamebyip (string IP)
{
IP = IP. Trim ();
If (IP = string. Empty)
Return string. empty;
Try
{
// Ping or not
If (this. Ping (IP ))
{
System. net. iphostentry host = system. net. DNS. gethostentry (IP );
Return host. hostname;
}
Else
Return string. empty;
}
Catch (exception)
{
Return string. empty;
}
}

(If your computer can access the Internet you can even query: IP Address "64.233.189.104" is Google's host named "hk-in-f104.google.com" ip address.

This. Ping (IP) method in the code will be discussed later.

Now that we know how to "obtain the host name based on the IP address", we need to talk about how to "obtain the Host IP address based on the host name.

2. Obtain the Host IP address based on the host name.

/// <Summary>
/// Obtain the Host IP address based on the host name (domain name)
/// </Summary>
/// <Param name = "hostname"> host name or domain name </param>
/// <Example> getipbydomain ("www.google.com"); </example>
/// <Returns> Host IP address </returns>
Public String getipbyhostname (string hostname)
{
Hostname = hostname. Trim ();
If (hostname = string. Empty)
Return string. empty;
Try
{
System. net. iphostentry host = system. net. DNS. gethostentry (hostname );
Return host. Addresslist. getvalue (0). tostring ();
}
Catch (exception)
{
Return string. empty;
}
}

NOTE: If your computer can access the Internet, you can even find that the IP address of "www.google.com" is "64.233.189.104 ".

Finally, let's talk about C #'s simple Ping function to test whether the network is connected.

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

/// <Summary>
/// Whether the specified host can be pinged
/// </Summary>
/// <Param name = "ip"> IP address, host name, or domain name </param>
/// <Returns> true or false </returns>
Public bool Ping (string IP)
{
System. net. networkinformation. Ping P = new system. net. networkinformation. Ping ();
System. net. networkinformation. pingoptions Options = new system. net. networkinformation. pingoptions ();
Options. dontfragment = true;
String data = "Test Data! ";
Byte [] buffer = encoding. ASCII. getbytes (data );
Int timeout = 1000; // timeout time, in milliseconds
System. net. networkinformation. pingreply reply = P. Send (IP, timeout, buffer, options );
If (reply. Status = system. net. networkinformation. ipstatus. Success)
Return true;
Else
Return false;
}

Private void button#click (Object sender, eventargs e) // obtain the IP address

{Textbox2.text = getipbyhostname (textbox1.text );}

Private void button2_click (Object sender, eventargs e) // obtain the Host Name

{Textbox1.text = gethostnamebyip (textbox2.text );}

Private void button3_click (Object sender, eventargs e) // obtain the local IP Address

{

String hostname = system. net. DNS. gethostname ();

Iphostentry ipentry = system. net. DNS. gethostentry (hostname );

// DNS. gethostbyname (myname); expired

IPaddress [] ADDR = ipentry. Addresslist;

Textbox2.text = ADDR [0]. tostring ();

}

/// <Summary>

/// LAN search event

/// </Summary>

Private void lansearch ()

{

String localhost = (DNS. gethostentry (DNS. gethostname ()))

. Addresslist [0]. tostring (); // the IP address of the local host.

String STR = localhost. substring (0, localhost. lastindexof ("."));

For (INT I = 0; I <255; I ++) // create 255 threads to scan IP addresses

{

String IP = STR + "." + I. tostring ();

This. Text = IP;

If (Ping (IP ))

{

Listbox1.items. Add (IP );

Application. doevents ();

}

Progressbarsearch. value = progressbarsearch. Value + 1;

}

}

Private void button4_click (Object sender, eventargs e) // online host

{

Lansearch ();

}

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.