收到几个数据包后,读取方法崩溃

时间:2016-02-17 14:27:31

标签: wcf windows-8.1 datareader stream-socket-client

我的应用程序将用户凭据发送到服务器,如果它们是正确的,服务器会发送一个数据包告诉客户端他们的凭据是错误的还是正确的。经过几次虚拟测试后,我的客户端由于这行代码而崩溃;

            var count = await reader.LoadAsync(512);

调试显示计数为0,

但在崩溃之前,我能够向服务器发送七(7)个虚拟请求。客户能够阅读七(7)份回复。

    private async void loginButtonEvent(object sender, RoutedEventArgs e)
    {
        //disable the button so the user can't spam the login button and flood packets 
        loginButton.IsEnabled = false;

        //if the user is connected to the server, then allow
        if(connected)
        {
            //get the stream
            DataWriter writer = new DataWriter(clientSocket.OutputStream);

            //custom class (like a packet IO, messafe frame/ framing
            MessageData md = new MessageData();
            md.MyUser = new User(usernameTextBox.Text, passwordTextBox.Text);
            md.MyType = MessageData.mType.LOGINREQUEST;
            //Serialize it into a string, thank you newton.Json
            string output = JsonConvert.SerializeObject(md);
            //write to the server
            writer.WriteString(output);
            //dedicate and push
            await writer.StoreAsync();
            //flush like you flush a toilet so it doesn't get clogged in the pipeline
            await writer.FlushAsync();
            //detatch the stream, not sure why?
            writer.DetachStream();

            //get the input stream
            DataReader reader = new DataReader(clientSocket.InputStream);
            //create string to hold the data
            string receivedData;
            //dynamically process the data
            reader.InputStreamOptions = InputStreamOptions.Partial;
            //store the bytes in count? why do we pass 512? What if I send a picture to the server and it is 1mb?
            var count = await reader.LoadAsync(512);
            //covert the byte to a string
            receivedData = reader.ReadString(count);
            //construct the md object into what the server sent
            md = JsonConvert.DeserializeObject<MessageData>(receivedData);
            switch (md.MyType)
            {
                case MessageData.mType.LOGINFAILED:
                    messageBox("Username or Password is wrong");
                    //Todo://
                    break;
                case MessageData.mType.LOGINSUCCESS:
                    messageBox("Logged");
                    //TODO: Go to login screen
                    break;
            }

            await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1));


        }
        loginButton.IsEnabled = true;


    }

我再次对此进行了测试,这次我能够向服务器发送十二(12)个数据包,并接收客户端可以解析的tweleve(12)数据包。

再一次,错误在于

            var count = await reader.LoadAsync(512);
  

MobileApp.exe中出现“System.ObjectDisposedException”类型的异常,但未在用户代码中处理

用户代码无法使用

Object Disposable Exception。

2 个答案:

答案 0 :(得分:1)

当您在异步方法中调用LoadAsync时,您将导航到棘手的水域。只应从UI线程调用一次LoadAsync方法。在引发LoadCompleted事件之前,不能再次调用该方法。很可能是你在上一个请求完成之前第二次调用它。

尝试将有问题的等待线切换为以下内容:

IAsyncOperation<int> asyncOp= reader.LoadAsync(512);
asyncOp.AsTask().Wait();
var count = asyncOp.GetResults();

如果您想要更详细的答案,请参阅以下答案:https://stackoverflow.com/a/9898995/3299157

答案 1 :(得分:1)

             //start a new task!
            Task t = new Task(ListenForMessage);
            //if the currrent task is not completed, then return
            //this means it wont calll var count = await reader.LoadAsync(512); I.E the devil
            if (!t.IsCompleted)
                return;

            //else if the task is completed, then run it!
            t.RunSynchronously();

感谢Ageonix,由于他的回答导致了这一实施,我得出了这个结论。

在信用到期时给予信用。