C# 在UWP中读取流时出现问题

C# 在UWP中读取流时出现问题,c#,.net,uwp,stream,network-programming,C#,.net,Uwp,Stream,Network Programming,我有一个使用StreamSocket的小UWP应用程序。通过使用socket.InputStream.AsStreamForRead()方法访问套接字 这几乎适用于所有大小的传入数据(10字节到6000字节)。但是当使用缓冲区大小的重载时,当接收到更多数据时,套接字将挂起。因此,如果缓冲区设置为4096,则不再接收6000字节。即使在读取10字节的数据块时,它也不起作用。ReadAsync方法永远挂起 我不确定这是否是一个错误。我希望我仍然可以收到数据。如果没有,我需要知道该缓冲区的默认大小或行

我有一个使用StreamSocket的小UWP应用程序。通过使用socket.InputStream.AsStreamForRead()方法访问套接字

这几乎适用于所有大小的传入数据(10字节到6000字节)。但是当使用缓冲区大小的重载时,当接收到更多数据时,套接字将挂起。因此,如果缓冲区设置为4096,则不再接收6000字节。即使在读取10字节的数据块时,它也不起作用。ReadAsync方法永远挂起

我不确定这是否是一个错误。我希望我仍然可以收到数据。如果没有,我需要知道该缓冲区的默认大小或行为

示例代码:

StreamSocket socket = InitSomewhere();
var readStream = socket.InputStream.AsStreamForRead(500);
var buffer = new byte[100]
readStream.ReadAsync(buffer, 0, 100) // Hangs here if received > 500!
有人有主意吗


致以最良好的祝愿,克里斯坦首先,我不能用官方样本在我这边复制这个问题

另一方面,您可以尝试使用类来读取上述示例中的数据

private async void OnConnection(
    StreamSocketListener sender, 
    StreamSocketListenerConnectionReceivedEventArgs args)
{
    DataReader reader = new DataReader(args.Socket.InputStream);
    try
    {
        while (true)
        {
            // Read first 4 bytes (length of the subsequent string).
            uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
            if (sizeFieldCount != sizeof(uint))
            {
                // The underlying socket was closed before we were able to read the whole data.
                return;
            }

            // Read the string.
            uint stringLength = reader.ReadUInt32();
            uint actualStringLength = await reader.LoadAsync(stringLength);
            if (stringLength != actualStringLength)
            {
                // The underlying socket was closed before we were able to read the whole data.
                return;
            }

            // Display the string on the screen. The event is invoked on a non-UI thread, so we need to marshal
            // the text back to the UI thread.
            NotifyUserFromAsyncThread(
                String.Format("Received data: \"{0}\"", reader.ReadString(actualStringLength)), 
                NotifyType.StatusMessage);
        }
    }