手写精简版TinyHttpd项目(二)

在上一章中,我们基本完了socket通信的建立和消息的处理,那么这一章我们就来看如何生成一个静态的网页。

1.新建网页

        首先我们再代码的同级目录下新建一个index.html文件(当然也可以自己选择一个路径)然后将

如下的代码复制到新建的index.html中:

<!DOCTYPE html>
<html>
<body>
    <textarea id="textArea" rows="4" cols="50">
        This is a text area with a border.
    </textarea>
    <div id="dialogBox" title="Dialog Box">
        <form action="message.cgi" method="post">
            <input type="text" id="inputBox" placeholder="Enter your message here">
            <button type="submit">Submit Form</button>
        </form>
    </div>
</body>
</html>

结果如下:

        

2.显示静态网页。

        我们现在已经有了一个本地的网页,接下来我们就只需要在合适的时候将其显示再网页上计科。在我TinyHttpd精读中也提到了如何去显示一个静态网页--读取本地的文件,将其内容传递到客户端(网页)即可。思路主体就是这样,那么我们就来看我们的代码如何实现的:

void saver_file(int client_id){
    std::cout<<"saver_file\n";
    char buffer[1024] = {0};
    std::string file_name = "index.html";
    std::string path = "./";
    std::fstream file(path+file_name);
    std::string line = "";
    if(file.is_open()){
        std::string new_line = "";
        while(std::getline(file,new_line)){
            line = line + new_line;
            new_line = "";
        }
        send(client_id,line.c_str(),line.length(),0);
        file.close();
    }else{
        printf("con't open file maybe path is error\n");
        not_found(client_id);
    }
    //close(client_id);
    return ;
}

2.1not_found函数

        这个函数主要就是在网页上显示not_found的信息。可以直接照抄TinyHttpd的代码:

void not_found(int client)
{
 char buf[1024];
 sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");
 send(client, buf, strlen(buf), 0);
 sprintf(buf, SERVER_STRING);
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "Content-Type: text/html\r\n");
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "\r\n");
 send(client, buf, strlen(buf), 0);
  //以下开始是body
 sprintf(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "<BODY><P>The server could not fulfill\r\n");
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "your request because the resource specified\r\n");
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "is unavailable or nonexistent.\r\n");
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "</BODY></HTML>\r\n");
 send(client, buf, strlen(buf), 0);
}

2.2head函数

        那么现在至此,我们还差最后一步就能显示一个静态网页了,那就是上一章我们代码中写到但是没有着重提到的响应头文件(headers函数),没有这个文件我们是无法显示静态网页的,所以这个文件比较重要,具体代码如下:

void headers(int client)
{
 char buf[1024];

 strcpy(buf, "HTTP/1.0 200 OK\r\n");
 send(client, buf, strlen(buf), 0);
 strcpy(buf, SERVER_STRING);
 send(client, buf, strlen(buf), 0);
 sprintf(buf, "Content-Type: text/html\r\n");
 send(client, buf, strlen(buf), 0);
 strcpy(buf, "\r\n");
  //以下开始是body
 send(client, buf, strlen(buf), 0);
}

 那么至此,把我们上述中的代码都写完后,我们就能显示静态网页了。(执行make,./service 然后网页输入localhost:8008)

我们将在下一章去实现一个动态网页。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-19 04:36:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-19 04:36:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-19 04:36:03       82 阅读
  4. Python语言-面向对象

    2024-06-19 04:36:03       91 阅读

热门阅读

  1. 超参数调优-通用深度学习篇(上)

    2024-06-19 04:36:03       51 阅读
  2. 扩展动态数据统计

    2024-06-19 04:36:03       32 阅读
  3. vlcplayer for android 源码编译log打印

    2024-06-19 04:36:03       29 阅读
  4. RSS Channel 元素

    2024-06-19 04:36:03       76 阅读
  5. 事务&AOP

    2024-06-19 04:36:03       30 阅读
  6. 算法刷题笔记 二进制中1的个数(C++实现)

    2024-06-19 04:36:03       35 阅读
  7. 代码随想三刷二叉树篇2

    2024-06-19 04:36:03       37 阅读
  8. QT day4

    QT day4

    2024-06-19 04:36:03      37 阅读
  9. 浅封装BeanUtils,优雅实现List数据copy拷贝

    2024-06-19 04:36:03       37 阅读