Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending ping with specific bytes amount using c#

Tags:

c#

.net

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 can i determine the amount of packets that the ping sends? like the -n on the command line.

thanks :)

like image 393
Michael A Avatar asked Dec 29 '10 10:12

Michael A


People also ask

How do I ping with specific packet size?

Type "ping -s " and press enter. Windows users will need to use "-l" instead of "-s." The default packet size is 56 bytes for Linux and Mac pings, and 32 bytes in Windows. The actual packet size will be slightly larger than what you enter due to the addition of the ICMP header information attached to the ping.

How do you change byte size in ping?

Type "-l" followed by a space, then a numeric value indicating the size of each data packet you wish to send, measured in bytes (the default is "32"). For example, type "ping -l 64" into the command prompt to adjust the size of each data packet to 64 bytes.

How do I ping 128 bytes of data?

Ping Packet Size In the above example, when we set the packet size to 100, it displays '128 bytes' in the output. This is because of the Ping packet header size, which is 28 bytes. So, if you specify the packet size as 100, 28 bytes for header will be added to it and 128 bytes will be sent.

How do I change my ping data size?

Though the default ping packet size is 32 bytes, we can change it by a simple command while pinging a destination from your computer. By using -l option, you can decide the size of the echo request packets. The valid value of the size of ICMP packets is from 0 to 65500.


Video Answer


1 Answers

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);
    }
}
like image 196
Frédéric Hamidi Avatar answered Oct 03 '22 13:10

Frédéric Hamidi