如何在winRT中实现AddressFamily

时间:2013-12-13 05:20:35

标签: c# windows-8 windows-phone-8 windows-runtime

我正在搜索System.Net.Sockets.AddressFamily.InterNetwork的替代品。我找到了HostName但无法找到像AdressFamily那样的东西。实际上,我正在转换winRT中的以下代码。我知道winRT支持Windows.Networking.Sockets等。请描述一些解决方案。 代码是,

if (System.Net.Sockets.AddressFamily.InterNetwork == _address.AddressFamily &&     _address.AddressFamily == address.AddressFamily)
        {
            long longLocal = BitConverter.ToInt32(_address.GetAddressBytes(), 0);
            long longNetMask = BitConverter.ToInt32(_netmask.GetAddressBytes(), 0);
            long longRemote = BitConverter.ToInt32(address.GetAddressBytes(), 0);
        }

_address也是IPAdress

1 个答案:

答案 0 :(得分:1)

AddressFamily并不直接支持WinRT / Metro项目,所以您需要检查ipAddress这样的信息

using Windows.Networking;
using Windows.Networking.Sockets;

HostName serverHost = new HostName("www.contoso.com");
StreamSocket clientSocket = new Windows.Networking.Sockets.StreamSocket();

// Try to connect to the remote host
await clientSocket.ConnectAsync(serverHost, "http");

var ipAddress = clientSocket.Information.RemoteAddress.DisplayName

要检查您的情况,请使用_address.Type == HostNameType.Ipv4然后执行某些操作,但请确保GetAddressBytes类中不存在HostName,因此您需要编写自己的函数来转换ipAddress 1}}到字节。