Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Http服务器使用StreamSocketListener,选项为;InputStreamOptions.Partial“输入流选项”;返回数据不完整_C#_Sockets_Http_Networking_Tcp - Fatal编程技术网

C#Http服务器使用StreamSocketListener,选项为;InputStreamOptions.Partial“输入流选项”;返回数据不完整

C#Http服务器使用StreamSocketListener,选项为;InputStreamOptions.Partial“输入流选项”;返回数据不完整,c#,sockets,http,networking,tcp,C#,Sockets,Http,Networking,Tcp,首先,我正在做一个Windows通用应用程序项目。我使用StreamSocketListener创建了一个简单的tcp侦听器。数据来自连接到本地网络的设备。该设备随机发送Http和Http/Xml数据包 我创建了套接字侦听器,如下所示: StreamSocketListener listener = new StreamSocketListener(); public async void HttpServer() { listener.ConnectionReceived += (s,

首先,我正在做一个Windows通用应用程序项目。我使用
StreamSocketListener
创建了一个简单的tcp侦听器。数据来自连接到本地网络的设备。该设备随机发送Http和Http/Xml数据包

我创建了套接字侦听器,如下所示:

StreamSocketListener listener = new StreamSocketListener();

public async void HttpServer()
{
   listener.ConnectionReceived += (s, e) => ProcessRequestAsync(e.Socket);
   HostName hostName = new HostName("192.168.0.150");
   NetworkAdapter thisAdapter = hostName.IPInformation.NetworkAdapter;
   await listener.BindServiceNameAsync("5400", SocketProtectionLevel.PlainSocket, thisAdapter);
}
这是数据可用时的回调函数:

private const uint BufferSize = 12000;

private async void ProcessRequestAsync(StreamSocket socket)
{

    StringBuilder request = new StringBuilder();

    using (IInputStream input = socket.InputStream)
    {             
        byte[] data = new byte[BufferSize];
        IBuffer buffer = data.AsBuffer();
        uint dataRead = BufferSize;
        while (dataRead == BufferSize)
        {
            await input.ReadAsync(buffer, BufferSize, InputStreamOptions.None);
            request.Append(Encoding.UTF8.GetString(data, 0, (Int32)buffer.Length));
            dataRead = buffer.Length;
         }

    String message = request.ToString();

    ExampleTextLabel.Text = message;

    ....

}
问题出在这里:

我已将BufferSize设置为12000。这是因为,到目前为止,我收到的最大数据长度低于11500。内容的长度在HTML标题(content-length)中,但我不确定如何读取行前的标题:

await input.ReadAsync(buffer, BufferSize, InputStreamOptions.None);
当我使用InputStreamOptions.None选项时,我收到的消息会延迟一个数据包。换句话说,当设备发送数据包1时,它不会将其写入以下文本标签:

ExampleTextLabel.Text = message;
当数据包2到达时,ExampleTextLabel显示来自数据包1的消息。我知道这一点,因为我使用Wireshark来观察传入的数据包

另一方面,如果使用InputStreamOptions.Partial选项,则不会有延迟。我收到Wireshark中显示的数据包。然而,这种解决方案的问题是,大部分时间数据包都已损坏(不完整)

有人能给我解释一下这个问题吗?如果有什么解决办法的话

谢谢大家!

回答我的问题

InputStreamOptions.Partial是当一个或多个字节可用时完成的异步读取操作

这意味着有时它将在不完成数据包的情况下结束读取操作。我对上面的代码做了以下更改以使其正常工作(不是实际的代码,只是提供意见):


您好,您是否在StreamSocketListener上成功创建了通用http服务器?我也在开发通用应用程序,我也需要http服务器。我们可以启动github项目。是的,我的HTTP服务器按预期工作,但我没有使其通用。似乎没有太多的例子,所以我会让它通用,并与大家分享。
while (allDataRead)
{
    await input.ReadAsync(buffer, BufferSize, InputStreamOptions.None);
    request.Append(Encoding.UTF8.GetString(data, 0, (Int32)buffer.Length));
    dataRead = buffer.Length;

    //make sure the data read contains the html header, otherwise loop again
    //until you can extract the HTML header.
    //Get the content length from the HTTP header.

    //If the buffer size does not match the content length + html header length,
    //then loop again to get the remaining packets until the buffer length
    //matches the content length + html header length

    //If the content length matches the buffer length then you have received the packet completely.
    //allDataRead=true to exit the loop.

}