简体   繁体   中英

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. 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.

Below is the code I am using to send the data. (StreamWriter AutoFlush is enabled)

        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?

Solved - Using sockets instead of the TcpClient class worked a treat and removing the stream overlays (BinaryReader/BinaryWriter)

You should not care. Because the TCP socket will do that automatically. TCP streams are not limited by MTO and there is NO reason to not use the sockets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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