如何在Windows Phone中找到连接网络的IP地址? [英] How can I find the IP address of connected network in windows phone?

查看:59
本文介绍了如何在Windows Phone中找到连接网络的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows Phone 中查找已连接网络的 IP 地址.我成功找到了连接的 Wi-FiIP 地址.我使用以下类来查找 IP 地址.

I am trying to find the IP address of connected network in windows phone. I was successful to find out the IP address of connected Wi-Fi. I have used the following class to find the IP address.

public class MyIPAddress
{
    Action<IPAddress> FoundCallback;
    UdpAnySourceMulticastClient MulticastSocket;
    const int PortNumber = 50000;       // pick a number, any number
    string MulticastMessage = "FIND-MY-IP-PLEASE" + new Random().Next().ToString();

    public void Find(Action<IPAddress> callback)
    {
        FoundCallback = callback;

        MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber);
        MulticastSocket.BeginJoinGroup((result) =>
        {
            try
            {
                MulticastSocket.EndJoinGroup(result);
                GroupJoined(result);
            }
            catch (Exception ex)
            {
                //  Debug.WriteLine("EndjoinGroup exception {0}", ex.Message);
                // This can happen eg when wifi is off
                FoundCallback(null);
            }
        },
            null);
    }

    void callback_send(IAsyncResult result)
    {
    }

    byte[] MulticastData;
    bool keepsearching;

    void GroupJoined(IAsyncResult result)
    {
        MulticastData = Encoding.UTF8.GetBytes(MulticastMessage);
        keepsearching = true;
        MulticastSocket.BeginSendToGroup(MulticastData, 0, MulticastData.Length, callback_send, null);

        while (keepsearching)
        {
            try
            {
                byte[] buffer = new byte[MulticastData.Length];
                MulticastSocket.BeginReceiveFromGroup(buffer, 0, buffer.Length, DoneReceiveFromGroup, buffer);
            }
            catch (Exception ex)
            {
                // Debug.WriteLine("Stopped Group read due to " + ex.Message);
                keepsearching = false;
            }
        }
    }

    void DoneReceiveFromGroup(IAsyncResult result)
    {
        string str = "";
        IPEndPoint where;
        int responselength = MulticastSocket.EndReceiveFromGroup(result, out where);
        byte[] buffer = result.AsyncState as byte[];
        if (responselength == MulticastData.Length && buffer.SequenceEqual(MulticastData))
        {
            str = where.Address.ToString();
            keepsearching = false;
            FoundCallback(where.Address);
        }

        Console.WriteLine(str);

    }
}

因此,通过使用上述类,我可以找到已连接 Wi-Fi 的 IP 地址.现在我试图找到通过数据连接连接的网络地址.在我的 Windows 手机中,我转到设置 --> 系统 --> 蜂窝网络并打开数据连接打开.

So by using the above class I can find the IP address of connected Wi-Fi. Now I am try to find the address of network which is connected by Data Connection. In my windows phone I goto Settings --> System --> Cellular and turn on data connection on.

如何获取蜂窝网络(数据连接)的 IP 地址?有没有相关的 API?

How can I get the IP address of Cellular Network(Data Connection)? Is there any API for that?

推荐答案

你可以试试这个....它适用于许多网络,这与您的代码不同,由于多播 IP,它只能在 wifi 网络上工作

you can try this .... it will work fine for many networks unlike your code which will work only on wifi network due to multicast IP

它将为您提供电话的 IP 地址...

it will provide you the ip address of the phone ...

        public static IPAddress Find()
        {
            List<string> ipAddresses = new List<string>();

            var hostnames = NetworkInformation.GetHostNames();
            foreach (var hn in hostnames)
            {
                if (hn.IPInformation != null)
                {
                    string ipAddress = hn.DisplayName;
                    ipAddresses.Add(ipAddress);
                }
            }

            IPAddress address = IPAddress.Parse(ipAddresses[0]);
            return address;
        }

这篇关于如何在Windows Phone中找到连接网络的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆