5

I am building a windows phone 7.1 app. I need to find out if the phone is connected to any Wifi and if so, what is its current IP in the local network (ie. 192.168.0.100 like this). I've been trying to find out these information for some time now. Please help.

I've managed to get the local IP on my console app by using the following code

public void ScanIP()
{                
    IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            String localIP = ip.ToString();
            Console.WriteLine(localIP);                        
        }
    }

    Console.ReadKey();
}

However, I need similar thing done for windows mobile 7 app. Any idea ? Please share.

4

2 回答 2

2

Do a multicast and listen for replies. Once you identify your multicast message, you can get the IP of the sender (which is yourself). You can use UdpAnySourceMulticastClient to do the multicasting. In case you're not in a wifi network, you will get a socket failure in the EndJoinGroup call. You should handle the exception and pass a specific value indicating you're not in a wifi network.

More info in this blog post by Andy Pennell.

于 2014-03-23T13:18:31.313 回答
0

它会为您提供手机的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;
    }
于 2013-07-18T07:13:32.757 回答