Thursday, February 11, 2010

How to ping Asynchronously C#



This is how you ping a Server/IP asynchronously. Meaning you can ping multiple hosts at once or on multiple threads as this command suggests. I've incorporated this into my code for my NetPinger 2010 Application available in Beta at the end at June 2010.

private void SendPing()

{

try

{

this.treePing.SelectedNode.ForeColor =

Color.OrangeRed;

}

catch (Exception ex)

{

mnuNotify.ShowBalloonTip(5000,

"NetPinger 2010",

ex.Message,

ToolTipIcon.Error); private void SendPing()

{

try

{

this.treePing.SelectedNode.ForeColor =

Color.OrangeRed;

}

catch (Exception ex)

{

mnuNotify.ShowBalloonTip(5000,

"NetPinger 2010",

ex.Message,

ToolTipIcon.Error);

toolStripStatusLabel1.Text = ex.Message;

}

pingSender = new System.Net.NetworkInformation.Ping();

pingSender.SendAsyncCancel();

// Create an event handler for ping complete

pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);

// Create a buffer of 32 bytes of data to be transmitted.

byte[] packetData = Encoding.ASCII.GetBytes("................................");

// Jump though 50 routing nodes tops, and don't fragment the packet

PingOptions packetOptions = new PingOptions(50, true);

// Send the ping asynchronously

this.lblServer.Text = thisServer;

this.lblDomain.Text = thisFQDN;

this.lblDevice.Text = thisDtype;

string sendThis = thisIP;

pingSender.SendAsync(thisIP, 2000, packetData, packetOptions, resetEvent);

}

toolStripStatusLabel1.Text = ex.Message;

}

pingSender = new System.Net.NetworkInformation.Ping();

pingSender.SendAsyncCancel();

// Create an event handler for ping complete

pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);

// Create a buffer of 32 bytes of data to be transmitted.

byte[] packetData = Encoding.ASCII.GetBytes("................................");

// Jump though 50 routing nodes tops, and don't fragment the packet

PingOptions packetOptions = new PingOptions(50, true);

// Send the ping asynchronously

this.lblServer.Text = thisServer;

this.lblDomain.Text = thisFQDN;

this.lblDevice.Text = thisDtype;

string sendThis = thisIP;

pingSender.SendAsync(thisIP, 2000, packetData, packetOptions, resetEvent);

}

public void ShowPingResults(PingReply pingResponse,

string temp1)

{

if (pingResponse == null)

{

// We got no response

txtPing.Text += "There was no response.\r\n\r\n";

if (this.lblIP.Text == "")

{

try

{

this.treePing.SelectedNode.ToolTipText =

"";

this.treePing.SelectedNode.ForeColor =

Color.OrangeRed;

}

catch

{

}

this.lblMachine.Text = "";

toolStripStatusLabel1.Text = "Error no response...";

picPing.Image =

NetPinger.Properties.Resources.off1;

lblPing.Text = "Dead";

lblPing.ForeColor = Color.OrangeRed;

}

if (this.lblIP.Text == "0.0.0.0")

{

this.treePing.SelectedNode.ForeColor =

Color.OrangeRed;

this.lblMachine.Text = "";

toolStripStatusLabel1.Text = "Error no response...";

picPing.Image =

NetPinger.Properties.Resources.off1;

lblPing.Text = "Dead";

lblPing.ForeColor = Color.OrangeRed;

}

return;

}

else if (pingResponse.Status == IPStatus.Success)

{

if (this.lblIP.Text != "0.0.0.0")

{

this.treePing.SelectedNode.ForeColor =

Color.LawnGreen;

// We got a response, let's see the statistics

txtPing.Text += "Reply from " + pingResponse.Address.ToString() + ": bytes=" + pingResponse.Buffer.Length + " time=" + pingResponse.RoundtripTime + " TTL=" + pingResponse.Options.Ttl + "\r\n";

lblPing.Text = "Alive";

string Addy = pingResponse.Address.ToString();

lblIP.Text =

Addy;

// treePing.SelectedNode.ToolTipText =

// lblIP.Text;

lblPing.ForeColor = Color.LawnGreen;

//lblServer.Text = "loading...";

lblIP.Text =

Addy;

picPing.Image = NetPinger.Properties.Resources.On1;

//this.lblServer.Text = "loading...";

this.lblServer.Text = thisServer;

this.lblDomain.Text = thisFQDN;

this.lblDevice.Text = thisDtype;

this.treePing.SelectedNode.ForeColor =

Color.LawnGreen;

try

{

System.Net.IPAddress ip =

System.Net.IPAddress.Parse(this.lblIP.Text);

this.lblMachine.Text = GetMacFromIP(ip).ToString();

this.treePing.SelectedNode.ForeColor =

Color.LawnGreen;

}

catch

{

}

}

}

else

{

//MessageBox.Show(lblIP.Text);

if ((lblIP.Text == "0.0.0.0") && (lblPing.Text != ""))

{

if (lblPing.Text != "Alive")

{

//this.lblServer.Text = "loading...";

toolStripStatusLabel1.Text = "Error no response...";

picPing.Image =

NetPinger.Properties.Resources.off1;

lblPing.Text = "Dead";

lblPing.ForeColor = Color.OrangeRed;

// The packet didn't get back as expected, explain why

txtPing.Text += "Ping was unsuccessful! " + "\r\n\r\n";

}

}

}

}



No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...