2016-12-16 173 views
1

所以我們被要求創建一個簡單的HTTP網頁服務器,可以通過網頁瀏覽器訪問(例如localhost:8080)。C-通過網頁瀏覽器訪問HTTP網頁服務器

我試過這段代碼:

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

而且進入後:在瀏覽器中「本地主機8080」,它會打開,說:「您已選擇打開空白這是text/html的從郵件標籤http://localhost:8080

該程序工作所需的三個文件:foo.c,doctest.jpg和favicon.ico位於一個目錄(桌面)中。我不知道如何處理這個錯誤。幫助是非常需要和讚賞的。這是我第一次涉足網絡服務器代碼。謝謝!

回答

0

該問題似乎沒有代碼,但http響應和html內容通過套接字發送。您還需要設置Content-Length:字段Connection:關閉標籤。我沒有更深入的瞭解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)

注:在上面的代碼中,我硬編碼Content-Length值,你應該計算它按你的HTML數據。