StreamSocket, DataWriter.StoreAsync(), DataReader.LoadAsync() — Asynchronous problems

混江龙づ霸主 提交于 2019-12-11 04:08:28

问题


I'm creating a Win 8 store app in which I connect to a server, written in Java, using StreamSocket. When I run the app in debug, with breakpoints on StreamSocket.ConnectAsync(...), DataWriter.StoreAsync(), and DataReader.LoadAsync(...), it connects, sends the message, and receives a message back. However, once I remove any one of my breakpoints, that method doesn't do it's job. How can I can fix this issue? Here is my code:

public async void Connect()
{
    try
    {
        await socket.ConnectAsync(new Windows.Networking.HostName(ip),
            "50000", SocketProtectionLevel.PlainSocket);
        Connected = true;
    }
    catch (Exception e)
    {
        if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown)
        {
            throw;
        }

        Windows.UI.Popups.MessageDialog md =
            new Windows.UI.Popups.MessageDialog("Error: " + e.Message);
        return;
    }
    return;

}

public async void HandShake()
{
    try
    {

        //output
        writer = new DataWriter(socket.OutputStream);
        writer.UnicodeEncoding = 
             Windows.Storage.Streams.UnicodeEncoding.Utf8;

        byte[] nameBytes = Encoding.UTF8.GetBytes(Name.ToCharArray());
        writer.WriteBytes(nameBytes);
        await writer.StoreAsync();

        await writer.FlushAsync();
        writer.DetachStream();
        writer.Dispose();


        //input
        reader = new DataReader(socket.InputStream);
        reader.InputStreamOptions = InputStreamOptions.Partial;
        reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;

        uint bytesAvailable = await reader.LoadAsync(4096);
        byte[] byArray = new byte[bytesAvailable];
        reader.ReadBytes(byArray);
        string temp = Encoding.UTF8.GetString(byArray, 0,
            Convert.ToInt32(bytesAvailable));

        temp = temp.Substring(0, temp.Length - 1);

        if (temp == "NAME OK")
        {
            GoodName = true;
        }

        reader.DetachStream();
        reader.Dispose();
    }
    catch (Exception e)
    {
        //await Task.WhenAll(tasks.ToArray());
        if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown)
        {
            throw;
        }

        Windows.UI.Popups.MessageDialog md =
            new Windows.UI.Popups.MessageDialog("Error: " + e.Message);
        md.ShowAsync();
    }
}

回答1:


LoadAsync by default will not block until all the requested bytes have been read. You are probably receiving a partial message.

You'll need to implement whatever kind of message framing your protocol uses, as I describe on my blog.

P.S. Avoid async void. It really complicates your error handling.




回答2:


I changed the return type of Connect() to Task. Then called it as such, await Connect(); I put send and receive code in separate methods and did the same. My issue was an asynchronous problem and this fixed it.



来源:https://stackoverflow.com/questions/21359004/streamsocket-datawriter-storeasync-datareader-loadasync-asynchronous-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!