WinRT中的ICMP Ping – 有可能吗?
如何在WinRT Modern UI应用程序中执行ICMP ping?
目前在WinRT中没有实现Ping(请参阅此处的相关问题),以及Silverlight中以前的策略是:
- 使用WCF服务
- 调用Javascript然后调用ActiveX组件
- 放弃( 这里 )
Vasily 在这里使用http来“ping”特定端口上的Web服务器,使用StreamSocket支持使用TCP套接字进行网络通信。
也许Windows.Networking.Socket是我必须使用的最高级API,如果我想为WinRT编写自己的ICMP库。
此实现使用System.Net.Sockets来生成ICMP echo请求 – 在标准.NET中
此 WinRT示例使用Windows.Networking.Sockets.DatagramSocket类创建UDP套接字。 我认为我需要的是原始套接字来做ICMP。
这甚至可以在WinRT沙箱中进行ICMP ping吗?
就像是:
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