i have problem with Ping.SendAsync() function in C#. I ping some ip adresses, but some of they are wrong. I need to remove wrong adresses from list. But how, because p_PingCompleted event args.replay.adress is 0.0.0.0? Here is my code:
System.Collections.Generic.List<Game> GameList = new System.Collections.Generic.List<Game>();
System.Timers.Timer timer = new System.Timers.Timer(5000);
public StartServer()
{
this.tcpListener = new TcpListener(IPAddress.Any, 8888);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
Console.WriteLine("Master server running...");
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Game[] games = GameList.ToArray();
foreach (Game curgame in games)
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
p.PingCompleted += new System.Net.NetworkInformation.PingCompletedEventHandler(p_PingCompleted);
p.SendAsync(IPAddress.Parse(curgame.IP), new object());
}
SendMessageToAll(output);
Console.WriteLine("Timer.Elapsed with data [" + output + "]");
}
void p_PingCompleted(object sender, System.Net.NetworkInformation.PingCompletedEventArgs e)
{
Console.WriteLine("Ping reply from: " + e.Reply.Address.ToString() + " has " + e.Reply.RoundtripTime.ToString() + " ms.");
if (e.Reply.RoundtripTime == 0 ||
e.Reply.RoundtripTime >= 2500)
{
Console.WriteLine(" Removing this server because ping is 0 or is greater than 2500 ms");
}
}
And output is:
Pinging 16.5.4.3... Ping reply from: 0.0.0.0 has 0 ms.
I think it has to do how you are using
SendAsync(string, object)
.At MSDN, it has a pretty good example. Use
SendAsync(String, Int32, Byte[], PingOptions, Object)
orSendAsync(IPAddress, Int32, Byte[], PingOptions, Object)
instead. Follow that example and it should work for you.In the async call, send the Ping object (or game object, whatever list you're dealing with) as the object state, and then from the ping completed event, just remove that object from the list if the address is invalid.
You could use the
UserState
property and a lock to ensure serialized access toGameList
:Then in your
p_PingCompleted
handler: