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

时间:2016-12-16 16:32:13

标签: c http webserver

因此我们被要求创建一个可以通过Web浏览器访问的简单HTTP Web服务器(例如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;
}

进入&#34; localhost:8080&#34;在浏览器中,它会打开一个消息标签,上面写着&#34;您已选择打开空白,这是来自http://localhost:8080&#34;的文字/ html;

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

1 个答案:

答案 0 :(得分:0)

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

获取一些信息

我尝试将网页设置为下方并显示页面(但由于我没有图像文件,因此显示为十字(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";

这个答案也可以帮助Sending a file over a TCP/IP socket (web server)

注意:在上面的代码中我硬编码了内容长度值,您应该根据您的html数据计算它。