Ping.SendAsync() 从 0.0.0.0 返回重播,如何获取 ping 地址?

发布于 2024-11-18 19:28:08 字数 1972 浏览 2 评论 0原文

我对 C# 中的 Ping.SendAsync() 函数有问题。 我 ping 一些 IP 地址,但其中一些是错误的。我需要从列表中删除错误的地址。但是如何,因为 p_PingCompleted 事件 args.replay.adress0.0.0.0? 这是我的代码:

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");
            }
        }

输出是:

正在 Ping 16.5.4.3...
来自 0.0.0.0 的 Ping 回复有 0 毫秒。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花随流水 2024-11-25 19:28:08

您可以使用 UserState 属性 和一个锁,以确保对 GameList 进行序列化访问:

byte[] buffer = Encoding.ASCII.GetBytes (new string('a', 32));
var options = new PingOptions (32, true);

Ping p = new Ping();
p.PingCompleted += p_PingCompleted;
foreach (Game curgame in GameList.ToArray())
{
    // e.UserState will be curgame
    p.SendAsync(IPAddress.Parse(curgame.IP), 2500, buffer, options, curgame);
}

然后在您的 p_PingCompleted 处理程序中:

void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
    var game = (Game)e.UserState;
    if (e.Reply.Status != IPStatus.Success)
    {
        Console.WriteLine(
            "  Could not reach {0} ({1}), removing this server...",
            game.IP,
            e.Reply.Status);
        lock (GameList)
        {
            GameList.Remove(game);
        }
    }
    else
    {
        Console.WriteLine(
            "Ping reply from: {0} in {1} ms",
            e.Reply.Address,
            e.Reply.RoundtripTime);
        if (e.Reply.RoundtripTime == 0 ||
            e.Reply.RoundtripTime >= 2500)
        {
            Console.WriteLine("  Ping too high, removing this server...");
            lock (GameList)
            {
                GameList.Remove(game);
            }
        }
    }
}

You could use the UserState property and a lock to ensure serialized access to GameList:

byte[] buffer = Encoding.ASCII.GetBytes (new string('a', 32));
var options = new PingOptions (32, true);

Ping p = new Ping();
p.PingCompleted += p_PingCompleted;
foreach (Game curgame in GameList.ToArray())
{
    // e.UserState will be curgame
    p.SendAsync(IPAddress.Parse(curgame.IP), 2500, buffer, options, curgame);
}

Then in your p_PingCompleted handler:

void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
    var game = (Game)e.UserState;
    if (e.Reply.Status != IPStatus.Success)
    {
        Console.WriteLine(
            "  Could not reach {0} ({1}), removing this server...",
            game.IP,
            e.Reply.Status);
        lock (GameList)
        {
            GameList.Remove(game);
        }
    }
    else
    {
        Console.WriteLine(
            "Ping reply from: {0} in {1} ms",
            e.Reply.Address,
            e.Reply.RoundtripTime);
        if (e.Reply.RoundtripTime == 0 ||
            e.Reply.RoundtripTime >= 2500)
        {
            Console.WriteLine("  Ping too high, removing this server...");
            lock (GameList)
            {
                GameList.Remove(game);
            }
        }
    }
}
落花随流水 2024-11-25 19:28:08

在异步调用中,发送 Ping 对象(或游戏对象,无论您正在处理的任何列表)作为对象状态,然后从 ping 完成事件中,如果地址无效,只需从列表中删除该对象。

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.

最单纯的乌龟 2024-11-25 19:28:08

我认为这与您使用 SendAsync(string, object) 的方式有关。

MSDN 上,它有一个很好的例子。请改用 SendAsync(String, Int32, Byte[], PingOptions, Object)SendAsync(IPAddress, Int32, Byte[], PingOptions, Object)。按照这个例子,它应该对你有用。

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) or SendAsync(IPAddress, Int32, Byte[], PingOptions, Object) instead. Follow that example and it should work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文