C++ C++;发送图像,但似乎失败了

C++ C++;发送图像,但似乎失败了,c++,linux,image,http,C++,Linux,Image,Http,我正在实现一个Web服务器来向客户端发送图像。当客户端浏览器为html页面发送图像请求时,服务器打算将请求的图像发回,或者应该这样做。 这是我发送HTTP响应的代码。fd是源文件描述符,cfd是客户端套接字描述符,所有这些都很好,没有明显的错误 ::send(cfd, firstLine.c_str(), firstLine.length(),0); // HTTP/1.1 200 OK response.addHeaderDate(); // Date: xxxxxxxxx int fd

我正在实现一个Web服务器来向客户端发送图像。当客户端浏览器为html页面发送图像请求时,服务器打算将请求的图像发回,或者应该这样做。 这是我发送HTTP响应的代码。fd是源文件描述符,cfd是客户端套接字描述符,所有这些都很好,没有明显的错误

::send(cfd, firstLine.c_str(), firstLine.length(),0);  // HTTP/1.1 200 OK
response.addHeaderDate();  // Date: xxxxxxxxx
int fd = response.ffd;  // file descriptor
// measure file size
int file_size;
file_size = lseek(fd, 0, SEEK_END);
response.addHeaderLength(file_size);  // Content-Length: xxx
lseek(fd, 0, SEEK_SET);
// headers
for (auto &item : response.headers) {
    const std::string &key = item.first, &val = item.second;
    ::send(cfd, key.c_str(), key.length(), 0);
    ::send(cfd, ": ", 2, 0);
    ::send(cfd, val.c_str(), val.length(), 0);
    ::send(cfd, "\r\n", 2, 0);
}
::send(cfd, "\r\n", 2, 0);   // blank line after headers
char buf[4096];
while (true) {
    int len = read(fd, buf, 4096);   // read from target file
    if (len <= 0) {
        break;
    }
    write(cfd, buf, len);   // write to socket
}
close(fd);   // close file
这也可以在html文件的http包中找到。我相信它说明了问题的根源,因为消息没有显示在其他http包中,而我不太理解它。我猜是因为包完全传输时我没有发送“\0”。我试过了,但仍然不起作用。
我不熟悉服务器和网络编程。谢谢阅读,请帮忙。这真是个麻烦事。

write(cfd,buf,len);//如果以非阻塞模式发送大图片,则写入套接字可能不会返回
len
。检查
write
中的返回值。如果它小于
len
你需要从它停止的地方继续写。我不确定我是否明白你的意思。”len“”是从“write”返回的,它打算写入与每次迭代中从映像中读取的字节数相同的字节数。“
len
是从
write
返回的”-但您不注意这一点,所以您怎么知道?这样做会更好:
ssize\u t write=0;ssize_t电流;while(writeni)必须阅读更多关于非阻塞和异步以及所有这些的内容…这是响应头。日期属性字符串还有一个\n。。。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
testtest....
<img src="/assets/1.png">
</body>
</html>
[Expert Info (Note/Malformed): HTTP body subdissector failed, trying heuristic subdissector]
    [HTTP body subdissector failed, trying heuristic subdissector]
    [Severity level: Note]
    [Group: Malformed]