C- 通过网络浏览器访问的 HTTP 网络服务器

C- HTTP web server accessed through web browsers

所以我们被要求创建一个简单的 HTTP 网络服务器,可以通过网络浏览器访问(例如 localhost:8080)。

我试过这段代码:

#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/sendfile.h>
#include <sys/stat.h>
#include <fcntl.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>MP2</title>\r\n"
        "<stle>body (background-color: #FFFF00) </style></head>\r\n"
        "<body><center><h1> Hello World! </h1><br>\r\n"
        "<img src=\"doctest.jpg\"></center></body></html>\r\n";

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;
    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())
        {
            close(fd_server);
            memset(buf, 0, 2048);
            read(fd_client, buf, 2047);

            printf("%s\n",buf);
            if (!strncmp(buf, "GET /favicon.ico", 16))
            {
                fdimg = open("favicon.ico", O_RDONLY);
                sendfile(fd_client, fdimg, NULL, 4000);
                close(fdimg);
            }
            else if (!strncmp(buf, "GET /doctest.jpg", 16))
            {
                fdimg = open("doctest.jpg", O_RDONLY);
                sendfile(fd_client, fdimg, NULL, 6000);
                close(fdimg);
            }
            else
                write(fd_client, webpage, sizeof(webpage) - 1);
            close(fd_client);
            printf("closing....\n");
            exit(0);
        }
        close(fd_client);
    }
    return 0;
}

在浏览器中输入 "localhost:8080" 后,它会打开一个显示 "You have chosen to open blank which is text/html from http://localhost:8080"

的消息选项卡

此程序运行所需的三个文件:foo.c、doctest.jpg 和 favicon.ico 在一个目录(桌面)中。我不知道如何处理这个错误。非常需要和感谢帮助。这是我第一次涉足 Web 服务器代码。谢谢!

代码似乎没有问题,但 http 响应和 html 内容通过套接字发送。您还需要设置 Content-Length: 字段 Connection: close 标记。我对 HTTP 协议没有更深入的了解,但您可以在此处获取一些信息 https://www.tutorialspoint.com/http/http_responses.htm

我尝试将网页设置为下面并显示该页面(但是由于我没有图像文件,它显示为 cross(x) )

char webpage[] = 
        "HTTP/1.1 200 OK\r\n"
        "Content-length: 194\r\n"
        "Content-Type: text/html\r\n"
        "Connection: close\r\n\r\n"
        "<!DOCTYPE html>\r\n"
        "<html><head><title>MP2</title>\r\n"
        "<stle>body (background-color: #FFFF00) </style></head>\r\n"
        "<body><center><h1> Hello World! </h1><br>\r\n"
        "<img src=\"doctest.jpg\"></center></body></html>\r\n";

这个答案也可能有帮助

注意:在上面的代码中,我硬编码了 Content-length 值,您应该根据您的 html 数据计算它。