简体   繁体   English

Http Web服务器-大响应数据包问题C#

[英]Http Web Server - big response packet issue C#

I have been making a HTTP web server for mainly a application generated uptime monitor but wanted to expand it so its more general use. 我一直在为主要由应用程序生成的正常运行时间监视器制作HTTP Web服务器,但希望对其进行扩展,以便更广泛地使用它。 A problem I am having with it is when a client requests for an image (for example) that is 1MB+ I don't know how to split the image data up to fit inside the MTU size of packets. 我遇到的一个问题是,当客户端请求的图像(例如)为1MB +时,我不知道如何将图像数据拆分成适合数据包MTU大小的大小。

Below is the code I am using to send the data. 以下是我用来发送数据的代码。 (StreamWriter AutoFlush is enabled) (已启用StreamWriter AutoFlush)

        private void sendResponse(Stream stream, string fileLocation, bool useGzip, string contentType, string ResponseLine, bool CloseConnection, string server)
    {
        writer.Write(ResponseLine + "/r/n");
        writer.Write("Server: " + server + "/r/n");
        if (CloseConnection == true)
            writer.Write("Connection: close" + "/r/n");
        else
            writer.Write("Connection: keep-alive" + "/r/n");

        //if (useGzip == true)
        //    writer.WriteLine("Content-Encoding: gzip")
        writer.Write("Content-Type: " + contentType + "/r/n");
        writer.Write("Content-Length: " + stream.Length + "/r/n");
        writer.Write("Accept-Ranging: bytes" + "/r/n");
        writer.Write("" + "/r/n");

        byte[] data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        writer.Write(data);
        writer.Flush();
    }

Also, in Wireshark the capture shows 500+ packets of max size with continuation or tcp segment, does HTTP have a way of splitting up big responses into multiple packets without tcp segmentation? 另外,在Wireshark中,捕获显示了500个以上具有连续或tcp段的最大大小的数据包,HTTP是否可以将大型响应拆分为多个数据包而不进行tcp分段?

Solved - Using sockets instead of the TcpClient class worked a treat and removing the stream overlays (BinaryReader/BinaryWriter) 已解决-使用套接字而不是TcpClient类可以处理并删除流叠加层(BinaryReader / BinaryWriter)

You should not care. 你不在乎 Because the TCP socket will do that automatically. 因为TCP套接字将自动执行此操作。 TCP streams are not limited by MTO and there is NO reason to not use the sockets. TCP流不受MTO的限制,没有理由不使用套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM