C # ping

Source: Internet
Author: User

First, add the textbox, listbox, And button controls. Enter the domain name or IP address for textbox, and display the result for listbox.

Type

Private void button#click (object sender, EventArgs e)

{

Ping p1 = new Ping (); // only demo, no error handling

PingReply reply = p1.Send (this. textBox1.Text); // blocked displayReply (reply); // display result} private void displayReply (PingReply reply) // display result

{

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 perform asynchronous processing, modify the button#click, and add the PingCompletedCallBack method.

Private void button#click (object sender, EventArgs e)

{

Ping p1 = new Ping ();

P1.PingCompleted + = new PingCompletedEventHandler (this. PingCompletedCallBack); // set the 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.