2012-10-16 36 views
3

如何在WinRT Modern UI應用程序中進行ICMP Ping?WinRT中的ICMP Ping - 可能嗎?

平沒有在WinRT中實現當前(請參閱相關的問題here),並在Silverlight爲前策略:

  • 使用WCF服務
  • 調用JavaScript,然後調用ActiveX組件
  • 給向上(here

瓦西here使用HTTP「平」一個網絡服務器的特定端口上使用StreamSocket支持使用TCP套接字進行網絡通信。

也許Windows.Networking.Socket s是最高級別的API我,如果我想要寫我自己的ICMP庫WinRT的使用..

This實現使用的System.Net.Sockets使ICMP迴應請求 - 標準.NET

This WinRT示例使用Windows.Networking.Sockets.DatagramSocket類創建一個UDP套接字。我認爲我需要的是用於ICMP的原始套接字。

這是甚至可能在WinRT沙箱ICMP ping?

回答

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

+0

感謝 - 我相信StreamSocket是TCP傳輸層,所以不能進行合作,創建ICMP ping命令。 –

+0

您是否需要真正使用Ping或簡單地複製功能? –

+3

是的,我需要使用ICMP ping :-)不幸的是,端口80或443請求不夠好。 –