sending ping with specific bytes amount using c#
how can i send a ping request with a specific size of bytes, the same as determining the -l when sending ping through the command line. can you give me an example?
also c开发者_如何学编程an i determine the amount of packets that the ping sends? like the -n on the command line.
thanks :)
You can use the System.Net.NetworkInformation.Ping class to send ICMP echo requests. It gives you complete control over the packet size and the number of packets sent:
using System.Net.NetworkInformation;
public void PingHost(string host, int packetSize, int packetCount)
{
int timeout = 1000; // 1 second timeout.
byte[] packet = new byte[packetSize];
// Initialize your packet bytes as you see fit.
Ping pinger = new Ping();
for (int i = 0; i < packetCount; ++i) {
pinger.Send(host, timeout, packet);
}
}
Ping message is sent using the ICMP using System.Net.NetworkInformation.Ping class. Here is simple example to send ping message to specific IP or Website. If you do not send your specific byte array, .Net automatically sent it's own byte array.
Example :
Ping objPing = new Ping();
try
{
PingReply objReply = objPing.Send(txtURL.Text, 1000);
if (objReply.Status == IPStatus.Success)
{
lblProductName.Text = string.Format("<b>Success</b> - IP Address:{0} Time:{1}ms", objReply.Address, objReply.RoundtripTime);
}
else
{
lblProductName.Text = objReply.Status.ToString();
}
}
catch (Exception ex)
{
lblProductName.Text = ex.Message;
}
精彩评论