C# Ping is not fast enough - c#

I am trying to make a scan wether the pc is online or offline.
But my current code is way to slow to scan with a good performance as if an computer is offline there is a delay of 3 to 5 seconds.
I even added the timeout parameter set as 500 but it still takes more than 3 seconds if a computer is offline.
public bool PingComputer(string computername)
{
bool check = false;
Ping ping = new Ping();
try
{
PingReply reply = ping.Send(computername, 500);
check = reply.Status == IPStatus.Success;
}
catch (PingException)
{
}
return check;
}
I also already read about asynchron pings but i could´nt find a suitable solution yet that simply returns true if computer is online or false if it is offline.
Thanks in advance.

If you pretended to Ping a list of Computers you can use Parallel or use async Task.
I tested both methods bellow with the same 77 IPs. Used variable sec = 3.
Tasks toke 00:00:02.7146249
Parallel toke 00:00:05.9941404
To use the methods
Dictionary<string, bool> pingsReturn = await Network.PingListAsync(dictionary,3);
I can give you the 2 examples:
Task
public static async Task<Dictionary<string, bool>> PingListAsync(Dictionary<string, bool> HostList, int sec = 3)
{
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
// set a quick TTL
PingOptions options = new PingOptions(20, true);
// internal support Task to handle Ping Exceptions like "host not found"
async Task<KeyValuePair<string, bool>> PingHost(string host)
{
try
{
var pingresult = await Task.Run(() => new Ping().SendPingAsync(host, sec * 1000, buffer, options));
//t.Wait();
if (pingresult.Status == IPStatus.Success)
return new KeyValuePair<string, bool>(host, true);
else
return new KeyValuePair<string, bool>(host, false);
}
catch
{
return new KeyValuePair<string, bool>(host, false);
}
}
//Using Tasks >>
var watch = new Stopwatch();
watch.Start();
var tasksb = HostList.Select(HostName => PingHost(HostName.Key.ToString()));
var pinglist = await Task.WhenAll(tasksb);
foreach (var pingreply in pinglist)
{
HostList[pingreply.Key] = pingreply.Value;
}
watch.Stop();
Log.Debug("PingList (Tasks) Time elapsed: " + watch.Elapsed);
//Using Tasks <<
return HostList;
}
Parallel
public static async Task<Dictionary<string, bool>> PingListAsync(Dictionary<string, bool> HostList, int sec = 3)
{
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
// set a quick TTL
PingOptions options = new PingOptions(20, true);
//Using Parallel >>
watch = new Stopwatch();
watch.Start();
// avoid exception "Collection was modified; enumeration operation may not execute."
// we need new dictionary and add values
Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
Parallel.ForEach(HostList.Keys, (currHost) =>
{
try
{
var pingReply = new Ping().Send(currHost, sec * 1000, buffer, options);
if (pingReply.Status == IPStatus.Success)
dictionary.Add(currHost, true);
else
dictionary.Add(currHost, false);
}
catch
{
// handle Ping Exceptions like "host not found"
dictionary.Add(currHost, false);
}
});
watch.Stop();
Log.Debug("PingList (Parallel) Time elapsed: " + watch.Elapsed);
//Using Parallel <<
return dictionary;
}
PS - I know this is an old question, but is still valid.

Related

.NET Ping() giving false timeouts?

I have a script that is looping through and sending a ping to a host every second. The purpose of which is to test wireless performance on my home network. The issue is, I seem to be getting timeouts from my script, while a standard "ping -t" on either client (pingER and pingEE) have no such timeouts. In fact, not once in the last 30 minutes has either timed out, while my script has received timeout replies every few minutes.
My essential script is below. In all cases, when it does happen, my reply is TimedOut with a response time of 0ms. Is my script causing false timeouts? Thanks.
EDIT: updated code to reflect newest changes, still having same behavior.
while (true)
{
System.Threading.Thread.Sleep(delay);
var builder = new StringBuilder();
hosts.ForEach(h =>
{
var buffer = new byte[32];
var reply = ping.Send(h, 4000, buffer, new PingOptions(600, true));
var error = reply.Status != IPStatus.Success || reply.RoundtripTime > maxRoundTrip;
if (verbose || error)
{
builder.AppendFormat("{0}: {1} ({2}ms)", h, reply.Status, reply.RoundtripTime);
builder.Append(Environment.NewLine);
if (error)
{
WriteLog(h, reply);
SendEmail(h, reply);
}
}
});
Console.WriteLine(DateTime.Now);
Console.WriteLine(builder.ToString());
}
If your ping -t timeout value is less than 1000 or the value of maxRoundTrip is less than 1000 then you are checking against different timeouts in your two different pings (.NET Ping.Send vs. ping -t). The default Windows ping timeout is 4000 ms. http://technet.microsoft.com/en-us/library/bb490968.aspx
Also in your .NET code, you are sending the max buffer size of 65500 bytes. The default Windows ping command sends only 32 bytes of data.
So the difference between either the timeout or buffer size is likely what is causing the variance.
More info about the .NET Ping.Send method: http://msdn.microsoft.com/en-us/library/ms144956(v=vs.110).aspx
I might have figured it out. I was using the netbios name for the machine in question. I'm guessing that each time it did a ping, it was factoring in the IP lookup in the round trip and was timing out. When I used just the IP, it worked just fine and hasn't timed out.
Thanks for the input.
In my case, the byte array size is the main reason (I programmed a similar ping application)
When
var buffer = new byte[65500];
2014/3/30 11:29:26
8.8.8.8: TimedOut (0ms)
changed to
var buffer = new byte[32];
2014/3/30 11:32:50
8.8.8.8: Success (8ms)
I deduced that the ping buffer if too high then the ICMP echo message might be fragmented become more than one packets then only send out,therefore it cannot completed the ping within the timeout period. (But seem like OP mentioned in answer it is hostname resolve problem, then may be my deduction is wrong)
my sample code
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int delay = 1000;
Ping ping = new Ping();
//OP using something else?
System.Net.IPAddress h = IPAddress.Parse("8.8.8.8");
while (true)
{
System.Threading.Thread.Sleep(delay);
var builder = new StringBuilder();
//change buffer size change the result...
//var buffer = new byte[65500];
var buffer = new byte[32];
var reply = ping.Send(h, 1000, buffer, new PingOptions(600, false));
var error = reply.Status != IPStatus.Success || reply.RoundtripTime > 3000;
if (error)
{
builder.AppendFormat("{0}: {1} ({2}ms)", h, reply.Status, reply.RoundtripTime);
builder.Append(Environment.NewLine);
}
else
{
builder.AppendFormat("{0}: {1} ({2}ms)", h, reply.Status, reply.RoundtripTime);
}
Console.WriteLine(DateTime.Now);
Console.WriteLine(builder.ToString());
}
}
}
}
var reply = ping.Send(h, 1000, buffer, new PingOptions(600, false));
1000 is timeout (ms) for the ping reply...
I'm seeing the same behavior as the OP. My ping.Send() is only sending a buffer of byte[30] and always uses the explicit IP so no DNS/NetBIOS involved. I've tried varying timeout values but consistently see regular timeout returns from Send() vs zero timeouts when using ping from the command-line.
public PingCheck(string address, int timeout = 250, int pingNbr = 1)
{
var rtn = IPAddress.TryParse(address, out IPAddress addr);
if (rtn)
{
IpAddress = addr;
var dataSend = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
var pinger = new Ping();
var pingerOptions = new PingOptions();
pingerOptions.DontFragment = true;
var buffer = Encoding.ASCII.GetBytes(dataSend);
var statusList = new List<IPStatus>();
var lossList = new List<bool>();
var tripList = new List<long>();
for (int i=0; i<pingNbr; i++)
{
var pingerReply = pinger.Send(addr, timeout, buffer, pingerOptions);
var bufback = pingerReply.Buffer;
var dataBack = Encoding.ASCII.GetString(bufback);
var status = pingerReply.Status;
var loss = (dataSend != dataBack);
var msecs = pingerReply.RoundtripTime;
statusList.Add(status);
lossList.Add(loss);
tripList.Add(msecs);
}
// TODO: Add statistics check
}
else
throw new PingException("No IP Address");
}

Read continous bytestream from Stream using TcpClient and Reactive Extensions

Consider the following code:
internal class Program
{
private static void Main(string[] args)
{
var client = new TcpClient();
client.ConnectAsync("localhost", 7105).Wait();
var stream = client.GetStream();
var observable = stream.ReadDataObservable().Repeat();
var s = from d in observable.Buffer(4)
let headerLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(d.ToArray(), 2))
let b = observable.Take(headerLength)
select b.ToEnumerable().ToArray();
s.Subscribe(a => Console.WriteLine("{0}", a));
Console.ReadLine();
}
}
public static class Extensions
{
public static IObservable<byte> ReadDataObservable(this Stream stream)
{
return Observable.Defer(async () =>
{
var buffer = new byte[1024];
var readBytes = await stream.ReadAsync(buffer, 0, buffer.Length);
return buffer.Take(readBytes).ToObservable();
});
}
}
Basically I want to parse the messages I receive with Reactive Extensions. The header of the message is parsed correctly using the Buffer(4) and I get the length of the remainder of the message. The problem that arises is that when I do stream.Take(headerLength), the code reevaluates the whole "chain" and tries to get a new message from the stream instead of returning the rest of the bytes which already has been read from the stream. To be more exact, the first ReadAsync(...) returns 38 bytes, the Buffer(4) returns the first 4 of those, the observable.Take(headerLength) does not return the remainding 34 bytes but instead tries to read a new message with ReadAsync.
The question is, how can I make sure the observable.Take(headerLength) receives the already read 34 bytes and not try to read a new message from the stream? I've searched around for a solution, but I can't really figure out how to achieve this.
Edit: This solution (Using Reactive Extensions (Rx) for socket programming practical?) is not what I'm looking for. This isn't reading everything available in the stream (up to buffersize) and makes a continous bytestream out of it. To me this solution doesn't seem like a very efficient way to read from a stream, hence my question.
This approach isn't going to work. The problem is the way you are using the observable. Buffer will not read 4 bytes and quit, it will continually read 4 byte chunks. The Take forms a second subscription that will read overlapping bytes. You'll find it much easier to parse the stream directly into messages.
The following code makes a good deal of effort to clean up properly as well.
Assuming your Message is just this, (ToString added for testing):
public class Message
{
public byte[] PayLoad;
public override string ToString()
{
return Encoding.UTF8.GetString(PayLoad);
}
}
And you have acquired a Stream then you can parse it as follows. First, a method to read an exact number of bytes from a stream:
public async static Task ReadExactBytesAsync(
Stream stream, byte[] buffer, CancellationToken ct)
{
var count = buffer.Length;
var totalBytesRemaining = count;
var totalBytesRead = 0;
while (totalBytesRemaining != 0)
{
var bytesRead = await stream.ReadAsync(
buffer, totalBytesRead, totalBytesRemaining, ct);
ct.ThrowIfCancellationRequested();
totalBytesRead += bytesRead;
totalBytesRemaining -= bytesRead;
}
}
Then the conversion of a stream to IObservable<Message>:
public static IObservable<Message> ReadMessages(
Stream sourceStream,
IScheduler scheduler = null)
{
int subscribed = 0;
scheduler = scheduler ?? Scheduler.Default;
return Observable.Create<Message>(o =>
{
// first check there is only one subscriber
// (multiple stream readers would cause havoc)
int previous = Interlocked.CompareExchange(ref subscribed, 1, 0);
if (previous != 0)
o.OnError(new Exception(
"Only one subscriber is allowed for each stream."));
// we will return a disposable that cleans
// up both the scheduled task below and
// the source stream
var dispose = new CompositeDisposable
{
Disposable.Create(sourceStream.Dispose)
};
// use async scheduling to get nice imperative code
var schedule = scheduler.ScheduleAsync(async (ctrl, ct) =>
{
// store the header here each time
var header = new byte[4];
// loop until cancellation requested
while (!ct.IsCancellationRequested)
{
try
{
// read the exact number of bytes for a header
await ReadExactBytesAsync(sourceStream, header, ct);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
// pass through any problem in the stream and quit
o.OnError(new InvalidDataException("Error in stream.", ex));
return;
}
ct.ThrowIfCancellationRequested();
var bodyLength = IPAddress.NetworkToHostOrder(
BitConverter.ToInt16(header, 2));
// create buffer to read the message
var payload = new byte[bodyLength];
// read exact bytes as before
try
{
await ReadExactBytesAsync(sourceStream, payload, ct);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
o.OnError(new InvalidDataException("Error in stream.", ex));
return;
}
// create a new message and send it to client
var message = new Message { PayLoad = payload };
o.OnNext(message);
}
// wrap things up
ct.ThrowIfCancellationRequested();
o.OnCompleted();
});
// return the suscription handle
dispose.Add(schedule);
return dispose;
});
}
EDIT - Very hacky test code I used:
private static void Main(string[] args)
{
var listener = new TcpListener(IPAddress.Any, 12873);
listener.Start();
var listenTask = listener.AcceptTcpClientAsync();
listenTask.ContinueWith((Task<TcpClient> t) =>
{
var client = t.Result;
var stream = client.GetStream();
const string messageText = "Hello World!";
var body = Encoding.UTF8.GetBytes(messageText);
var header = BitConverter.GetBytes(
IPAddress.HostToNetworkOrder(body.Length));
for (int i = 0; i < 5; i++)
{
stream.Write(header, 0, 4);
stream.Write(body, 0, 4);
stream.Flush();
// deliberate nasty delay
Thread.Sleep(2000);
stream.Write(body, 4, body.Length - 4);
stream.Flush();
}
stream.Close();
listener.Stop();
});
var tcpClient = new TcpClient();
tcpClient.Connect(new IPEndPoint(IPAddress.Loopback, 12873));
var clientStream = tcpClient.GetStream();
ReadMessages(clientStream).Subscribe(
Console.WriteLine,
ex => Console.WriteLine("Error: " + ex.Message),
() => Console.WriteLine("Done!"));
Console.ReadLine();
}
Wrapping up
You need to think about setting a timeout for reads, in case the server dies, and some kind of "end message" should be sent by the server. Currently this method will just continually tries to receive bytes. As you haven't specced it, I haven't included anything like this - but if you do, then as I've written it just breaking out of the while loop will cause OnCompleted to be sent.
I guess what is needed here is Qactive: A Rx.Net based queryable reactive tcp server provider
Server
Observable
.Interval(TimeSpan.FromSeconds(1))
.ServeQbservableTcp(new IPEndPoint(IPAddress.Loopback, 3205))
.Subscribe();
Client
var datasourceAddress = new IPEndPoint(IPAddress.Loopback, 3205);
var datasource = new TcpQbservableClient<long>(datasourceAddress);
(
from value in datasource.Query()
//The code below is actually executed on the server
where value <= 5 || value >= 8
select value
)
.Subscribe(Console.WriteLine);
What´s mind blowing about this is that clients can say what and how frequently they want the data they receive and the server can still limit and control when, how frequent and how much data it returns.
For more info on this https://github.com/RxDave/Qactive
Another blog.sample
https://sachabarbs.wordpress.com/2016/12/23/rx-over-the-wire/

C# checking Internet connection [duplicate]

This question already has answers here:
What is the best way to check for Internet connectivity using .NET?
(27 answers)
Closed 8 years ago.
Can you please tell me if there is a way to check if there is a internet connection in my computer when my C# program is running. For a simple example, if internet is working, I would output a message box saying Internet is available. else I would output a message saying, Internet is unavailable.
Without using the library function to see if network is available (since this doesn't check internet connectivity)
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
Or without opening a webpage and seeing if it's return data
using (WebClient client = new WebClient())
htmlCode = client.DownloadString("http://google.com");
Because both of these above methods don't suit my needs.
a little shorter version:
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}
Another option is:
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success) {
// presumably online
}
You can find a broader discussion here
Consider the following code snippet...
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success)
{
// presumably online
}
Good Luck!
just wrote async functions to do that:
private void myPingCompletedCallback(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
return;
if (e.Error != null)
return;
if (e.Reply.Status == IPStatus.Success)
{
//ok connected to internet, do something...
}
}
private void checkInternet()
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(myPingCompletedCallback);
try
{
myPing.SendAsync("google.com", 3000 /*3 secs timeout*/, new byte[32], new PingOptions(64, true));
}
catch
{
}
}
My NetworkMonitor class now provides this (based on other responses here):
public bool IsInternetAvailable
{
get { return IsNetworkAvailable && _CanPingGoogle(); }
}
private static bool _CanPingGoogle()
{
const int timeout = 1000;
const string host = "google.com";
var ping = new Ping();
var buffer = new byte[32];
var pingOptions = new PingOptions();
try {
var reply = ping.Send(host, timeout, buffer, pingOptions);
return (reply != null && reply.Status == IPStatus.Success);
}
catch (Exception) {
return false;
}
}
This is my approach;
Check that a network connection is available, if it isn't then we wont be able to connect to another host.
Check if we can connect to some major hosts. Use a fallback just in case that site isn't available.
public static bool ConnectToInternet(int timeout_per_host_millis = 1000, string[] hosts_to_ping = null)
{
bool network_available = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
if (network_available)
{
string[] hosts = hosts_to_ping ?? new string[] { "www.google.com", "www.facebook.com" };
Ping p = new Ping();
foreach (string host in hosts)
{
try
{
PingReply r = p.Send(host, timeout_per_host_millis);
if (r.Status == IPStatus.Success)
return true;
}
catch { }
}
}
return false;
}
Notes:
Don't use too many hosts when reaching out, weigh up the cost in time of making all the pings against the diminishing likelihood of success.
If we send the ping to a certain that host we intend to connect to later (eg a http request) a returned ping doesn't mean we are connected to that particular host. Consider what would happen if the host is blocked. eg Facebook is blocked in Iran,China,... Does that ISP return the ping?
A DNS request wont be sufficient as it may be cached
public static bool HasConnection()
{
try
{
System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("www.google.com");
return true;
}
catch
{
return false;
}
}

Ping.Send(ip, timeout) in .Net returns too fast and complains on timeout

I'm using this method repeatedly in order to monitor connection to machine in the local network. When same test is done from cmd, results are steady and consistent:
C:\Windows\system32>ping -t 192.168.11.12
Pinging 192.168.11.12 with 32 bytes of data:
Reply from 192.168.11.12: bytes=32 time=1ms TTL=126
Reply from 192.168.11.12: bytes=32 time=1ms TTL=126
But when running from C# with 500 ms timeout it occasionally fails even before the timeout can expire:
public void TestIpAnswersPing()
{
var ip = "192.168.11.12";
var timeout = TimeSpan.FromMilliseconds(500);
var p = new Ping();
foreach (var i in Enumerable.Range(0, 1000))
{
var start = DateTime.Now;
PingReply reply = p.Send(ip, (int)timeout.TotalMilliseconds);
if(reply.Status != IPStatus.Success)
{
Debug.Assert(DateTime.Now - start >= timeout);
}
}
}
If I change timeout to 1 sec - all pass successfully with average time of pings 0.9 ms.
The only similar thing I have found is this - http://support.microsoft.com/kb/2533627, which doesn't help much.
Why this might happen and how to monitor high rate connection?
I had the same problem and fix it with multiple tries.
And don't know if Thread.Sleep(50); is necessary, but it seems to help.
Buffer, TimeOut, TTL are just snakeoil ;)
private PingReply Ping(string ip, int tryCount = 0)
{
var ping = new Ping();
byte[] buffer = new byte[65500];
var pingReply = ping.Send(ip, 500, buffer, new PingOptions(600, false));
if (pingReply.Status == IPStatus.Success)
{
return pingReply;
}
if (tryCount < 5)
{
Thread.Sleep(50);
return Ping(ip, tryCount + 1);
}
return pingReply;
}
Just in case someone still has this issue: because of some reason, Thread.Sleep(50) indeed solves the problem.

TraceRoute and Ping in C#

Does anyone have C# code handy for doing a ping and traceroute to a target computer? I am looking for a pure code solution, not what I'm doing now, which is invoking the ping.exe and tracert.exe program and parsing the output. I would like something more robust.
Given that I had to write a TraceRoute class today I figured I might as well share the source code.
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Net;
namespace Answer
{
public class TraceRoute
{
private const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
public static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress)
{
return GetTraceRoute(hostNameOrAddress, 1);
}
private static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress, int ttl)
{
Ping pinger = new Ping();
PingOptions pingerOptions = new PingOptions(ttl, true);
int timeout = 10000;
byte[] buffer = Encoding.ASCII.GetBytes(Data);
PingReply reply = default(PingReply);
reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions);
List<IPAddress> result = new List<IPAddress>();
if (reply.Status == IPStatus.Success)
{
result.Add(reply.Address);
}
else if (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)
{
//add the currently returned address if an address was found with this TTL
if (reply.Status == IPStatus.TtlExpired) result.Add(reply.Address);
//recurse to get the next address...
IEnumerable<IPAddress> tempResult = default(IEnumerable<IPAddress>);
tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1);
result.AddRange(tempResult);
}
else
{
//failure
}
return result;
}
}
}
And a VB version for anyone that wants/needs it
Public Class TraceRoute
Private Const Data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Public Shared Function GetTraceRoute(ByVal hostNameOrAddress As String) As IEnumerable(Of IPAddress)
Return GetTraceRoute(hostNameOrAddress, 1)
End Function
Private Shared Function GetTraceRoute(ByVal hostNameOrAddress As String, ByVal ttl As Integer) As IEnumerable(Of IPAddress)
Dim pinger As Ping = New Ping
Dim pingerOptions As PingOptions = New PingOptions(ttl, True)
Dim timeout As Integer = 10000
Dim buffer() As Byte = Encoding.ASCII.GetBytes(Data)
Dim reply As PingReply
reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions)
Dim result As List(Of IPAddress) = New List(Of IPAddress)
If reply.Status = IPStatus.Success Then
result.Add(reply.Address)
ElseIf reply.Status = IPStatus.TtlExpired Then
'add the currently returned address
result.Add(reply.Address)
'recurse to get the next address...
Dim tempResult As IEnumerable(Of IPAddress)
tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1)
result.AddRange(tempResult)
Else
'failure
End If
Return result
End Function
End Class
This implementation is simple, lazy (properly enumerable) and it will not go on searching forever (maxTTL) like some of the other answers.
public static IEnumerable<IPAddress> GetTraceRoute(string hostname)
{
// following are similar to the defaults in the "traceroute" unix command.
const int timeout = 10000;
const int maxTTL = 30;
const int bufferSize = 32;
byte[] buffer = new byte[bufferSize];
new Random().NextBytes(buffer);
using (var pinger = new Ping())
{
for (int ttl = 1; ttl <= maxTTL; ttl++)
{
PingOptions options = new PingOptions(ttl, true);
PingReply reply = pinger.Send(hostname, timeout, buffer, options);
// we've found a route at this ttl
if (reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired)
yield return reply.Address;
// if we reach a status other than expired or timed out, we're done searching or there has been an error
if (reply.Status != IPStatus.TtlExpired && reply.Status != IPStatus.TimedOut)
break;
}
}
}
Although the Base Class Library includes Ping, the BCL does not include any tracert functionality.
However, a quick search reveals two open-source attempts, the first in C# the second in C++:
http://www.codeproject.com/KB/IP/tracert.aspx
http://www.codeguru.com/Cpp/I-N/network/basicnetworkoperations/article.php/c5457/
For the ping part, take a look at the Ping class on MSDN.
This is the most efficient way I could think of.
Please vote it up if you like it so others can benefit.
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
namespace NetRouteAnalysis
{
class Program
{
static void Main(string[] args)
{
var route = TraceRoute.GetTraceRoute("8.8.8.8")
foreach (var step in route)
{
Console.WriteLine($"{step.Address,-20} {step.Status,-20} \t{step.RoundtripTime} ms");
}
}
}
public static class TraceRoute
{
public static IEnumerable<PingReply> GetTraceRoute(string hostnameOrIp)
{
// Initial variables
var limit = 1000;
var buffer = new byte[32];
var pingOpts = new PingOptions(1, true);
var ping = new Ping();
// Result holder.
PingReply result = null;
do
{
result = ping.Send(hostnameOrIp, 4000, buffer, pingOpts);
pingOpts = new PingOptions(pingOpts.Ttl + 1, pingOpts.DontFragment);
if (result.Status != IPStatus.TimedOut)
{
yield return result;
}
}
while (result.Status != IPStatus.Success && pingOpts.Ttl < limit);
}
}
}
Ping: We can use the Ping class built into the .NET Framework.
Instantiate a Ping and subscribe to the PingCompleted event:
Ping pingSender = new Ping();
pingSender.PingCompleted += PingCompletedCallback;
Add code to configure and action the ping, e.g.:
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
string who = "www.google.com";
AutoResetEvent waiter = new AutoResetEvent(false);
int timeout = 12000;
PingOptions options = new PingOptions(64, true);
pingSender.SendAsync(who, timeout, buffer, options, waiter);
Add a PingCompletedEventHandler:
public static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
... Do stuff here
}
Code-dump of a full working example, based on MSDN's example:
public static void Main(string[] args)
{
string who = "www.google.com";
AutoResetEvent waiter = new AutoResetEvent(false);
Ping pingSender = new Ping();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += PingCompletedCallback;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
// Wait 12 seconds for a reply.
int timeout = 12000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions(64, true);
Console.WriteLine("Time to live: {0}", options.Ttl);
Console.WriteLine("Don't fragment: {0}", options.DontFragment);
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(who, timeout, buffer, options, waiter);
// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne();
Console.WriteLine("Ping example completed.");
}
public static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine("Ping failed:");
Console.WriteLine(e.Error.ToString());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
Console.WriteLine($"Roundtrip Time: {e.Reply.RoundtripTime}");
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
As am improvement to Scotts code answer above, I found that his solution doesn't work if the route tapers off into nothing before reaching the destination - it never returns. A better solution with at least a partial route could be this (which I've tested and it works well). You can change the '20' in the for loop to something bigger or smaller or try to detect if it's taking too long if you want to control the number of iterations some other way. Full credit to Scott for the original code - thanks.
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Net;
...
public static void TraceRoute(string hostNameOrAddress)
{
for (int i = 1; i < 20; i++)
{
IPAddress ip = GetTraceRoute(hostNameOrAddress, i);
if(ip == null)
{
break;
}
Console.WriteLine(ip.ToString());
}
}
private static IPAddress GetTraceRoute(string hostNameOrAddress, int ttl)
{
const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
Ping pinger = new Ping();
PingOptions pingerOptions = new PingOptions(ttl, true);
int timeout = 10000;
byte[] buffer = Encoding.ASCII.GetBytes(Data);
PingReply reply = default(PingReply);
reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions);
List<IPAddress> result = new List<IPAddress>();
if (reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired)
{
return reply.Address;
}
else
{
return null;
}
}

Categories

Resources