{"id":37862,"date":"2025-05-18T07:26:04","date_gmt":"2025-05-17T23:26:04","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/37862.html"},"modified":"2025-05-18T07:26:04","modified_gmt":"2025-05-17T23:26:04","slug":"tingwebserver%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%bb%a3%e7%a0%81%e8%a7%a3%e8%af%bb01","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/37862.html","title":{"rendered":"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01"},"content":{"rendered":"<h3>\u672c\u89e3\u8bfb\u6587\u7ae0\u5bf9\u5b8c\u6574\u4ee3\u7801\u8fdb\u884c\u89e3\u8bfb&#xff0c;\u4ecemain.cpp\u7740\u624b\u4e3b\u770b\u4ee3\u7801&#xff0c;\u7136\u540e\u4e00\u6b65\u6b65\u5256\u89e3\u4ee3\u7801\u6587\u4ef6&#xff0c;\u4f46\u4e0d\u4f1a\u63d2\u5165\u6240\u6709\u4ee3\u7801&#xff0c;\u6240\u4ee5\u9700\u8981\u642d\u914d\u6253\u5f00TingWebServer\u7684\u6e90\u4ee3\u7801\u4e00\u8d77\u5b66\u4e60<\/h3>\n<p>\u6e90\u4ee3\u7801\u5730\u5740&#xff1a;TinyWebServer\/webserver.cpp at master \u00b7 qinguoyi\/TinyWebServer<\/p>\n<p>\u4f5c\u8005\u7684\u539f\u89e3\u8bfb&#xff1a;\u6700\u65b0\u7248Web\u670d\u52a1\u5668\u9879\u76ee\u8be6\u89e3 &#8211; 01 \u7ebf\u7a0b\u540c\u6b65\u673a\u5236\u5c01\u88c5\u7c7b<\/p>\n<p>Tingwebserver \u7684\u914d\u7f6e\u4e0e\u4f7f\u7528&#xff1a;c&#043;&#043; \u7ecf\u5178\u670d\u52a1\u5668\u5f00\u6e90\u9879\u76ee Tinywebserver\u7684\u4f7f\u7528\u4e0e\u914d\u7f6e(\u767e\u5ea6\u667a\u80fd\u4e91\u670d\u52a1\u5668\u5b89\u88c5ubuntu18.04\u53ef\u7528\u516c\u7f51ip\u8bbf\u95ee)-CSDN\u535a\u5ba2<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"958\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517232602-68291b0aec4e5.png\" width=\"1200\" \/><\/h3>\n<p>\u6211\u4eec\u5c06\u6309\u7167\u8fd9\u4e2a\u5173\u7cfb\u56fe\u9010\u7ea7\u89e3\u8bfb\u4ee3\u7801<\/p>\n<h3>\u5148\u770b\u4e3b\u4ee3\u7801&#xff1a;main.cpp<\/h3>\n<p>#include &#034;config.h&#034;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    \/\/\u9700\u8981\u4fee\u6539\u7684\u6570\u636e\u5e93\u4fe1\u606f,\u767b\u5f55\u540d,\u5bc6\u7801,\u5e93\u540d<br \/>\n    string user &#061; &#034;root&#034;;<br \/>\n    string passwd &#061; &#034;root&#034;;<br \/>\n    string databasename &#061; &#034;qgydb&#034;;<\/p>\n<p>    \/\/\u547d\u4ee4\u884c\u89e3\u6790<br \/>\n    Config config;<br \/>\n    config.parse_arg(argc, argv);<\/p>\n<p>    WebServer server;<\/p>\n<p>    \/\/\u521d\u59cb\u5316<br \/>\n    server.init(config.PORT, user, passwd, databasename, config.LOGWrite,<br \/>\n                config.OPT_LINGER, config.TRIGMode,  config.sql_num,  config.thread_num,<br \/>\n                config.close_log, config.actor_model);<\/p>\n<p>    \/\/\u65e5\u5fd7<br \/>\n    server.log_write();<\/p>\n<p>    \/\/\u6570\u636e\u5e93<br \/>\n    server.sql_pool();<\/p>\n<p>    \/\/\u7ebf\u7a0b\u6c60<br \/>\n    server.thread_pool();<\/p>\n<p>    \/\/\u89e6\u53d1\u6a21\u5f0f<br \/>\n    server.trig_mode();<\/p>\n<p>    \/\/\u76d1\u542c<br \/>\n    server.eventListen();<\/p>\n<p>    \/\/\u8fd0\u884c<br \/>\n    server.eventLoop();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>\u00a0\u8fd9\u4e09\u884c\u662f\u5728\u7a0b\u5e8f\u7f16\u8bd1\u8fd0\u884c\u524d\u5c31\u9700\u8981\u4fee\u6539\u7684&#xff0c;\u786e\u4fdduser&#xff0c;password\u548cdatabase\u662f\u81ea\u5df1\u7684\u6570\u636e\u5e93\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>string user &#061; &#034;root&#034;;<br \/>\n    string passwd &#061; &#034;root&#034;;<br \/>\n    string databasename &#061; &#034;qgydb&#034;; <\/p>\n<p>\u00a0\u8fd9\u662fconfig\u89e3\u6790\u547d\u4ee4\u884c\u7684\u8fd0\u884c\u4ee3\u7801\u7684&#xff0c;\u4e00\u904d\u7684shell\u811a\u672c\u4ee3\u7801\u683c\u5f0f\u4e3a&#xff1a;<\/p>\n<p>.\/server [-p port] [-l LOGWrite] [-m TRIGMode] [-o OPT_LINGER] [-s sql_num] [-t thread_num] [-c close_log] [-a actor_model]<\/p>\n<p>\u800cconfig\u662f\u89e3\u6790\u540e\u9762\u7684-p,-l\u4e4b\u7c7b\u7684\u6307\u5b9a\u53c2\u6570&#xff0c;\u4ece\u800c\u5b9e\u73b0\u4e2a\u6027\u5316\u8fd0\u884c\u670d\u52a1\u5668\u00a0<\/p>\n<p>\/\/\u547d\u4ee4\u884c\u89e3\u6790<br \/>\n    Config config;<br \/>\n    config.parse_arg(argc, argv); <\/p>\n<p>\u540e\u9762\u5219\u662fwebserver\u7684\u521d\u59cb\u5316&#xff0c;\u4f20\u5165\u53c2\u6570\u662fconfig\u7684\u89e3\u6790\u547d\u4ee4&#xff0c;\u4e4b\u540e\u4fbf\u662f\u670d\u52a1\u5668\u542f\u52a8\u7684\u89c4\u8303\u6807\u51c6\u6d41\u7a0b&#xff1a;\u65e5\u5fd7-&gt;\u6570\u636e\u5e93-&gt;\u7ebf\u7a0b\u6c60-&gt; \u89e6\u53d1\u6a21\u5f0f-&gt; \u4e8b\u4ef6\u76d1\u542c-&gt; \u4e8b\u4ef6\u5faa\u73af,\u670d\u52a1\u5668\u5f00\u59cb\u8fd0\u884c<\/p>\n<li>\u8d44\u6e90\u521d\u59cb\u5316\u987a\u5e8f&#xff1a;\n<ul>\n<li>\u670d\u52a1\u5668\u542f\u52a8\u65f6&#xff0c;\u9996\u5148\u9700\u8981\u786e\u4fdd\u65e5\u5fd7\u7cfb\u7edf\u53ef\u4ee5\u8bb0\u5f55\u540e\u7eed\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>\u7136\u540e\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u6c60&#xff0c;\u786e\u4fdd\u540e\u7eed\u64cd\u4f5c&#xff08;\u5982\u8bf7\u6c42\u5904\u7406&#xff09;\u80fd\u591f\u987a\u5229\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\u521d\u59cb\u5316\u7ebf\u7a0b\u6c60&#xff0c;\u4ee5\u786e\u4fdd\u670d\u52a1\u5668\u80fd\u591f\u5e76\u53d1\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/li>\n<li>\u968f\u540e\u914d\u7f6e\u89e6\u53d1\u6a21\u5f0f&#xff0c;\u786e\u4fdd\u4e8b\u4ef6\u5904\u7406\u65b9\u5f0f\u7b26\u5408\u9700\u6c42\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u9010\u6b65\u521d\u59cb\u5316\u4f9d\u8d56\u5173\u7cfb&#xff1a;\n<ul>\n<li>\u4e8b\u4ef6\u76d1\u542c\u662f\u670d\u52a1\u5668\u5f00\u59cb\u63a5\u6536\u8bf7\u6c42\u7684\u6b65\u9aa4&#xff0c;\u5b83\u4f9d\u8d56\u4e8e\u524d\u9762\u6240\u6709\u8d44\u6e90\u7684\u914d\u7f6e&#xff0c;\u5305\u62ec\u65e5\u5fd7\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001\u7ebf\u7a0b\u6c60\u548c\u89e6\u53d1\u6a21\u5f0f\u3002<\/li>\n<li>\u4e8b\u4ef6\u5faa\u73af\u662f\u670d\u52a1\u5668\u7684\u4e3b\u5faa\u73af&#xff0c;\u5b83\u4f9d\u8d56\u4e8e\u6240\u6709\u521d\u59cb\u5316\u8fc7\u7a0b&#xff08;\u5982\u76d1\u542c\u7aef\u53e3\u3001\u7ebf\u7a0b\u6c60\u7b49&#xff09;\u5b8c\u6210\u624d\u80fd\u542f\u52a8\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u8fd9\u4e2a\u987a\u5e8f\u786e\u4fdd\u4e86\u6bcf\u4e2a\u7cfb\u7edf\u7ec4\u4ef6\u7684\u521d\u59cb\u5316\u90fd\u5728\u5176\u4f9d\u8d56\u7684\u7ec4\u4ef6\u4e4b\u524d\u8fdb\u884c&#xff0c;\u4ece\u800c\u907f\u514d\u51fa\u73b0\u8d44\u6e90\u4e0d\u53ef\u7528\u6216\u914d\u7f6e\u9519\u8bef\u7684\u95ee\u9898\u3002<\/p>\n<p>\u56e0\u4e3amain.cpp\u4e2d\u53ea\u662finclude\u4e86config .h\u8fd9\u4e2a\u5934\u6587\u4ef6&#xff0c;\u6211\u4eec\u5c31\u8ddf\u7740\u5f15\u7528\u5934\u6587\u4ef6\u5165\u624b\u00a0<\/p>\n<h3>\u00a0\u547d\u4ee4\u884c\u89e3\u6790config.h&#043;config.cpp<\/h3>\n<p>config.h\u975e\u5e38\u7b80\u5355\u5e76\u4e14\u53c2\u6570\u90fd\u7ed9\u4e86\u6ce8\u89e3&#xff0c;\u63d0\u4e00\u5634\u5b8f\u5b9a\u4e49\u5934\u6587\u4ef6&#xff0c;\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u7f16\u8bd1\u62a5\u9519&#xff0c;\u6240\u4ee5\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f<\/p>\n<p>\/\/ config.h #ifndef CONFIG_H #define CONFIG_H<\/p>\n<p>\/\/ \u5934\u6587\u4ef6\u7684\u5185\u5bb9<\/p>\n<p>#endif \/\/ CONFIG_H \u00a0<\/p>\n<p>\u73b0\u4ee3\u7f16\u8bd1\u5668\u901a\u5e38\u8fd8\u652f\u6301\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u907f\u514d\u5934\u6587\u4ef6\u591a\u6b21\u5305\u542b&#xff0c;\u90a3\u5c31\u662f\u4f7f\u7528 #pragma once\u3002\u5b83\u7684\u6548\u679c\u4e0e #ifndef \/ #define \u914d\u5408\u4f7f\u7528\u7684\u5934\u6587\u4ef6\u4fdd\u62a4\u673a\u5236\u76f8\u540c&#xff0c;\u4f46\u5199\u6cd5\u66f4\u7b80\u6d01&#xff0c;\u4e14\u4e0d\u5bb9\u6613\u51fa\u9519<\/p>\n<p>\u5728config.cpp\u4e2d&#xff0c;\u6784\u9020\u51fd\u6570Config&#xff08;&#xff09;\u5b9a\u4e49\u4e86\u9ed8\u8ba4\u53c2\u6570&#xff0c;\u670d\u52a1\u5668\u7684\u521d\u59cb\u5b9a\u4e49\u72b6\u6001&#xff0c;\u9ed8\u8ba4\u7aef\u53e3\u53f7\u4e3a9006\u3002<\/p>\n<p>void Config::parse_arg(int argc, char*argv[])<br \/>\n{<br \/>\n  int opt;<br \/>\n    const char *str &#061; &#034;p:l:m:o:s:t:c:a:&#034;;<br \/>\n    while ((opt &#061; getopt(argc, argv, str)) !&#061; -1)<br \/>\n    {<br \/>\n      switch (opt)<br \/>\n        {<br \/>\n        case &#039;p&#039;:<br \/>\n        {<br \/>\n            PORT &#061; atoi(optarg);<br \/>\n            break;<br \/>\n        }<br \/>\n        &#8230;<br \/>\n        default:<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4f7f\u7528\u89e3\u6790\u547d\u4ee4\u884c\u51fd\u6570getopt&#xff08;&#xff09;&#xff0c;\u8fd4\u56de\u7684opt\u6307\u5411\u5408\u6cd5\u547d\u4ee4\u884c\u9009\u9879&#xff0c;\u4f8b\u5982-p \u8fd4\u56de &#039;p&#039;&#xff0c;\u800coptarg \u662f getopt \u89e3\u6790\u5230\u7684\u5f53\u524d\u9009\u9879\u7684\u53c2\u6570\u503c\u3002\u5b83\u662f\u4e00\u4e2a char* \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf&#xff0c;\u7528\u4e8e\u5b58\u50a8\u5f53\u524d\u9009\u9879\u5bf9\u5e94\u7684\u53c2\u6570\u3002<\/p>\n<p>getopt \u662f\u4e00\u4e2a\u6807\u51c6\u5e93\u51fd\u6570&#xff0c;\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5b83\u901a\u8fc7\u4e00\u4e2a\u9009\u9879\u5b57\u7b26\u4e32&#xff08;\u6bd4\u5982 str&#xff09;\u5b9a\u4e49\u7a0b\u5e8f\u652f\u6301\u7684\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u5e76\u81ea\u52a8\u5904\u7406\u53c2\u6570\u89e3\u6790\u3002<\/p>\n<ul>\n<li>\u53c2\u6570\u8bf4\u660e&#xff1a;\n<ul>\n<li>argc: \u53c2\u6570\u7684\u6570\u91cf&#xff08;\u6765\u81ea\u547d\u4ee4\u884c&#xff09;\u3002<\/li>\n<li>argv: \u53c2\u6570\u7684\u503c&#xff08;\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u7684\u5177\u4f53\u5185\u5bb9&#xff09;\u3002<\/li>\n<li>str: \u4e00\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u6307\u5b9a\u7a0b\u5e8f\u652f\u6301\u7684\u547d\u4ee4\u884c\u9009\u9879\u3002\u6bcf\u4e2a\u5b57\u7b26\u4ee3\u8868\u4e00\u4e2a\u9009\u9879&#xff0c;\u5b57\u7b26\u540e\u9762\u53ef\u80fd\u8ddf\u7740\u4e00\u4e2a\u5192\u53f7&#xff08;:&#xff09;&#xff0c;\u8868\u793a\u8be5\u9009\u9879\u540e\u9762\u9700\u8981\u4e00\u4e2a\u53c2\u6570<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u4e3b\u4f53\u6587\u4ef6&#xff1a;webserver.h&#043;webserver.cpp<\/h2>\n<h3>\u00a0webserver .h<\/h3>\n<p>\u56e0\u4e3a\u662f\u4e3b\u4f53\u6587\u4ef6&#xff0c;include\u4e86\u5f88\u591a\u9700\u8981\u4f7f\u7528\u5230\u7684\u5934\u6587\u4ef6\u548c\u5e93&#xff0c;\u9664\u4e86main\u4e2d\u7528\u5230\u7684\u8fd0\u884c\u51fd\u6570\u548c\u521d\u59cb\u5316\u4e4b\u5916&#xff0c;public\u91cc\u8fd8\u6709\u5b9a\u65f6\u5668\u51fd\u6570\u548cepoll\u7684\u76f8\u5173\u914d\u7f6e<\/p>\n<p>void timer(int connfd, struct sockaddr_in client_address); \/\/ \u521b\u5efa\u5b9a\u65f6\u5668<br \/>\n    void adjust_timer(util_timer *timer); \/\/ \u8c03\u6574\u5b9a\u65f6\u5668<br \/>\n    void deal_timer(util_timer *timer, int sockfd); \/\/ \u5904\u7406\u5b9a\u65f6\u5668<br \/>\nbool dealclientdata();        \/\/ \u5904\u7406\u5ba2\u6237\u7aef\u6570\u636e<\/p>\n<p>\/\/ \u5b9a\u65f6\u5668\u914d\u7f6e<br \/>\n    client_data *users_timer;      \/\/ \u5b9a\u65f6\u5668\u76f8\u5173\u6570\u636e\u7ed3\u6784&#xff0c;\u7ba1\u7406\u8fde\u63a5\u7684\u8d85\u65f6<br \/>\n    Utils utils;                   \/\/ \u5de5\u5177\u7c7b&#xff0c;\u7528\u4e8e\u5904\u7406\u5b9a\u65f6\u5668\u7b49<br \/>\n\/\/\u5b9a\u4e49\u5728timer\u7684lst_timer\u4e2d<br \/>\n\/\/epoll\u4e8b\u4ef6<br \/>\n bool dealwithsignal(bool&amp; timeout, bool&amp; stop_server); \/\/ \u5904\u7406\u4fe1\u53f7<br \/>\n    void dealwithread(int sockfd); \/\/ \u5904\u7406\u8bfb\u64cd\u4f5c<br \/>\n    void dealwithwrite(int sockfd); \/\/ \u5904\u7406\u5199\u64cd\u4f5c<\/p>\n<p>  epoll_event events[MAX_EVENT_NUMBER]; \/\/ \u5b58\u653eepoll\u4e8b\u4ef6\u7684\u6570\u7ec4<\/p>\n<p>    int m_listenfd;                \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    int m_OPT_LINGER;              \/\/ SO_LINGER\u8bbe\u7f6e\u6807\u5fd7&#xff0c;\u662f\u5426\u5ef6\u8fdf\u5173\u95ed\u5957\u63a5\u5b57<br \/>\n    int m_TRIGMode;                \/\/ \u603b\u7684\u89e6\u53d1\u6a21\u5f0f&#xff08;\u6c34\u5e73\u89e6\u53d1\/\u8fb9\u7f18\u89e6\u53d1&#xff09;<br \/>\n    int m_LISTENTrigmode;          \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7684\u89e6\u53d1\u6a21\u5f0f&#xff08;\u6c34\u5e73\u89e6\u53d1\/\u8fb9\u7f18\u89e6\u53d1&#xff09;<br \/>\n    int m_CONNTrigmode;            \/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u89e6\u53d1\u6a21\u5f0f&#xff08;\u6c34\u5e73\u89e6\u53d1\/\u8fb9\u7f18\u89e6\u53d1&#xff09;<\/p>\n<h3>webserver .cpp<\/h3>\n<h4>WebServer::WebServer()<\/h4>\n<p>\u6784\u9020\u51fd\u6570&#xff1a;\u00a0<\/p>\n<p>\/\/http_conn\u7c7b\u5bf9\u8c61<br \/>\n    users &#061; new http_conn[MAX_FD]; <\/p>\n<p>\u00a0http_conn \u7c7b\u662f Web \u670d\u52a1\u5668\u9879\u76ee\u4e2d\u4e0e\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u76f8\u5173\u7684\u6838\u5fc3\u7c7b&#xff0c;\u627f\u62c5\u7740\u7ba1\u7406\u8fde\u63a5\u3001\u5904\u7406 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u3001\u4ee5\u53ca\u4e0e\u4e8b\u4ef6\u9a71\u52a8\u673a\u5236&#xff08;\u5982 epoll&#xff09;\u534f\u540c\u5de5\u4f5c\u7b49\u4efb\u52a1\u3002\u901a\u8fc7\u8be5\u7c7b&#xff0c;\u670d\u52a1\u5668\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u548c I\/O \u64cd\u4f5c\u3002<\/p>\n<p>\/\/root\u6587\u4ef6\u5939\u8def\u5f84<br \/>\n    char server_path[200];<br \/>\n    getcwd(server_path, 200);<br \/>\n    char root[6] &#061; &#034;\/root&#034;;<br \/>\n    m_root &#061; (char *)malloc(strlen(server_path) &#043; strlen(root) &#043; 1);<br \/>\n    strcpy(m_root, server_path);<br \/>\n    strcat(m_root, root); <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u76ee\u7684\u662f\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u52a8\u6001\u6784\u5efa\u4e00\u4e2a\u8def\u5f84&#xff0c;\u8be5\u8def\u5f84\u662f\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84&#xff08;\u901a\u8fc7 getcwd \u83b7\u53d6&#xff09;\u52a0\u4e0a\u4e00\u4e2a\u56fa\u5b9a\u7684 \/root \u8def\u5f84\u3002\u4f8b\u5982&#xff0c;\u5982\u679c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662f \/home\/user\/project&#xff0c;\u90a3\u4e48\u6784\u5efa\u51fa\u6765\u7684\u8def\u5f84\u4f1a\u662f \/home\/user\/project\/root\u3002<\/p>\n<ul>\n<li>getcwd \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/li>\n<li>strcat \u548c strcpy \u62fc\u63a5\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u8def\u5f84&#xff0c;\u5b58\u50a8\u5728 m_root \u4e2d\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">WebServer::~WebServer()\u00a0<\/span> \u00a0 \u00a0 \u00a0<\/p>\n<p>\u6790\u6784\u51fd\u6570&#xff0c;\u5173\u95edepoll,listen,\u548c\u7ba1\u9053pipe&#xff0c;delete\u91ca\u653eusers http_conn\u548cusers_timer\u5b9a\u65f6\u5668\u548c\u6c60\u7a7a\u95f4m_pool<\/p>\n<h4><span style=\"color:#0d0016\">WebServer::init()<\/span><\/h4>\n<p>main&#xff08;&#xff09;\u4e2d\u7684\u521d\u59cb\u5316server\u5bf9\u8c61<\/p>\n<p>void WebServer::init(int port, string user, string passWord, string databaseName, int log_write,<br \/>\n                     int opt_linger, int trigmode, int sql_num, int thread_num, int close_log, int actor_model)<br \/>\n{<br \/>\n    m_port &#061; port;<br \/>\n    m_user &#061; user;<br \/>\n    m_passWord &#061; passWord;<br \/>\n    m_databaseName &#061; databaseName;<br \/>\n    m_sql_num &#061; sql_num;<br \/>\n    m_thread_num &#061; thread_num;<br \/>\n    m_log_write &#061; log_write;<br \/>\n    m_OPT_LINGER &#061; opt_linger;\/\/\u4f18\u96c5\u5173\u95ed\u8fde\u63a5<br \/>\n    m_TRIGMode &#061; trigmode;\/\/\u89e6\u53d1\u6a21\u5f0f<br \/>\n    m_close_log &#061; close_log;\/\/\u662f\u5426\u5173\u95ed\u65e5\u5fd7<br \/>\n    m_actormodel &#061; actor_model;\/\/\u5e76\u53d1\u6a21\u578b\u9009\u62e9<br \/>\n} <\/p>\n<h4><span style=\"color:#0d0016\">trig_mode()<\/span><\/h4>\n<p>\u6839\u636e\u4f20\u5165\u7684m_TRIGMode\u503c\u6765\u8bbe\u5b9a\u670d\u52a1\u5668\u8fd0\u884c\u7684\u6a21\u5f0f&#xff0c;\u9ed8\u8ba4&#xff1a;LT&#043;LT&#xff0c;\u901a\u8fc7\u6539\u53d8\u00a0m_LISTENTrigmode \u548cm_CONNTrigmode \u7684\u503c\u6765\u63a7\u5236\u3002<\/p>\n<p>void WebServer::trig_mode()<br \/>\n{<br \/>\n    \/\/LT &#043; LT<br \/>\n    if (0 &#061;&#061; m_TRIGMode)<br \/>\n    {<br \/>\n        m_LISTENTrigmode &#061; 0;<br \/>\n        m_CONNTrigmode &#061; 0;<br \/>\n    }<br \/>\n    \/\/LT &#043; ET<br \/>\n    else if (1 &#061;&#061; m_TRIGMode)<br \/>\n    {<br \/>\n        m_LISTENTrigmode &#061; 0;<br \/>\n        m_CONNTrigmode &#061; 1;<br \/>\n    }<br \/>\n    \/\/ET &#043; LT<br \/>\n    else if (2 &#061;&#061; m_TRIGMode)<br \/>\n    {<br \/>\n        m_LISTENTrigmode &#061; 1;<br \/>\n        m_CONNTrigmode &#061; 0;<br \/>\n    }<br \/>\n    \/\/ET &#043; ET<br \/>\n    else if (3 &#061;&#061; m_TRIGMode)<br \/>\n    {<br \/>\n        m_LISTENTrigmode &#061; 1;<br \/>\n        m_CONNTrigmode &#061; 1;<br \/>\n    }<br \/>\n} <\/p>\n<p>\u65e5\u5fd7\u51fd\u6570\u00a0<\/p>\n<h4>log_write()\u00a0<\/h4>\n<p>void WebServer::log_write()<br \/>\n{<br \/>\n    if (0 &#061;&#061; m_close_log) \/\/\u65e5\u5fd7\u6ca1\u6709\u88ab\u5173\u95ed<br \/>\n    {<br \/>\n        \/\/\u521d\u59cb\u5316\u65e5\u5fd7<br \/>\n        if (1 &#061;&#061; m_log_write)  \/\/\u65e5\u5fd7\u53ef\u5199<br \/>\n            Log::get_instance()-&gt;init(&#034;.\/ServerLog&#034;, m_close_log, 2000, 800000, 800);<br \/>\n        else   \/\/\u65e5\u5fd7\u4e0d\u53ef\u5199&#xff0c;\u6700\u540e\u76840\u8868\u793a\u65e5\u5fd7\u7684\u6761\u76ee<br \/>\n            Log::get_instance()-&gt;init(&#034;.\/ServerLog&#034;, m_close_log, 2000, 800000, 0);<br \/>\n    }<br \/>\n} <\/p>\n<h4>sql_pool&#xff08;&#xff09;<\/h4>\n<p>\u6570\u636e\u5e93\u521d\u59cb\u5316\u51fd\u6570\u00a0<\/p>\n<p>void WebServer::sql_pool()<br \/>\n{<br \/>\n    \/\/\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u6c60<br \/>\n    m_connPool &#061; connection_pool::GetInstance();<br \/>\n    m_connPool-&gt;init(&#034;localhost&#034;, m_user, m_passWord, m_databaseName, 3306, m_sql_num, m_close_log);<\/p>\n<p>    \/\/\u521d\u59cb\u5316\u6570\u636e\u5e93\u8bfb\u53d6\u8868<br \/>\n    users-&gt;initmysql_result(m_connPool);<br \/>\n} <\/p>\n<h4>thread_pool&#xff08;&#xff09;<\/h4>\n<p>\u7ebf\u7a0b\u6c60\u521d\u59cb\u5316\u51fd\u6570\u00a0<\/p>\n<p>void WebServer::thread_pool()<br \/>\n{<br \/>\n    \/\/\u521d\u59cb\u5316\u7ebf\u7a0b\u6c60<br \/>\n    m_pool &#061; new threadpool&lt;http_conn&gt;(m_actormodel, m_connPool, m_thread_num);<br \/>\n} <\/p>\n<h4>eventLoop&#xff08;&#xff09;<\/h4>\n<p>Server\u7684\u903b\u8f91\u4e2d\u5fc3&#xff0c;loop\u51fd\u6570&#xff0c;\u5904\u7406<\/p>\n<ul>\n<li>\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5<\/li>\n<li>\u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u5173\u95ed\u6216\u9519\u8bef<\/li>\n<li>\u4fe1\u53f7\u4e8b\u4ef6<\/li>\n<li>\u5ba2\u6237\u7aef\u6570\u636e\u7684\u8bfb\u53d6\u548c\u54cd\u5e94\u6570\u636e\u7684\u5199\u5165<\/li>\n<li>\u5b9a\u65f6\u5668\u4efb\u52a1<\/li>\n<\/ul>\n<p>\u91cd\u70b9<\/p>\n<p>\u56e0\u4e3awebserver.cpp\u5728\u6784\u5efa\u7684\u65f6\u5019\u6d89\u53ca\u51fd\u6570\u95f4\u4f7f\u7528\u4f9d\u8d56\u7684\u95ee\u9898&#xff0c;eventLoop\u5199\u5728\u6e90\u4ee3\u7801\u7684\u6700\u4e0b\u9762&#xff0c;\u800c\u7b14\u8005\u628a\u672c\u51fd\u6570\u8c03\u5ea6\u5230\u6700\u4e0a\u9762\u8bb2\u89e3\u662f\u4ee5\u4e3aloop\u6d89\u53ca\u5927\u4f53\u6d41\u7a0b\u548c\u5176\u4ed6\u51fd\u6570&#xff0c;\u5728\u770bloop\u51fd\u6570\u7684\u540c\u65f6\u627e\u5230\u5bf9\u5e94\u7684\u4f7f\u7528\u51fd\u6570\u80fd\u66f4\u597d\u7406\u6e05\u601d\u8def\u7406\u89e3\u4ee3\u7801\u3002\u00a0<\/p>\n<\/p>\n<p>void WebServer::eventLoop()<br \/>\n{<br \/>\n    bool timeout &#061; false;<br \/>\n    bool stop_server &#061; false;\/\/\u6807\u8bb0\u662f\u5426\u6709\u5b9a\u65f6\u5668\u8d85\u65f6\u4e8b\u4ef6\u548c\u670d\u52a1\u5668\u662f\u5426\u505c\u6b62\u8fd0\u884c<\/p>\n<p>    while (!stop_server)\/\/\u5faa\u73af\u5904\u7406\u4e8b\u4ef6<br \/>\n    {<br \/>\n        \/\/\u963b\u585e\u6a21\u5f0f&#xff0c;\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f&#xff08;io\u590d\u7528&#xff09;<br \/>\n        int number &#061; epoll_wait(m_epollfd, events, MAX_EVENT_NUMBER, -1);<br \/>\n        if (number &lt; 0 &amp;&amp; errno !&#061; EINTR)\/\/\u51fa\u73b0\u9519\u8bef\u5904\u7406<br \/>\n        {<br \/>\n            LOG_ERROR(&#034;%s&#034;, &#034;epoll failure&#034;);\/\/\u8bb0\u5f55\u9519\u8bef\u5230\u65e5\u5fd7<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        for (int i &#061; 0; i &lt; number; i&#043;&#043;)\/\/\u5904\u7406\u4e0d\u540c\u7684\u4e8b\u4ef6<br \/>\n        {<br \/>\n            int sockfd &#061; events[i].data.fd;<\/p>\n<p>            \/\/\u5904\u7406\u65b0\u5230\u7684\u5ba2\u6237\u8fde\u63a5<br \/>\n            if (sockfd &#061;&#061; m_listenfd)<br \/>\n            {<br \/>\n                bool flag &#061; dealclientdata();\/\/\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n                if (false &#061;&#061; flag)<br \/>\n                    continue;<br \/>\n            }<br \/>\n            else if (events[i].events &amp; (EPOLLRDHUP | EPOLLHUP | EPOLLERR))<br \/>\n            {    \/\/\u8868\u793a\u5173\u95ed&#xff0c;\u5f02\u5e38\u6216\u9519\u8bef<br \/>\n                \/\/\u670d\u52a1\u5668\u7aef\u5173\u95ed\u8fde\u63a5&#xff0c;\u79fb\u9664\u5bf9\u5e94\u7684\u5b9a\u65f6\u5668<br \/>\n                util_timer *timer &#061; users_timer[sockfd].timer;<br \/>\n                deal_timer(timer, sockfd);<br \/>\n            }<br \/>\n            \/\/\u5904\u7406\u4fe1\u53f7&#xff0c;\u5728listen\u4e2d\u521b\u5efa\u4e86pipe\u7ba1\u9053\u5e76\u6302\u4e0a\u76d1\u542c&#xff0c;\u8fd9\u91cc\u662f\u76d1\u542c\u5230\u7ba1\u9053\u53ef\u8bfb\u6570\u636e\u65f6\u7684\u5904\u7406<br \/>\n            else if ((sockfd &#061;&#061; m_pipefd[0]) &amp;&amp; (events[i].events &amp; EPOLLIN))<br \/>\n            {<br \/>\n                bool flag &#061; dealwithsignal(timeout, stop_server);\/\/\u5904\u7406\u4fe1\u53f7<br \/>\n                if (false &#061;&#061; flag)<br \/>\n                    LOG_ERROR(&#034;%s&#034;, &#034;dealclientdata failure&#034;);<br \/>\n            }<br \/>\n            \/\/\u5904\u7406\u5ba2\u6237\u8fde\u63a5\u4e0a\u63a5\u6536\u5230\u7684\u6570\u636e<br \/>\n            else if (events[i].events &amp; EPOLLIN)\/\/\u8bfb\u4e8b\u4ef6<br \/>\n            {<br \/>\n                dealwithread(sockfd);<br \/>\n            }<br \/>\n            else if (events[i].events &amp; EPOLLOUT)\/\/\u5199\u4e8b\u4ef6<br \/>\n            {<br \/>\n                dealwithwrite(sockfd);<br \/>\n            }<br \/>\n        }<br \/>\n        if (timeout)\/\/\u8ba1\u65f6\u5668\u5173\u95ed<br \/>\n        {<br \/>\n            utils.timer_handler();<\/p>\n<p>            LOG_INFO(&#034;%s&#034;, &#034;timer tick&#034;);\u65e5\u5fd7\u8bb0\u5f55<\/p>\n<p>            timeout &#061; false;<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<h4>eventListen&#xff08;&#xff09;<\/h4>\n<p>\u7f51\u7edc\u7f16\u7a0b\u6b65\u9aa4&#xff0c;\u8bbe\u7acb\u76d1\u542c\u5668&#xff0c;socket listen&#xff0c;\u5224\u65ad\u662f\u5426\u4f18\u96c5\u5173\u95ed\u8fde\u63a5&#xff0c;\u8bbe\u7f6e\u00a0setsockopt<\/p>\n<p>\u4f18\u96c5\u5173\u95ed\u9009\u9879&#xff1a;setsockopt(m_listenfd, SOL_SOCKET, SO_LINGER, &amp;tmp, sizeof(tmp));<\/p>\n<p><span style=\"color:#fe2c24\">setsockopt\u51fd\u6570<\/span>&#xff1a;int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);<\/p>\n<ul>\n<li>sockfd&#xff1a;\u8981\u8bbe\u7f6e\u9009\u9879\u7684\u5957\u63a5\u5b57\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>level&#xff1a;\u9009\u9879\u6240\u5728\u7684\u534f\u8bae\u5c42&#xff0c;\u901a\u5e38\u662f SOL_SOCKET&#xff08;\u5957\u63a5\u5b57\u5c42&#xff09;\u6216\u534f\u8bae\u5c42&#xff08;\u5982 IPPROTO_TCP&#xff09;\u3002<\/li>\n<li>optname&#xff1a;\u8981\u8bbe\u7f6e\u7684\u9009\u9879\u7684\u540d\u79f0&#xff0c;\u4f8b\u5982 SO_REUSEADDR&#xff0c;SO_LINGER&#xff0c;SO_RCVBUF \u7b49\u3002<\/li>\n<li>optval&#xff1a;\u6307\u5411\u5305\u542b\u9009\u9879\u503c\u7684\u7f13\u51b2\u533a\u7684\u6307\u9488\u3002<\/li>\n<li>optlen&#xff1a;\u9009\u9879\u503c\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">SO_LINGER<\/span>&#xff1a;\u8bbe\u7f6e\u5957\u63a5\u5b57\u5173\u95ed\u65f6\u7684\u5ef6\u8fdf\u65f6\u95f4\u3002<\/p>\n<p>SO_LINGER \u9009\u9879\u5b9a\u4e49\u4e86\u5728\u5957\u63a5\u5b57\u88ab\u5173\u95ed\u65f6&#xff0c;\u5e94\u8be5\u5982\u4f55\u5904\u7406\u5269\u4f59\u6570\u636e\u3002\u5176\u7ed3\u6784\u4f53\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>struct linger { int l_onoff; \/\/ \u662f\u5426\u542f\u7528\u5ef6\u8fdf\u5173\u95ed&#xff08;0\u4e3a\u4e0d\u542f\u7528&#xff0c;1\u4e3a\u542f\u7528&#xff09; <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int l_linger; \/\/ \u5ef6\u8fdf\u5173\u95ed\u7684\u65f6\u95f4&#xff08;\u5355\u4f4d\u4e3a\u79d2&#xff09; };<\/p>\n<\/p>\n<p>\u5f53 l_onoff \u8bbe\u7f6e\u4e3a 1 \u65f6&#xff0c;\u8868\u793a\u542f\u7528\u5ef6\u8fdf\u5173\u95ed&#xff0c;l_linger \u8bbe\u7f6e\u4e3a\u6307\u5b9a\u7684\u65f6\u95f4\u957f\u5ea6&#xff08;\u5355\u4f4d\u662f\u79d2&#xff09;\u3002\u5728\u6b64\u671f\u95f4&#xff0c;\u5957\u63a5\u5b57\u4e0d\u4f1a\u7acb\u5373\u5173\u95ed&#xff0c;\u800c\u662f\u7b49\u5f85\u6240\u6709\u672a\u53d1\u9001\u5b8c\u7684\u6570\u636e\u53d1\u9001\u5b8c\u6bd5\u540e\u518d\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p>\u5f53 l_onoff \u8bbe\u7f6e\u4e3a 0 \u65f6&#xff0c;\u8868\u793a\u7981\u7528\u5ef6\u8fdf\u5173\u95ed\u3002\u6b64\u65f6&#xff0c;\u8c03\u7528 close() \u51fd\u6570\u4f1a\u7acb\u5373\u5173\u95ed\u5957\u63a5\u5b57&#xff0c;\u5e76\u4e14\u4efb\u4f55\u672a\u53d1\u9001\u7684\u6570\u636e\u5c06\u4f1a\u88ab\u4e22\u5f03\u3002<\/p>\n<p>\u5229\u7528setsockopt\u5b8c\u6210\u4f18\u96c5\u5173\u95ed\u64cd\u4f5c<\/p>\n<p>\u6ce8&#xff1a;uilts\u7c7b\u4e3atimer\u6587\u4ef6\u5939lst_timer.cpp\u4e2d\u5b9a\u4e49\u7684\u7c7b\u6587\u4ef6&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u5c01\u88c5\u7684\u5de5\u5177\u51fd\u6570&#xff0c;webserver.cpp\u548c\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f1a\u5927\u91cf\u7528\u5230&#xff0c;\u770b\u5230\u65f6\u53ef\u4ee5\u53bblst_timer.cpp\u67e5\u9605\u5177\u4f53\u51fd\u6570\u5b9a\u4e49<\/p>\n<p>void WebServer::eventListen()<br \/>\n{<br \/>\n    \/\/\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6b65\u9aa4<br \/>\n    m_listenfd &#061; socket(PF_INET, SOCK_STREAM, 0);<br \/>\n    assert(m_listenfd &gt;&#061; 0);\/\/\u65ad\u8a00\u8bbe\u7f6e&#xff0c;\u5982\u679c\u5931\u8d25\u9000\u51fa<\/p>\n<p>    \/\/\u4f18\u96c5\u5173\u95ed\u8fde\u63a5<br \/>\n    if (0 &#061;&#061; m_OPT_LINGER)<br \/>\n    {<br \/>\n        struct linger tmp &#061; {0, 1};<br \/>\n        setsockopt(m_listenfd, SOL_SOCKET, SO_LINGER, &amp;tmp, sizeof(tmp));<br \/>\n    }<br \/>\n    else if (1 &#061;&#061; m_OPT_LINGER)<br \/>\n    {<br \/>\n        struct linger tmp &#061; {1, 1};<br \/>\n        setsockopt(m_listenfd, SOL_SOCKET, SO_LINGER, &amp;tmp, sizeof(tmp));<br \/>\n    }<\/p>\n<p>    int ret &#061; 0;<br \/>\n    struct sockaddr_in address;<br \/>\n    bzero(&amp;address, sizeof(address));<br \/>\n    address.sin_family &#061; AF_INET;<br \/>\n    address.sin_addr.s_addr &#061; htonl(INADDR_ANY);<br \/>\n    address.sin_port &#061; htons(m_port);<\/p>\n<p>    int flag &#061; 1;<br \/>\n    setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEADDR, &amp;flag, sizeof(flag));<br \/>\n    \/\/\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528<br \/>\n    \/\/SO_REUSEADDR&#xff1a;\u5141\u8bb8\u91cd\u7528\u672c\u5730\u5730\u5740\u3002\u5e38\u7528\u4e8e\u5141\u8bb8\u591a\u4e2a\u670d\u52a1\u5668\u8fdb\u7a0b\u7ed1\u5b9a\u5230\u540c\u4e00\u7aef\u53e3&#xff0c;\u6216\u8005\u5728\u7a0b\u5e8f\u5f02\u5e38\u9000\u51fa        \u540e\u5feb\u901f\u7ed1\u5b9a\u5230\u540c\u4e00\u7aef\u53e3\u3002<br \/>\n    ret &#061; bind(m_listenfd, (struct sockaddr *)&amp;address, sizeof(address));<br \/>\n    assert(ret &gt;&#061; 0);<br \/>\n    ret &#061; listen(m_listenfd, 5);<br \/>\n    assert(ret &gt;&#061; 0);<br \/>\n    \/\/\u521d\u59cb\u5316\u5b9a\u65f6\u5668<br \/>\n    utils.init(TIMESLOT);<\/p>\n<p>    \/\/epoll\u521b\u5efa\u5185\u6838\u4e8b\u4ef6\u8868<br \/>\n    epoll_event events[MAX_EVENT_NUMBER];\/\/\u521b\u5efaepoll\u6570\u7ec4<br \/>\n    m_epollfd &#061; epoll_create(5);\/\/\u521b\u5efaepoll\u5b9e\u4f8b\u7528\u4e8e\u76d1\u542c<br \/>\n    assert(m_epollfd !&#061; -1);<\/p>\n<p>    \/\/\u5c06m_listenfd\u6302\u5728\u76d1\u542cm_epollfd\u7684epoll\u5b9e\u4f8b\u4e2d&#xff0c;m_LISTENTrigmode\u4e3a\u89e6\u53d1\u6a21\u5f0f<br \/>\n    utils.addfd(m_epollfd, m_listenfd, false, m_LISTENTrigmode);<br \/>\n    \/\/m_epollfd\u8d4b\u503c\u7ed9http\u8bf7\u6c42\u7c7b<br \/>\n    http_conn::m_epollfd &#061; m_epollfd;<\/p>\n<p>    \/\/\u521b\u5efa\u4e86\u7ba1\u9053m_pipefd\u7528\u4e8e\u8fdb\u7a0b\u95f4\u901a\u4fe1<br \/>\n    ret &#061; socketpair(PF_UNIX, SOCK_STREAM, 0, m_pipefd);<br \/>\n    assert(ret !&#061; -1);<br \/>\n    \/\/\u8bbe\u7f6e\u5199\u64cd\u4f5c\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\n    utils.setnonblocking(m_pipefd[1]);<br \/>\n    \/\/\u76d1\u542c\u8bfb\u7aef\u53e3\u4e8b\u4ef6<br \/>\n    utils.addfd(m_epollfd, m_pipefd[0], false, 0);<br \/>\n    \/\/\u5ffd\u7565 SIGPIPE \u4fe1\u53f7&#xff0c;\u6709\u52a9\u4e8e\u907f\u514d\u8fdb\u7a0b\u56e0\u7ba1\u9053\u9519\u8bef\u800c\u610f\u5916\u9000\u51fa<br \/>\n    utils.addsig(SIGPIPE, SIG_IGN);<br \/>\n    \/\/\u8bbe\u7f6e SIGALRM \u4fe1\u53f7&#xff08;\u5b9a\u65f6\u5668\u4fe1\u53f7&#xff09;&#xff0c;\u5b9a\u65f6\u5668\u5230\u671f\u65f6\u53d1\u9001<br \/>\n    utils.addsig(SIGALRM, utils.sig_handler, false);<br \/>\n    \/\/\u8bbe\u7f6e SIGTERM \u4fe1\u53f7&#xff08;\u7ec8\u6b62\u4fe1\u53f7&#xff09;&#xff0c;\u5916\u90e8\u8bf7\u6c42\u7ec8\u6b62\u7a0b\u5e8f\u65f6\u53d1\u51fa<br \/>\n    utils.addsig(SIGTERM, utils.sig_handler, false);<br \/>\n    \/\/\u4fe1\u53f7\u8bbe\u5b9a\u540e\u89e6\u53d1\u8c03\u7528handler\u51fd\u6570&#xff0c;send\u8fdb\u7ba1\u9053\u7684\u5199\u7aef<br \/>\n    \/\/\u5b9a\u65f6\u5668\u8bbe\u7f6e<br \/>\n    alarm(TIMESLOT);<\/p>\n<p>    \/\/\u5de5\u5177\u7c7b,\u4fe1\u53f7\u548c\u63cf\u8ff0\u7b26\u57fa\u7840\u64cd\u4f5c&#xff0c;\u5b58\u50a8\u5168\u5c40\u8d44\u6e90<br \/>\n    Utils::u_pipefd &#061; m_pipefd;<br \/>\n    Utils::u_epollfd &#061; m_epollfd;<br \/>\n} <\/p>\n<h4>timer&#xff08;&#xff09;\u00a0<\/h4>\n<p>\u4e3afd\u8bbe\u7f6e\u8ba1\u65f6\u5668&#xff0c;\u8ba1\u65f6\u5668\u51fd\u6570\u90fd\u5728timer\u76ee\u5f55\u7684lst_ timer\u4e0b&#xff0c;\u76f8\u5173\u51fd\u6570\u5b9a\u4e49\u53ef\u4ee5\u8df3\u8f6c\u7ffb\u770b<\/p>\n<p>\/\/\u4e3a\u65b0\u8fde\u63a5\u8bbe\u7f6e\u8ba1\u65f6\u5668<br \/>\nvoid WebServer::timer(int connfd, struct sockaddr_in client_address)<br \/>\n{<br \/>\n    \/\/users\u662fwebserver.h\u91cchttp_conn *users;\u521d\u59cb\u5316http\u8bf7\u6c42\u6570\u636e<br \/>\n    users[connfd].init(connfd, client_address, m_root, m_CONNTrigmode, m_close_log, m_user,                     m_passWord, m_databaseName);<\/p>\n<p>    \/\/\u521d\u59cb\u5316client_data\u6570\u636e<br \/>\n    \/\/\u521b\u5efa\u5b9a\u65f6\u5668&#xff0c;\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u548c\u8d85\u65f6\u65f6\u95f4&#xff0c;\u7ed1\u5b9a\u7528\u6237\u6570\u636e&#xff0c;\u5c06\u5b9a\u65f6\u5668\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d<br \/>\n    users_timer[connfd].address &#061; client_address;<br \/>\n    users_timer[connfd].sockfd &#061; connfd;<br \/>\n    \/\/\u8bbe\u7acb\u548c\u521b\u5efa\u5b9a\u65f6\u5668<br \/>\n    util_timer *timer &#061; new util_timer;<br \/>\n    timer-&gt;user_data &#061; &amp;users_timer[connfd];<br \/>\n    timer-&gt;cb_func &#061; cb_func;\/\/\u56de\u8c03\u51fd\u6570&#xff0c;\u5904\u7406\u51fd\u6570<br \/>\n    time_t cur &#061; time(NULL);\/\/\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233<br \/>\n    timer-&gt;expire &#061; cur &#043; 3 * TIMESLOT;\/\/\u8bbe\u7f6e\u5b9a\u65f6\u5668\u7684\u5230\u671f\u65f6\u95f4<br \/>\n    users_timer[connfd].timer &#061; timer;\/\/\u5b9a\u65f6\u5668\u5173\u8054<br \/>\n    utils.m_timer_lst.add_timer(timer);\/\/\u5b9a\u65f6\u5668\u6302\u8f7d\u5217\u8868<br \/>\n}<\/p>\n<h4>adjust_timer&#xff08;&#xff09;<\/h4>\n<p>\u8c03\u6574\u8ba1\u65f6\u5668&#xff0c;\u5ef6\u957f\u8fde\u63a5\u65f6\u95f4\u00a0<\/p>\n<p>\/\/\u82e5\u6709\u6570\u636e\u4f20\u8f93&#xff0c;\u5219\u5c06\u5b9a\u65f6\u5668\u5f80\u540e\u5ef6\u8fdf3\u4e2a\u5355\u4f4d<br \/>\n\/\/\u5e76\u5bf9\u65b0\u7684\u5b9a\u65f6\u5668\u5728\u94fe\u8868\u4e0a\u7684\u4f4d\u7f6e\u8fdb\u884c\u8c03\u6574<br \/>\nvoid WebServer::adjust_timer(util_timer *timer)<br \/>\n{<br \/>\n    time_t cur &#061; time(NULL);<br \/>\n    timer-&gt;expire &#061; cur &#043; 3 * TIMESLOT;<br \/>\n    utils.m_timer_lst.adjust_timer(timer);<br \/>\n    \/\/\u65e5\u5fd7\u8bb0\u5f55\u8c03\u6574<br \/>\n    LOG_INFO(&#034;%s&#034;, &#034;adjust timer once&#034;);<br \/>\n} <\/p>\n<h4>\u00a0deal_timer&#xff08;&#xff09;<\/h4>\n<p>\u5173\u95ed\u8ba1\u65f6\u5668<\/p>\n<p>\/\/\u5173\u95ed\u8ba1\u65f6\u5668<br \/>\nvoid WebServer::deal_timer(util_timer *timer, int sockfd)<br \/>\n{<br \/>\n    timer-&gt;cb_func(&amp;users_timer[sockfd]);<br \/>\n    if (timer)<br \/>\n    {<br \/>\n        utils.m_timer_lst.del_timer(timer);<br \/>\n    }<br \/>\n    \/\/\u65e5\u5fd7\u8bb0\u5f55\u8ba1\u65f6\u5668\u5173\u95ed<br \/>\n    LOG_INFO(&#034;close fd %d&#034;, users_timer[sockfd].sockfd);<br \/>\n} <\/p>\n<h4>dealclientdata&#xff08;&#xff09;<\/h4>\n<p>\u5904\u7406\u8fde\u63a5\u4e8b\u4ef6&#xff0c;\u6839\u636e\u4e0d\u540c\u7684\u89e6\u53d1\u6a21\u5f0f&#xff0c;accept\u521b\u5efa\u65b0\u7684\u5957\u63a5\u5b57\u5904\u7406\u540e\u7eed\u64cd\u4f5c<\/p>\n<p>\/\/\u5904\u7406\u8fde\u63a5\u4e8b\u4ef6<br \/>\nbool WebServer::dealclientdata()<br \/>\n{<br \/>\n    struct sockaddr_in client_address;<br \/>\n    socklen_t client_addrlength &#061; sizeof(client_address);<br \/>\n    if (0 &#061;&#061; m_LISTENTrigmode)   \/\/\u5982\u679c\u8bbe\u7f6eLT\u89e6\u53d1\u6a21\u5f0f<br \/>\n    {<br \/>\n        int connfd &#061; accept(m_listenfd, (struct sockaddr *)&amp;client_address, &amp;client_addrlength);<br \/>\n        if (connfd &lt; 0)<br \/>\n        {<br \/>\n            LOG_ERROR(&#034;%s:errno is:%d&#034;, &#034;accept error&#034;, errno);\/\/\u65e5\u5fd7<br \/>\n            return false;<br \/>\n        }<br \/>\n        if (http_conn::m_user_count &gt;&#061; MAX_FD)\/\/\u8d85\u51fa\u4e0a\u9650<br \/>\n        {<br \/>\n            utils.show_error(connfd, &#034;Internal server busy&#034;);<br \/>\n            LOG_ERROR(&#034;%s&#034;, &#034;Internal server busy&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        timer(connfd, client_address)&#xff1b;\u8bbe\u7f6e\u5b9a\u65f6\u5668<br \/>\n    }<\/p>\n<p>    else   \/\/ET\u6a21\u5f0f\u8fdb\u884c\u5faa\u73af\u5224\u65ad<br \/>\n    {<br \/>\n        while (1)<br \/>\n        {<br \/>\n            int connfd &#061; accept(m_listenfd, (struct sockaddr *)&amp;client_address, &amp;client_addrlength);<br \/>\n            if (connfd &lt; 0)<br \/>\n            {<br \/>\n                LOG_ERROR(&#034;%s:errno is:%d&#034;, &#034;accept error&#034;, errno);<br \/>\n                break;<br \/>\n            }<br \/>\n            if (http_conn::m_user_count &gt;&#061; MAX_FD)<br \/>\n            {<br \/>\n                utils.show_error(connfd, &#034;Internal server busy&#034;);<br \/>\n                LOG_ERROR(&#034;%s&#034;, &#034;Internal server busy&#034;);<br \/>\n                break;<br \/>\n            }<br \/>\n            timer(connfd, client_address);<br \/>\n        }<br \/>\n        return false;<br \/>\n    }<br \/>\n    return true;<br \/>\n}<\/p>\n<h4>dealwithsignal&#xff08;&#xff09;<\/h4>\n<p>\u5904\u7406\u4fe1\u53f7\u00a0<\/p>\n<p>\/\/\u5904\u7406\u4fe1\u53f7&#xff0c;\u5224\u65ad\u6761\u4ef6&#xff1a;\u662f\u5426\u505c\u6b62\u670d\u52a1\u5668&#xff0c;\u662f\u5426\u8ba1\u65f6\u5668\u7ed3\u675f&#xff0c;\u5426-&gt;\u5904\u7406<br \/>\nbool WebServer::dealwithsignal(bool &amp;timeout, bool &amp;stop_server)<br \/>\n{<br \/>\n    int ret &#061; 0;<br \/>\n    int sig;<br \/>\n    char signals[1024];<br \/>\n    ret &#061; recv(m_pipefd[0], signals, sizeof(signals), 0);\/\/\u8bfb\u53d6\u7ba1\u9053\u5185\u4fe1\u606f<br \/>\n    if (ret &#061;&#061; -1)\/\/\u9519\u8bef\u5904\u7406<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    else if (ret &#061;&#061; 0)<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        for (int i &#061; 0; i &lt; ret; &#043;&#043;i)<br \/>\n        {<br \/>\n            switch (signals[i])\/\/\u5904\u7406\u4fe1\u53f7<br \/>\n            {<br \/>\n            case SIGALRM:<br \/>\n            {<br \/>\n                timeout &#061; true;\/\/\u8bbe\u7f6e\u5df2\u7ecf\u8d85\u65f6&#xff0c;\u9700\u8981\u8fdb\u884c\u5904\u7406<br \/>\n                break;<br \/>\n            }<br \/>\n            case SIGTERM:<br \/>\n            {<br \/>\n                stop_server &#061; true;\/\/\u8868\u793a\u9700\u8981\u505c\u6b62\u670d\u52a1<br \/>\n                break;<br \/>\n            }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    return true;\/\/\u4fe1\u53f7\u5904\u7406\u5b8c\u6bd5<br \/>\n} <\/p>\n<h4>dealwithread&#xff08;&#xff09;<\/h4>\n<p>\u8bfb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff0c;\u6839\u636e\u8bbe\u5b9a\u6a21\u5f0f\u8fdb\u884c\u4e0d\u540c\u64cd\u4f5c&#xff0c;\u903b\u8f91\u76f8\u540c&#xff0c;\u90fd\u662f\u628a\u8bfb\u4e8b\u4ef6\u52a0\u5165\u8bf7\u6c42\u5904\u7406\u961f\u5217\u00a0<\/p>\n<p>\/\/\u8bfb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<br \/>\nvoid WebServer::dealwithread(int sockfd)<br \/>\n{<br \/>\n     \/\/\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u6709\u4e00\u4e2a\u8ba1\u65f6\u5668&#xff0c;\u786e\u4fdd\u8fde\u63a5\u8d85\u65f6\u8fdb\u884c\u91ca\u653e\u8d44\u6e90\u64cd\u4f5c<br \/>\n    util_timer *timer &#061; users_timer[sockfd].timer;<\/p>\n<p>    \/\/reactor \u5982\u679c\u8bbe\u5b9a\u4e3areactor\u6a21\u5f0f\u4e0b<br \/>\n    if (1 &#061;&#061; m_actormodel)<br \/>\n    {<br \/>\n        if (timer)<br \/>\n        {<br \/>\n            adjust_timer(timer);\/\/\u8c03\u6574timer&#xff0c;\u4ee5\u4fbf\u5ba2\u6237\u7aef\u5728\u6709\u6d3b\u52a8\u671f\u95f4\u4e0d\u65ad\u5f00\u8fde\u63a5<br \/>\n        }<\/p>\n<p>        \/\/\u82e5\u76d1\u6d4b\u5230\u8bfb\u4e8b\u4ef6&#xff0c;\u5c06\u8be5\u4e8b\u4ef6\u653e\u5165\u8bf7\u6c42\u961f\u5217<br \/>\n        m_pool-&gt;append(users &#043; sockfd, 0);<\/p>\n<p>        while (true)\/\/\u5faa\u73af\u5224\u5b9a&#xff0c;\u76f4\u5230<br \/>\n        {<br \/>\n            if (1 &#061;&#061; users[sockfd].improv)\/\/\u6709\u5176\u4ed6\u64cd\u4f5c\u8981\u6267\u884c<br \/>\n            {<br \/>\n                if (1 &#061;&#061; users[sockfd].timer_flag)\/\/\u9700\u8981\u5904\u7406\u5b9a\u65f6\u5668\u903b\u8f91<br \/>\n                {<br \/>\n                    deal_timer(timer, sockfd);<br \/>\n                    users[sockfd].timer_flag &#061; 0;<br \/>\n                }<br \/>\n                users[sockfd].improv &#061; 0;\/\/\u6807\u8bb0\u5df2\u7ecf\u5904\u7406&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        \/\/proactor<br \/>\n        if (users[sockfd].read_once())\/\/\u670d\u52a1\u5668\u4e3b\u52a8\u4ece\u5ba2\u6237\u7aef\u8bfb\u53d6\u6570\u636e\u5e76\u8fdb\u884c\u5904\u7406<br \/>\n        {<br \/>\n            LOG_INFO(&#034;deal with the client(%s)&#034;, inet_ntoa(users[sockfd].get_address()-&gt;sin_addr));\/\/\u6253\u5370\u65e5\u5fd7&#xff0c;\u8bb0\u5f55\u5ba2\u6237\u7aefIP<\/p>\n<p>            \/\/\u82e5\u76d1\u6d4b\u5230\u8bfb\u4e8b\u4ef6&#xff0c;\u5c06\u8be5\u4e8b\u4ef6\u653e\u5165\u8bf7\u6c42\u961f\u5217<br \/>\n            m_pool-&gt;append_p(users &#043; sockfd);<\/p>\n<p>            if (timer)<br \/>\n            {<br \/>\n                adjust_timer(timer);\/\/\u5ef6\u8fdf\u8fde\u63a5\u65f6\u95f4<br \/>\n            }<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            deal_timer(timer, sockfd);\/\/\u5904\u7406\u5931\u8d25&#xff0c;\u5173\u95ed\u8fde\u63a5<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<h4>dealwithwrite&#xff08;&#xff09;<\/h4>\n<p>\u5199\u4e8b\u4ef6\u5904\u7406&#xff0c;\u548c\u4e0a\u8ff0\u8bfb\u4e8b\u4ef6\u5904\u7406\u76f8\u540c\u00a0<\/p>\n<p>\/\/\u5904\u7406\u5199\u4e8b\u4ef6&#xff0c;\u548c\u8bfb\u4e8b\u4ef6\u4ee3\u7801\u903b\u8f91\u76f8\u540c&#xff0c;\u5148\u5224\u65ad\u4f55\u79cd\u89e6\u53d1\u6a21\u5f0f&#xff0c;\u7136\u540e\u4ea4\u7ed9\u5904\u7406\u961f\u5217\u5904\u7406&#xff0c;\u5173\u95ed\u6216\u8005\u5ef6\u957f\u8ba1\u65f6\u5668<br \/>\nvoid WebServer::dealwithwrite(int sockfd)<br \/>\n{<br \/>\n    util_timer *timer &#061; users_timer[sockfd].timer;<br \/>\n    \/\/reactor<br \/>\n    if (1 &#061;&#061; m_actormodel)<br \/>\n    {<br \/>\n        if (timer)<br \/>\n        {<br \/>\n            adjust_timer(timer);<br \/>\n        }<\/p>\n<p>        m_pool-&gt;append(users &#043; sockfd, 1);<\/p>\n<p>        while (true)<br \/>\n        {<br \/>\n            if (1 &#061;&#061; users[sockfd].improv)<br \/>\n            {<br \/>\n                if (1 &#061;&#061; users[sockfd].timer_flag)<br \/>\n                {<br \/>\n                    deal_timer(timer, sockfd);<br \/>\n                    users[sockfd].timer_flag &#061; 0;<br \/>\n                }<br \/>\n                users[sockfd].improv &#061; 0;<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        \/\/proactor<br \/>\n        if (users[sockfd].write())<br \/>\n        {<br \/>\n            LOG_INFO(&#034;send data to the client(%s)&#034;, inet_ntoa(users[sockfd].get_address()-&gt;sin_addr));<\/p>\n<p>            if (timer)<br \/>\n            {<br \/>\n                adjust_timer(timer);<br \/>\n            }<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            deal_timer(timer, sockfd);<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<h2>\u603b\u7ed3webserver&#xff1a;<\/h2>\n<p>\u5728\u770b\u5b8cwebserver\u7684\u4ee3\u7801\u540e&#xff0c;\u6211\u4eec\u77e5\u9053\u4e86\u670d\u52a1\u5668\u7684\u5927\u4f53\u6a21\u578b\u6846\u67b6\u4e3a&#xff1a;<\/p>\n<p>\u5148\u521d\u59cb\u5316\u65e5\u5fd7&#xff0c;\u6570\u636e\u5e93&#xff0c;\u7ebf\u7a0b\u6c60&#xff0c;\u89e6\u53d1\u6a21\u5f0f&#xff0c;\u7136\u540eeventlisten\u521b\u5efa\u4e86socket\u5957\u63a5\u5b57&#xff0c;\u8fdb\u884c\u670d\u52a1\u5668\u7684\u5e38\u89c4\u5316\u6784\u5efa\u64cd\u4f5c&#xff0c;\u901a\u8fc7socket&#xff0c;bind&#xff0c;listen\u3002<\/p>\n<p>\u7136\u540esetsocketopt\u8bbe\u7f6e\u662f\u5426\u4f18\u96c5\u5173\u95ed\u8fde\u63a5&#xff0c;\u63a5\u7740epoll\u521b\u5efa\u5185\u6838\u4e8b\u4ef6\u8868&#xff0c;\u5c06m_listenfd\u6302\u4e0a\u76d1\u542c\u6811&#xff0c;\u4e0e\u6b64\u540c\u65f6&#xff0c;\u521b\u5efam_pipe\u7ba1\u9053&#xff0c;\u5c06\u8bfb\u7aef\u4e00\u8d77\u6302\u4e0a\u76d1\u542c\u6811&#xff0c;\u4fe1\u53f7\u4f20\u8f93\u5c31\u662f\u5728\u7ba1\u9053\u95f4\u8fdb\u884c\u7684&#xff0c;\u7136\u540e\u8bbe\u7f6e\u4e8b\u4ef6\u4fe1\u53f7\u00a0&#xff0c;\u5982&#xff1a;\u8ba1\u65f6\u5668\u7ed3\u675f\u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u7ed3\u675f\u4fe1\u53f7&#xff0c;\u5e76\u5ffd\u7565pipe\u95f4\u901a\u4fe1\u7684\u5e38\u89c1\u9519\u8bef\u4fe1\u53f7\u4ee5\u514d\u5e72\u6270\u63a5\u53d7\u4fe1\u53f7\u7684\u51c6\u786e\u5ea6\u3002<\/p>\n<p>\u63a5\u7740&#xff0c;\u5728\u670d\u52a1\u5668\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u8bf7\u6c42\u65f6&#xff0c;loop\u5224\u65ad\u5904\u7406\u8bf7\u6c42\u4e3a\u8bf7\u6c42\u8fde\u63a5&#xff0c;\u8c03\u7528dealclient\u51fd\u6570\u5904\u7406&#xff0c;\u521b\u5efaclientfd\u5efa\u7acb\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u7136\u540edealclient\u51fd\u6570\u8c03\u7528timer\u51fd\u6570\u8bbe\u7acb\u5b9a\u65f6\u5668&#xff0c;timer\u51fd\u6570\u4e2d\u53c8\u5c06client\u6570\u636e\u7ed9http_conn\u521d\u59cb\u5316&#xff0c;\u6302\u4e0a\u76d1\u542c\u6811\u3002<\/p>\n<p>loop\u5faa\u73af\u5230pipe\u4e0a\u7684\u4fe1\u53f7\u65f6\u8fdb\u884c\u4fe1\u53f7\u5904\u7406&#xff0c;\u6536\u5230\u670d\u52a1\u7aef\u5173\u95ed\u8fde\u63a5\u65f6&#xff0c;\u5173\u95ed\u8ba1\u65f6\u5668\u3002<\/p>\n<p>loop\u5faa\u73af\u5230\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u65f6&#xff0c;\u5224\u65ad\u4e8b\u4ef6\u4e3a\u8bfb\u6216\u5199\u4e8b\u4ef6&#xff0c;\u8fdb\u884cdealwithread\/dealwithwrite\u5904\u7406&#xff0c;\u6839\u636e\u4e0d\u540c\u7684\u8bbe\u5b9a\u6a21\u5f0f\u8fdb\u884creactor\/proactor\u5904\u7406&#xff0c;\u5c06\u4e8b\u4ef6\u9632\u653e\u5165\u8bf7\u6c42\u961f\u5217\u4e2d&#xff0c;\u8ba9\u7ebf\u7a0b\u6c60\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u6700\u540e\u5982\u8ba1\u65f6\u5668\u7ed3\u675f\u8c03\u7528utils.timer_handler\u5904\u7406\u8ba1\u65f6\u5668&#xff0c;while\u5faa\u73af\u7531\u670d\u52a1\u5668\u5173\u95ed\u8fde\u63a5&#xff0c;stop_server&#061;true\u65f6\u8df3\u51fa&#xff0c;\u670d\u52a1\u5668\u505c\u6b62\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb886\u6b21\uff0c\u70b9\u8d5e18\u6b21\uff0c\u6536\u85cf30\u6b21\u3002\u89e3\u8bfbmian.cpp config.h config.cpp webserver.h webserver.cpp\u6587\u4ef6\uff0c\u8bfb\u61c2\u9879\u76ee\u67b6\u6784_tingwebio\u5e93<\/p>\n","protected":false},"author":2,"featured_media":37861,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,1351,43],"topic":[],"class_list":["post-37862","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-tingwebserver","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/37862.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb886\u6b21\uff0c\u70b9\u8d5e18\u6b21\uff0c\u6536\u85cf30\u6b21\u3002\u89e3\u8bfbmian.cpp config.h config.cpp webserver.h webserver.cpp\u6587\u4ef6\uff0c\u8bfb\u61c2\u9879\u76ee\u67b6\u6784_tingwebio\u5e93\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/37862.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-17T23:26:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517232602-68291b0aec4e5.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37862.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/37862.html\",\"name\":\"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-17T23:26:04+00:00\",\"dateModified\":\"2025-05-17T23:26:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/37862.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/37862.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37862.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/37862.html","og_locale":"zh_CN","og_type":"article","og_title":"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb886\u6b21\uff0c\u70b9\u8d5e18\u6b21\uff0c\u6536\u85cf30\u6b21\u3002\u89e3\u8bfbmian.cpp config.h config.cpp webserver.h webserver.cpp\u6587\u4ef6\uff0c\u8bfb\u61c2\u9879\u76ee\u67b6\u6784_tingwebio\u5e93","og_url":"https:\/\/www.wsisp.com\/helps\/37862.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-17T23:26:04+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517232602-68291b0aec4e5.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/37862.html","url":"https:\/\/www.wsisp.com\/helps\/37862.html","name":"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-17T23:26:04+00:00","dateModified":"2025-05-17T23:26:04+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/37862.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/37862.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/37862.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"TingWebServer\u670d\u52a1\u5668\u4ee3\u7801\u89e3\u8bfb01"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/37862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=37862"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/37862\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/37861"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=37862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=37862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=37862"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=37862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}