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

时间:2017-08-14 08:28:50

标签: c sockets http web server

目前正在尝试使用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;

}

1 个答案:

答案 0 :(得分:1)

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

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

尝试在图像响应中添加HTTP / 1.1标头:

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);
}