正确接收使用StreamSocket挂起数据 [英] Correctly receiving data using StreamSocket hangs

查看:396
本文介绍了正确接收使用StreamSocket挂起数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 StreamSocketListener 来收到一款Windows Phone 8(作为服务器)通过HTTP POST客户端发送的数据。我可以接收数据,但是当所有的数据被读取 DataReader.LoadAsync()方法挂起,直到我手动取消从客户端的连接:

  _streamSocket =新StreamSocketListener(); 
_streamSocket.ConnectionReceived + = _streamSocket_ConnectionReceived;
等待_streamSocket.BindServiceNameAsync(30000);


使用专用异步无效_streamSocket_ConnectionReceived(StreamSocketListener发件人,
StreamSocketListenerConnectionReceivedEventArgs参数)
{
(IInputStream inStream中= args.Socket.InputStream)
{
DataReader的读者=新的DataReader(插播广告);
reader.InputStreamOptions = InputStreamOptions.Partial;


{
numReadBytes =等待reader.LoadAsync(1 LT;< 20); //挂起时,读出所有数据,直到客户取消的连接,例如numReadBytes == 0
System.Diagnostics.Debug.WriteLine(读:+ numReadBytes);

//处理数据
如果(numReadBytes大于0)
{
字节[] = tmpBuf新的字节[numReadBytes]
reader.ReadBytes(tmpBuf);
}
}当(numReadBytes大于0);
}

System.Diagnostics.Debug.WriteLine(读完);
}

什么不对的示例代码?


< DIV CLASS =h2_lin>解决方案

据推测,客户端使用HTTP 1.1连接保持 - 所以它不是关闭流。你在等待客户端发送更多的数据,但客户端没有发送任何更多的数据,因为它是完成该请求的打算。



您应该使用从HTTP请求内容长度知道有多少数据,以尝试接收 - 和使用超时,以避免在一个非常乖的客户端的情况下,永远等待


I am using a StreamSocketListener to receive data on a Windows Phone 8 (acting as server) sent by a HTTP POST client. I can receive the data but when all data is read the DataReader.LoadAsync() method hangs until I manually cancel the connection from the client:

_streamSocket = new StreamSocketListener();
_streamSocket.ConnectionReceived += _streamSocket_ConnectionReceived;
await _streamSocket.BindServiceNameAsync("30000");


private async void _streamSocket_ConnectionReceived(StreamSocketListener sender,
        StreamSocketListenerConnectionReceivedEventArgs args)
{
    using (IInputStream inStream = args.Socket.InputStream)
    {
        DataReader reader = new DataReader(inStream);
        reader.InputStreamOptions = InputStreamOptions.Partial;

        do
        {
            numReadBytes = await reader.LoadAsync(1 << 20);  // "Hangs" when all data is read until the client cancels the connection, e.g. numReadBytes == 0
            System.Diagnostics.Debug.WriteLine("Read: " + numReadBytes);

            // Process data
            if (numReadBytes > 0)
            {
                byte[] tmpBuf = new byte[numReadBytes];
                reader.ReadBytes(tmpBuf);
            }
        } while (numReadBytes > 0);
    }

    System.Diagnostics.Debug.WriteLine("Finished reading");
}

What's wrong with this sample code?

解决方案

Presumably the client is using HTTP 1.1 with connection keep-alive - so it's not closing the stream. You're waiting for the client to send more data, but the client has no intention of sending any more data because it's "done" for that request.

You should use the content length from the HTTP request to know how much data to try to receive - and use a timeout to avoid waiting forever in the case of a badly-behaved client.

这篇关于正确接收使用StreamSocket挂起数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆