忽略通过HTTP GET C ++接收的char缓冲区中的转义字符

时间:2022-09-20 19:33:25

I'm sending a request to get an image from a webpage. I'm using C++ with winsocket and tcp http get request. I receive all the info in my char buffer but when I stream it to a file or string it's very short because there are string terminators in it. What's the best/most efficient way to deal with the escape characters?

我正在发送一个从网页上获取图片的请求。我正在使用C ++与winsocket和tcp http获取请求。我收到了char缓冲区中的所有信息,但是当我将它传输到文件或字符串时,它非常短,因为它中有字符串终止符。处理转义字符的最佳/最有效方法是什么?

Thanks in advance.

提前致谢。

EDIT: ofstream out("temp.jpg");

编辑:ofstream out(“temp.jpg”);

    //m_Received.reserve(STRINGBUFFERSIZE);
    char rBuffer[BUFFERSIZE];

    int readSize = 0;
    int totalSize = 0;

    do
    {
        PRINT("Reset buffer");
        ZeroMemory(rBuffer, sizeof(rBuffer));

        PRINT("Receiving...");
        readSize = recv(socket, rBuffer, sizeof(rBuffer), 0);

        PRINT("Received " << readSize << " bytes...");
        if (readSize > 0)
        {
            totalSize += readSize;
            //m_Received.append(rBuffer);

            for (int i = 0; i < readSize; ++i)
            {
                out << rBuffer[i];
            }

            if (readSize < BUFFERSIZE)
            {
                PRINT("Stopping receiving...");
                break;
            }
        }
        else if (readSize == -1)            
            throw SocketError("ErrorReceiving", readSize);

    } while (readSize > 0);

Even when putting each character in individually I get a small difference with the original image which leads to corruption.

即使将每个角色单独放入,我也会与原始图像产生微小的差异,从而导致腐败。

1 个解决方案

#1


Alright, so I fixed it by simply writing the char buffer binary to my file. Should have thought of this earlier.

好吧,所以我通过简单地将char缓冲区二进制文件写入我的文件来修复它。应该早点想到这个。

#1


Alright, so I fixed it by simply writing the char buffer binary to my file. Should have thought of this earlier.

好吧,所以我通过简单地将char缓冲区二进制文件写入我的文件来修复它。应该早点想到这个。