【问题标题】:"The image cannot be displayed error" when sending image over socket in C++在 C++ 中通过套接字发送图像时出现“图像无法显示错误”
【发布时间】:2017-02-28 18:58:43
【问题描述】:

我正在尝试通过套接字将所有类型的文件发送到 C++ 中的浏览器。我可以通过套接字很好地发送 .txt 和 .html 文件,但是当我尝试发送 jpeg 时,我收到错误 The image "localhost:8199/img.jpg" cannot be displayed because it contains errors。我不确定为什么我的程序可以正常发送文本文件但无法处理图像。这就是我读取文件并将其写入客户端的方式:

 int fileLength = read(in, buf, BUFSIZE);
    buf[fileLength] = 0;
    char *fileContents = buf;

    while (fileLength > 0) {

        string msg =  "HTTP/1.0 200 OK\r\nContent-Type:" + fileExt + "\r\n\r\n\r\nHere is response data";
        int bytes_written;

        if(vrsn == "1.1" || vrsn == "1.0"){
            write(fd, msg.c_str(), strlen(msg.c_str()));    
            bytes_written = write(fd, fileContents, fileLength);
        } else {
            bytes_written = write(fd, fileContents, fileLength);
        }

        fileLength -= bytes_written;
        fileContents += bytes_written;  
    }

完整代码在这里:http://pastebin.com/vU9N0gRi

如果我在浏览器网络控制台中检查响应标头,我会看到 Content-Type 是 image/jpeg,所以我不确定我做错了什么。

图像文件的处理方式是否与普通文本文件不同?如果是这样,我究竟需要做什么才能处理将图像文件发送到浏览器?

【问题讨论】:

  • 标头损坏和长度错误(图像不是字符串)。

标签: c++ image sockets


【解决方案1】:

string msg = "HTTP/1.0 200 OK\r\nContent-Type:" + fileExt + "\r\n\r\n\r\n这里是响应数据";

这是对二进制数据(如图像)的无效 HTTP 响应。在 HTTP 标头末尾终止 \r\n\r\n 之后,之后的所有内容都是消息正文数据。因此,您将\r\nHere is response data 作为图像的前几个字节发送,从而破坏了它们。您需要完全删除它,即使对于您的 txt 和 html 文件也是如此。

更糟糕的是,您会在每次循环迭代上发送msg,因此您会在每个文件数据缓冲区之前加上您的 HTTP 响应字符串,从而进一步彻底破坏您的图像数据。

此外,您的响应缺少 Content-Length 和 Connection: close 响应标头。

试试类似的方法:

int sendRaw(int fd, const void *buf, int buflen)
{
    const char *pbuf = static_cast<const char*>(buf);
    int bytes_written;

    while (buflen > 0) {
        bytes_written = write(fd, pbuf, buflen);
        if (written == -1) return -1;
        pbuf += bytes_written;  
        buflen -= bytes_written;
    }

    return 0;
}

int sendStr(int fd, const string &s)
{
    return sendRaw(fd, s.c_str(), s.length());
}

...

struct stat s;
fstat(in, &s);
off_t fileLength = s.st_size;

char buf[BUFSIZE];
int bytes_read, bytes_written;

if ((vrsn == "1.1") || (vrsn == "1.0")) {
    ostringstream msg;
    msg << "HTTP/1.0 200 OK\r\n"
        << "Content-Type:" << fileExt << "\r\n"
        << "Content-Length: " << fileLength << "\r\n"
        << "Connection: close\r\n"
        << "\r\n";
    sendStr(fd, msg.str());
}

while (fileLength > 0) {
    bytes_read = read(in, buf, min(fileLength, BUFSIZE));
    if (bytes_read <= 0) break;
    if (sendRaw(fd, buf, bytes_read) == -1) break;
    fileLength -= bytes_read;  
}

close(fd);

【讨论】:

    【解决方案2】:

    write(fd, msg.c_str(), strlen(msg.c_str())); - 您是否考虑过图像可能包含空(零)字节?因此将其视为 C 风格的字符串并不是一个好主意。

    您应该发送原始数据并写入该数据的大小 - 不是直到第一个空字节。

    【讨论】:

    • 此行此处未发送原始数据:bytes_written = write(fd, fileContents, fileLength); ?
    • vrsn 是一个字符串。它是 HTTP/1.1 字符串的 1.1 部分。
    • @MahmudAdam:你发送的 HTTP 响应都错了。看我的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 2019-08-15
    • 2013-12-17
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多