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 ());
}
}