1

I need to calculate the network latency on a system which has multiple connected adapters . I am using the System.Net.NetworkInformation.Ping class to ping an address and use the RoundtripTime property to determine latency.

This works fine. However on a system with multiple connected adapters , I need to provide the source IP to use, to determine the latency on each of the available connections.

This class however does not provide an option to ping using a particular source IP address

I need something similar to the ping DOS command . This command has the option of -S which allows you to provide a source IP address.

Is there a way to specify the source IP address in System.Net.NetworkInformation.Ping. The PingOptions class does not provide any such option .

Thanks.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
Avinash Agarwal
  • 157
  • 1
  • 9

2 Answers2

0
//Provide any URL to ping.
Uri objURL = new Uri("ANY URL");
System.Net.NetworkInformation.Ping objPing = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions objPingOptn = new System.Net.NetworkInformation.PingOptions();

//Decides if packet to be sent in a go or divide in small chunks
objPingOptn.DontFragment = true;
//Creating a buffer of 32 bytes.
string tPacketData = "DummyPacketsDataDummyPacketsData";
byte[] bBuffer = Encoding.ASCII.GetBytes(tPacketData);
//Can provide host name directly if available
System.Net.NetworkInformation.PingReply objPingRply = objPing.Send(objURL.Host, 120, bBuffer, objPingOptn);

objPing.Dispose();
if (objPingRply.Status == System.Net.NetworkInformation.IPStatus.Success)
    return true;
else
    return false;
Flavio
  • 11,925
  • 3
  • 32
  • 36
Arick
  • 21
  • 5
0

I found this link (http://www.dreamincode.net/forums/topic/71263-using-the-ping-class-in-c%23/) helpful with looking at the Ping class but I have not found a way to set the source for a Ping.

One thing to keep in mind when using ICMP based pings is that networking equipment will often give ICMP traffic lower priority than normal packets, especially when the packets cross network boundaries such as WAN links. This can lead to pings being dropped or showing higher latency than traffic is actually experiencing and lends itself to being an indicator of problems rather than a measurement https://stackoverflow.com/a/1671489/901395

The biggest question may be is, is your application going to be on a network with QoS and if so what type of traffic are you really looking at measuring?

IPGlobalStatistics class may be of assistance: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalstatistics(v=vs.90).aspx

this answer may be helpful as well: https://stackoverflow.com/a/2506432/901395 using the code below to loop through the interfaces

class MainClass 
{ 
static void Main() 
{ 
    if (!NetworkInterface.GetIsNetworkAvailable()) 
       return; 

    NetworkInterface[] interfaces  
        = NetworkInterface.GetAllNetworkInterfaces(); 

    foreach (NetworkInterface ni in interfaces) 
    {                 
        Console.WriteLine("    Bytes Sent: {0}",  
            ni.GetIPv4Statistics().BytesSent); 
        Console.WriteLine("    Bytes Received: {0}", 
            ni.GetIPv4Statistics().BytesReceived); 
    } 
} 
} 
Community
  • 1
  • 1
Erick
  • 486
  • 2
  • 12
  • Thanks.I am not looking at measuring any particular traffic. I am working on a simple Windows application to monitor the quality of available network connections. The properties that I want to monitor are upload speed,download speed and latency. – Avinash Agarwal Feb 29 '12 at 18:56
  • I have updated the answer with another idea it doesn't solve latency but it helps with general statistics – Erick Feb 29 '12 at 19:29