【问题标题】:Build a basic webserver建立一个基本的网络服务器
【发布时间】:2014-09-14 04:43:13
【问题描述】:

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

【问题讨论】:

    标签: android linux apache http webserver


    【解决方案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);
      }
    }
    

    【讨论】:

    • 好吧,我知道 C……但我以前从未做过任何网络。那么我从哪里开始呢?我不知道什么是 fd 或 epoll
    • 我的回答被否决了!我被冒犯了:P @yuxuantim 你在寻找高层次的理解或代码细节吗?如果您进行谷歌搜索,可以在任何地方找到示例应用程序。这是一个,paulgriffiths.net/program/c/webserv.php
    • 我会投票,但我没有代表。我正在寻找一个关于在 linux 上使用 C 进行联网的地方。希望有代码
    • @yuxuantim 如果您还没有,请务必尝试在 Google 上搜索“在 linux 上使用 C 进行网络”
    猜你喜欢
    • 1970-01-01
    • 2021-02-25
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多