【问题标题】:ICMP Ping in WinRT - Is it possible? [duplicate]WinRT 中的 ICMP Ping - 可能吗? [复制]
【发布时间】:2012-10-16 18:16:46
【问题描述】:

如何在 WinRT Modern UI 应用程序中执行 ICMP ping?

目前在 WinRT 中没有实现 Ping(请参阅相关问题 here),Silverlight 中以前的策略是:

  • 使用 WCF 服务
  • 调用 Javascript,然后调用 ActiveX 组件
  • 放弃 (here)

Vasily here 使用 http 在特定端口上使用 StreamSocket 来“ping”网络服务器,StreamSocket 支持使用 TCP 套接字进行网络通信。

如果我想为 WinRT 编写自己的 ICMP 库,也许Windows.Networking.Sockets 是我必须使用的最高级别 API。

This 实现使用 System.Net.Sockets 发出 ICMP 回显请求 - 在标准 .NET 中

This WinRT 示例使用 Windows.Networking.Sockets.DatagramSocket 类创建 UDP 套接字。我认为我需要的是原始套接字来执行 ICMP。

这甚至可以在 WinRT 沙盒中进行 ICMP ping 吗?

【问题讨论】:

    标签: c# networking windows-runtime icmp


    【解决方案1】:

    类似:

    try
                {
                    using (var tcpClient = new StreamSocket())
                    {
                        await tcpClient.ConnectAsync(
                            new Windows.Networking.HostName(HostName),
                            PortNumber,
                            SocketProtectionLevel.PlainSocket);
    
                        var localIp = tcpClient.Information.LocalAddress.DisplayName;
                        var remoteIp = tcpClient.Information.RemoteAddress.DisplayName;
    
                        ConnectionAttemptInformation = String.Format("Success, remote server contacted at IP address {0}",
                                                                     remoteIp);
                        tcpClient.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.HResult == -2147013895)
                    {
                        ConnectionAttemptInformation = "Error: No such host is known";
                    }
                    else if (ex.HResult == -2147014836)
                    {
                        ConnectionAttemptInformation = "Error: Timeout when connecting (check hostname and port)";
                    }
                    else
                    {
                        ConnectionAttemptInformation = "Error: Exception returned from network stack: " + ex.Message;
                    }
                }
                finally
                {
                    ConnectionInProgress = false;
                }
    

    完整来源:github

    【讨论】:

    • 谢谢 - 我相信 StreamSocket 是 TCP 传输级别的,因此无法用于创建 ICMP ping。
    • 您需要实际使用 Ping 还是简单地复制功能?
    • 是的,我需要使用 ICMP ping :-) 不幸的是,端口 80 或 443 请求还不够好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多