开发者

Silverlight 4 Socket ConnectAsync returns Success but socket is not connected

I have a policy file server up and running. For a while I was getting the AccessDenied because the policy file was not set properly. Now I no longer receive that error, so I know that's not the issue. I have a simple server running that simple loops on accepting client connections from any address. I also wrote a simple client, so I know the server works. In Silverlight I set my args and then call ConnectAsync. It return immedately on localhost (makes sense) and when I check the event args LastOperation is Connect and SocketError is Success. However, when I check my socket, it is not connected at all. Any ideas..? Been banging my head against a wall for hours over this.

A few other things I've tried. I moved the servers off my local box onto another server. Still didn't work. I did a packet capture and noticed that it is receiving the Poilcy File, but after that, there is no packet sent out by the browser to even attempt to connect to the other server.

    public void Connect(string ip)
    {
        SocketAsyncEventArgs saea = new SocketAsyncEventArgs();
        Socket socket开发者_运维技巧 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        var endpoint = new IPEndPoint(IPAddress.Parse(ip), 4502);
        saea.UserToken = socket;
        saea.RemoteEndPoint = endpoint;
        saea.Completed += new EventHandler<SocketAsyncEventArgs>(AsyncEventComplete);
        var completedSync = socket.ConnectAsync(saea);
        if (completedSync)
        {
            AsyncEventComplete(null, saea);
        }

        Result = ip;
    }

    void AsyncEventComplete(object sender, SocketAsyncEventArgs e)
    {
        switch (e.LastOperation)
        {
            case SocketAsyncOperation.Connect:
                MessageBox.Show("CONNECTED");
                break;
            case SocketAsyncOperation.Receive:
                MessageBox.Show("DATA RECEIEVED");
                // do stuff
                break;
        }
    }


I think you should use e.SocketError and not e.LastOperation

You could also use e.ConnectSocket (in Silverlight only)

You should also add a "not" in this condition : if ( ! completedSync )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜