如何在WinRT Modern UI应用程序中执行ICMP ping?
目前在WinRT中未实现Ping(请参阅相关问题here),以及Silverlight中以前的策略是:
Vasily here使用http来ping特定端口上的网络服务器,使用StreamSocket支持使用TCP套接字进行网络通信。
如果我想为WinRT编写自己的ICMP库,那么Windows.Networking.Socket可能是我必须使用的最高级API。
This实现使用System.Net.Sockets来生成ICMP echo请求 - 在标准.NET中
This WinRT示例使用Windows.Networking.Sockets.DatagramSocket类创建UDP套接字。我认为我需要的是原始套接字来做ICMP。
甚至可以在WinRT沙箱中进行ICMP ping吗?
答案 0 :(得分: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