如何在Windows Phone中找到已连接网络的IP地址?

时间:2013-07-12 10:51:35

标签: windows-phone-7 networking windows-phone-8

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

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手机中,我转到设置 - &gt;系统 - &gt;移动电话和打开数据连接

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

1 个答案:

答案 0 :(得分:0)

你可以尝试这个.... 它可以很好地适用于许多网络,而不像你的代码,由于多播IP而只能在wifi网络上运行

它将为您提供手机的IP地址......

        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;
        }