Ping in C #

Source: Internet
Author: User
Tags error handling

First add the Textbox,listbox,button control, where the textbox enters the domain name or the ip,listbox displays the result.

In the Button1_Click event type

private void Button1_Click (object sender, EventArgs e)

{

ping P1 = new Ping (); Just demo, do not do error handling

pingreply reply = p1. Send (This.textBox1.Text);//Blocking mode displayreply (reply); Display results} private void Displayreply (pingreply Reply)//Display results

{

StringBuilder Sbuilder;

if (reply. Status = = ipstatus.success)

{

Sbuilder = new StringBuilder ();

Sbuilder. Append (String. Format ("Address: {0}", reply.) Address.tostring ()));

Sbuilder. Append (String. Format ("roundtrip time: {0}", reply.) Roundtriptime));

Sbuilder. Append (String. Format ("Time to Live: {0}", reply.) OPTIONS.TTL));

Sbuilder. Append (String. Format ("Don t fragment: {0}", reply.) Options.dontfragment));

Sbuilder. Append (String. Format ("Buffer size: {0}", reply.) Buffer.length));

LISTBOX1.ITEMS.ADD (Sbuilder. ToString ());

}

}

You can also do asynchronous processing, modify button1_click, and add Pingcompletedcallback methods

private void Button1_Click (object sender, EventArgs e)

{

ping P1 = new Ping ();

P1. pingcompleted + = new Pingcompletedeventhandler (this. Pingcompletedcallback);//Set pingcompleted event handler

P1. SendAsync (this.textBox1.Text, NULL);

}

private void Pingcompletedcallback (object sender, Pingcompletedeventargs e)

{

if (e.cancelled)

{

LISTBOX1.ITEMS.ADD ("Ping canncel");

Return

}

if (e.error!= null)

{

LISTBOX1.ITEMS.ADD (E.error.message);

Return

}

StringBuilder Sbuilder;

pingreply reply = e.reply;

if (reply. Status = = ipstatus.success)

{

Sbuilder = new StringBuilder ();

Sbuilder. Append (String. Format ("Address: {0}", reply.) Address.tostring ()));

Sbuilder. Append (String. Format ("roundtrip time: {0}", reply.) Roundtriptime));

Sbuilder. Append (String. Format ("Time to Live: {0}", reply.) OPTIONS.TTL));

Sbuilder. Append (String. Format ("Don t fragment: {0}", reply.) Options.dontfragment));

Sbuilder. Append (String. Format ("Buffer size: {0}", reply.) Buffer.length));

LISTBOX1.ITEMS.ADD (Sbuilder. ToString ());

}

}

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.