gpt4 book ai didi

C Web 服务器,图像未显示在浏览器上?

转载 作者:可可西里 更新时间:2023-11-01 17:26:43 25 4
gpt4 key购买 nike

目前正在尝试用 C 编写一个简单的 Web 服务器。在网上找到了一个很酷的片段。除了图像,一切都有效。我不明白为什么它没有出现在浏览器上。 sendfile() 返回 9109(图像的大小)。

我也对这段代码片段在 fork() 方面的作用感到困惑。为什么它需要 fork() 呢?以及为什么我们在可能有更多请求进入时关闭循环中的客户端?还是对于每个请求,都有一个新的连接?提前致谢!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sendfile.h>

char webpage[] =
"HTTP/1.1 200 Ok\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<!DOCTYPE html>\r\n"
"<html><head><title>Test Title</title>\r\n"
"<body>Hello World <img src='test.jpg'/></body>"
"</html>";

int main(int argc, char *argv[]){
struct sockaddr_in server_addr, client_addr;
socklen_t sin_len = sizeof(client_addr);
int fd_server, fd_client;
/* Storing the contents sent by the browser (a request) */
char buf[2048];
int fdimg;
int on = 1;

fd_server = socket(AF_INET, SOCK_STREAM, 0);
if(fd_server < 0){
perror("socket");
exit(1);
}

setsockopt(fd_server, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);

if(bind(fd_server, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1){
perror("bind");
close(fd_server);
exit(1);
}

if(listen(fd_server, 10) == -1){
perror("listen");
close(fd_server);
exit(1);
}

while(1){
fd_client = accept(fd_server, (struct sockaddr *) &client_addr, &sin_len);

if(fd_client == -1){
perror("Connection failed...\n");
continue;
}

printf("Got client connection...\n");

if(!fork()){

/* Child process */

/* Close this as the client no longer needs it */
close(fd_server);
memset(buf, 0, 2048);
read(fd_client, buf, 2047); /* 2047 because of null char? */

/* Print the request on the console */
printf("%s\n", buf);

if(!strncmp(buf, "GET /test.jpg", 13)){
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
fdimg = open("test.jpg", O_RDONLY);
int sent = sendfile(fd_client, fdimg, NULL, 10000);
printf("sent: %d", sent);
close(fdimg);
}
else{
write(fd_client, webpage, sizeof(webpage) - 1);
}

close(fd_client);
printf("closing connection...\n");
exit(0);
}

/* Parent process */
close(fd_client);


}


return 0;

}

最佳答案

这可能是由于浏览器只允许端口 80 上的 HTTP/0.9。

HTTP/0.9 和 HTTP/1.1 之间的主要区别在于服务器在数据之前发送 header 。

尝试将 HTTP/1.1 header 添加到图像响应中:

char imageheader[] = 
"HTTP/1.1 200 Ok\r\n"
"Content-Type: image/jpeg\r\n\r\n";

// ....

if (!strncmp(buf, "GET /test.jpg", 13)) {
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// Send header first: (Note, should loop in case of partial write or EINTR)
write(fd_client, imageheader, sizeof(imageheader) - 1);
// Send image content
fdimg = open("test.jpg", O_RDONLY);
int sent = sendfile(fd_client, fdimg, NULL, 10000);
printf("sent: %d", sent);
close(fdimg);
}

关于C Web 服务器,图像未显示在浏览器上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com