Send Multiple Pings without waiting for reply Windows C#
Im currently doing research towards my final year BSc project. The final product will include indoor location tracking functionality. The traditional, or most utilised method seems to be RSSI triangulation, but I am keen to attempt to improve the accuracy of the PING method as I think this w开发者_如何学运维ould be better suited to locations that may suffer from signal attenuation (the locations I am intending to use the device may have a moderate ammount of radio interference).
I was wondering if it was possible to write software in c# that would mimick the ping flood ability of linux ping utility(which enable the sending of multiple pings without waiting for a reply). I hypothesize that using multiple pings and timing them from the first to the last will enable the usage of the method over shorter distances.
Many Thanks
Dylan
try this hope it works fine
private ArrayList IPList()
{
string myipsplit = string.Empty;
string myip =
Dns.GetHostAddresses(
Dns.GetHostName())[0].ToString();
string[] myiparray = myip.Split(new[] {'.'});
for (int j = 1; j < myiparray.Length; j++)
myipsplit += myiparray[j - 1] + ".";
var sb = new ArrayList();
MyPing ping = delegate(int id)
{
string ls = myipsplit + id;
var pingSender = new System.Net.NetworkInformation.Ping();
PingReply reply = pingSender.Send(ls, 0);
if (reply != null)
if (reply.Status == IPStatus.Success)
sb.Add(ls);
};
var asyncResults = new IAsyncResult[0x100];
for (int i = 1; i < 0x100; i++)
asyncResults[i] = ping.BeginInvoke(i, null, null);
for (int i = 1; i < 0x100; i++)
ping.EndInvoke(asyncResults[i]);
return sb;
}
#region Nested type: MyPing
private delegate void MyPing(int id);
#endregion
You have to use the Ping.SendAsync() function. As stated in the MSDN page:
Each call to this method executes in a separate thread that is automatically allocated from the thread pool. When the asynchronous operation completes, it raises the PingCompleted event. To specify the method that is called when SendAsync raises the event, you must add a PingCompletedEventHandler delegate to the event before calling SendAsync.
Here's a splendid example on how to do it: http://msdn.microsoft.com/en-us/library/a63bsyf0.aspx
精彩评论