简体   繁体   English

Windows Phone一个套接字操作遇到网络中断

[英]Windows phone a socket operation encountered a dead network

I am trying to get the IP address of networks like Wi-Fi,Data Network . 我正在尝试获取Wi-Fi,数据网络等网络的IP地址。 I use the following class to find the 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);

    }
}

I was successful to find out the IP address of connected Wi-Fi . 我成功地找到了连接的Wi-Fi的IP地址。 I turn off Wi-Fi and turn on the Data Connection . 我关闭Wi-Fi并打开“ 数据连接” I am not able to get the IP address of connected network. 我无法获取所连接网络的IP地址。 I got the error ** a socket operation encountered a dead network**. 我收到错误**套接字操作遇到网络中断**。 I have also refer this question A socket operation encountered a dead network . 我也已经提到了这个问题一个套接字操作遇到了一个死网 How can I solve this problem ? 我怎么解决这个问题 ?

Question is a bit old, but answer may be useful for someone: 问题有点老了,但答案对某人可能有用:

You get this error, because your MyIPAddress class can only find a local IP (the address inside your internal WiFi network, behind router). 因为您的MyIPAddress类只能找到一个本地IP (内部WiFi网络内部的地址,在路由器后面),所以会出现此错误。 To get an external IP address you should call an external server that will tell you your IP (eg. whatismyip.com). 要获取一个外部IP地址,您应该呼叫一个可以告诉您IP的外部服务器(例如whatismyip.com)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM