ICMP Ping in WinRT - Is it possible? [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

How to do an ICMP ping in a WinRT Modern UI application?

Ping is not implemented in WinRT currently (see related question here) and the previous strategies in Silverlight being:

  • Use a WCF Service
  • Call Javascript which then calls an ActiveX component
  • Give up (here)

Vasily here uses http to 'ping' a webserver on a specific port using StreamSocket which supports network communication using a TCP socket.

Perhaps Windows.Networking.Sockets is the highest level API I have to use if I want to write my own ICMP library for WinRT..

This implementation uses System.Net.Sockets to make an ICMP echo request - in standard .NET

This WinRT sample uses the Windows.Networking.Sockets.DatagramSocket class to create a UDP socket. I think what I need is raw sockets to do ICMP.

Is this even possible in the WinRT sandbox to ICMP ping?

回答1:

Something like:

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

full source here: github



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!