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

2019-05-12 05:08发布

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条回答
贼婆χ
2楼-- · 2019-05-12 05:17

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

查看更多
登录 后发表回答