Windows phone silverlight 套接字 HostNotFound 错误

Windows phone silverlight socket HostNotFound error

我在尝试异步连接套接字时收到 HostNotFound 错误。我敢肯定,主机正在工作。最奇怪的是,所有从“8.1 u1 *”开始的模拟器都不会出现这样的错误。他们连接没有任何问题。只有我的设备 (htc windows phone 8s) 和没有 8.1 的模拟器会出现此错误。主机地址109.235.68.205,端口6005。我的目标是8.0windowsphone。我不知道如何解决它。

public string Connect(string hostName, int portNumber)
    {

        string result = string.Empty;

        // Create DnsEndPoint. The hostName and port are passed in to this method.
        DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);

        // Create a stream-based, TCP socket using the InterNetwork Address Family. 
        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        // Create a SocketAsyncEventArgs object to be used in the connection request
        SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
        socketEventArg.RemoteEndPoint = hostEntry;


        // Inline event handler for the Completed event.
        // Note: This event handler was implemented inline in order to make this method self-contained.
        socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
        {
            // Retrieve the result of this request
            result = e.SocketError.ToString();

            // Signal that the request is complete, unblocking the UI thread
            _clientDone.Set();
            if(OnConnect != null)
                OnConnect(true, new ConnectionEventArgs() { Response = result });
        });



        // Sets the state of the event to nonsignaled, causing threads to block
        _clientDone.Reset();

        // Make an asynchronous Connect request over the socket
        _socket.ConnectAsync(socketEventArg);

        // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
        // If no response comes back within this time then proceed
        _clientDone.WaitOne(TIMEOUT_MILLISECONDS);

        return result;
    }

我能够在 Windows Phone 8 个模拟器上成功 运行 你的代码,目标是我的一台网络机器。

socket.ConnectAsync 调用已经是一个异步请求,不会阻塞您的 UI 所以我认为您不需要手动步骤来管理线程同步。

但是如果你坚持要这样实现,你可以在你的代码中用IPEndpoint替换DnsEndPoint。

IPEndPoint hostEntry = new IPEndPoint(IPAddress.Parse(hostName), portNumber);

希望这对您有所帮助。