构建一个基本的Web服务器

时间:2014-09-14 04:43:14

标签: android linux apache http webserver

我想知道如何制作Apache。我想要的是制作一个允许我的计算机托管1个文件的程序(或脚本)。除教育外,基本没有意义。当有人在端口8080上访问我的IP时,它会向他们显示index.html

1 个答案:

答案 0 :(得分:1)

这是我对服务器应用程序基本结构的看法。

/* pseudocode */

listen_fd = socket(host, port);

// fork or create thread to listen for connections
{
  while (1) {
    if (0 < listen(listen_fd)) {
      fd = accept(listen_fd);
      // add fd to a data structure you can manage. I recommend epoll.
    }
  }
}

// worker process or thread
{
  while (1) {
    // wait for events on an fd. then read data into a buffer
    n = read(fd, &buf, buf_size);
    // parse request however you need to... this is a lot of the art of a web server I think.
    // load and serve content
    response_len = alloc_and_fread("index.html", &response, response_max_size);
    send(fd, response, response_len);
  }
}