在本地网络中查找某些windows手机设备的ip



我正在尝试构建C#应用程序(桌面、windows,现在并不重要),我想用它连接到我的windows手机,使用套接字传输一些数据。。。我知道如何做到这一点,。

当通过插座连接时,我不想手动输入windows手机设备的IP地址。所以我想知道是否有可能从Windows手机应用程序发送一些HTTP请求和一些消息,并在计算机上获取该消息,以确定哪个IP是Windows手机的IP?

换句话说,如何从网络上设备的IP地址包中知道哪个IP地址属于Windows手机的IP地址?

从此处衰减此链接

添加命名空间:

使用Windows.Networking

 public static string FindIPAddress()
        {
            List<string> ipAddresses = new List<string>();
            var hostnames = NetworkInformation.GetHostNames();
            foreach (var hn in hostnames)
            {
                //IanaInterfaceType == 71 => Wifi
                //IanaInterfaceType == 6 => Ethernet (Emulator)
                if (hn.IPInformation != null && 
                    (hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71 
                    || hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
                {
                    string ipAddress = hn.DisplayName;
                    ipAddresses.Add(ipAddress);
                }
            }
            if (ipAddresses.Count < 1)
            {
                return null;
            }
            else if (ipAddresses.Count == 1)
            {
                return ipAddresses[0];
            }
            else
            {
                //if multiple suitable address were found use the last one
                //(regularly the external interface of an emulated device)
                return ipAddresses[ipAddresses.Count - 1];
            }
        }

编辑:

如果我错了,你想从一堆IP地址中获得特定的Windows Phone IP地址。AFAIK你不能这样。但我建议你在获取Windows Phone的IP时附加一些字符串,如下所示,这将与其他IP地址不同。

    string IPadd = FindIPAddress();    
    string WPIPadd = IPadd + " - Windows phone "
    MessageDialog.show(WPIPadd);

如果你发现除此之外还有什么新东西,请告诉我们。感谢

最新更新