{"id":12724,"date":"2025-04-18T19:47:50","date_gmt":"2025-04-18T11:47:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/12724.html"},"modified":"2025-04-18T19:47:50","modified_gmt":"2025-04-18T11:47:50","slug":"%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%85%ad%ef%bc%9ac%e5%ae%9e%e7%8e%b0%e5%9f%ba%e4%ba%8ereactor%e7%9a%84%e7%99%be%e4%b8%87%e7%ba%a7%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/12724.html","title":{"rendered":"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<p>\u76ee\u5f55<\/p>\n<p id=\"%E5%89%8D%E8%A8%80-toc\" style=\"margin-left:0px\">\u524d\u8a00<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E6%A0%87%E9%A2%98%E7%9A%84%E7%99%BE%E4%B8%87%E7%BA%A7%E5%B9%B6%E5%8F%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F-toc\" style=\"margin-left:0px\">\u4e00\u3001\u57fa\u4e8e Reactor \u6a21\u5f0f\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E6%BA%90%E7%A0%81%E5%B1%95%E7%A4%BA-toc\" style=\"margin-left:0px\">\u4e8c\u3001\u6e90\u7801\u5c55\u793a<\/p>\n<p id=\"-toc\" style=\"margin-left:80px\">\n<p id=\"%E4%BA%8C%E3%80%81%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4-toc\" style=\"margin-left:0px\">\u4e09\u3001\u4ee3\u7801\u5206\u6790<\/p>\n<p id=\"1.%E5%BC%95%E5%85%A5%E5%BA%93-toc\" style=\"margin-left:40px\">1.\u5b9a\u4e49\u5e38\u91cf\u4e0e\u7ed3\u6784\u4f53<\/p>\n<p id=\"2.%E8%AF%BB%E5%85%A5%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:40px\">\n<p id=\"2.set_event%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">2.set_event \u51fd\u6570<\/p>\n<p id=\"3.event_register%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">3.event_register \u51fd\u6570<\/p>\n<p id=\"4.%E8%BF%9E%E6%8E%A5%E6%8E%A5%E6%94%B6%E4%B8%8E%E5%8F%91%E9%80%81%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">4.\u8fde\u63a5\u63a5\u6536\u4e0e\u53d1\u9001\u56de\u8c03\u51fd\u6570<\/p>\n<p id=\"5.init_server%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">5.init_server \u51fd\u6570<\/p>\n<p id=\"6.main%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">6.main \u51fd\u6570<\/p>\n<p id=\"7.%E6%80%BB%E7%BB%93%EF%BC%9A-toc\" style=\"margin-left:40px\">7.\u603b\u7ed3&#xff1a;<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:0px\">\u56db\u3001\u5e38\u89c1\u95ee\u9898<\/p>\n<p id=\"1.%E9%BB%98%E8%AE%A4%E7%9A%84open%20files%E6%95%B0%E9%87%8F%E9%99%90%E5%88%B6%E4%B8%BA1024%E2%80%8B%E7%BC%96%E8%BE%91-toc\" style=\"margin-left:40px\">1.\u9ed8\u8ba4\u7684open files\u6570\u91cf\u9650\u5236\u4e3a1024\u200b\u7f16\u8f91<\/p>\n<p id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A-toc\" style=\"margin-left:40px\">\u89e3\u51b3\u65b9\u6848&#xff1a;<\/p>\n<p id=\"2.%E4%B8%8D%E8%83%BD%E5%88%86%E9%85%8D%E5%9C%B0%E5%9D%80-toc\" style=\"margin-left:40px\">2.\u4e0d\u80fd\u5206\u914d\u5730\u5740<\/p>\n<p id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A%E5%BB%BA%E7%AB%8B%E5%A4%9A%E4%B8%AAserver(%E6%8F%90%E4%BE%9Bsport)-toc\" style=\"margin-left:40px\">\u89e3\u51b3\u65b9\u6848&#xff1a;\u5efa\u7acb\u591a\u4e2aserver(\u63d0\u4f9bsport)<\/p>\n<p id=\"3.%E7%B3%BB%E7%BB%9F%E7%89%88%E6%9C%AC%E5%AF%BC%E8%87%B4%E7%9A%84%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:40px\">3.\u7cfb\u7edf\u7248\u672c\u5bfc\u81f4\u7684\u95ee\u9898<\/p>\n<p id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A-toc\" style=\"margin-left:40px\">\u89e3\u51b3\u65b9\u6848&#xff1a;<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E7%99%BE%E4%B8%87%E7%BA%A7%E5%B9%B6%E5%8F%91%E7%BB%93%E6%9E%9C%E5%B1%95%E7%A4%BA-toc\" style=\"margin-left:0px\">\u4e94\u3001\u767e\u4e07\u7ea7\u5e76\u53d1\u7ed3\u679c\u5c55\u793a<\/p>\n<p id=\"%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:0px\">\u603b\u7ed3<\/p>\n<hr \/>\n<h2 id=\"%E5%89%8D%E8%A8%80\">\u524d\u8a00<\/h2>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u57fa\u4e8e\u672c\u7cfb\u5217\u4e0a\u4e00\u7bc7\u300a\u7f51\u7edc\u7f16\u7a0b(\u4e94)\u300b\u7684reactor\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u505a\u4e86\u4e00\u4e9b\u6539\u8fdb\u800c\u6210\u3002<\/p>\n<p>0voice \u00b7 GitHub<\/p>\n<hr \/>\n<h2 id=\"%E4%B8%80%E3%80%81%E6%A0%87%E9%A2%98%E7%9A%84%E7%99%BE%E4%B8%87%E7%BA%A7%E5%B9%B6%E5%8F%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F\">\u4e00\u3001\u57fa\u4e8e Reactor \u6a21\u5f0f\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668\u662f\u4ec0\u4e48&#xff1f;<\/h2>\n<p>\u57fa\u4e8e Reactor \u6a21\u5f0f\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 \u662f\u6307\u4e00\u4e2a\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u767e\u4e07\u7ea7\u5e76\u53d1\u8fde\u63a5\u7684\u670d\u52a1\u5668\u67b6\u6784&#xff0c;\u5b83\u901a\u5e38\u4f7f\u7528 Reactor \u8bbe\u8ba1\u6a21\u5f0f\u6765\u7ba1\u7406\u5927\u91cf\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002Reactor \u6a21\u5f0f\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u6a21\u5f0f&#xff0c;\u4e3b\u8981\u7528\u4e8e I\/O \u591a\u8def\u590d\u7528&#xff0c;\u4f7f\u5f97\u670d\u52a1\u5668\u53ef\u4ee5\u5728\u5355\u4e00\u7ebf\u7a0b\u6216\u5c11\u91cf\u7ebf\u7a0b\u4e2d\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5&#xff0c;\u907f\u514d\u4e86\u4f20\u7edf\u7684\u591a\u7ebf\u7a0b\u6a21\u578b\u4e2d\u7ebf\u7a0b\u5f00\u9500\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u6027\u80fd\u74f6\u9888\u3002\u672c\u6587\u91c7\u7528\u5355\u7ebf\u7a0b\u5904\u7406\u767e\u4e07\u7ea7\u5e76\u53d1\u3002<\/p>\n<h2 id=\"%E4%BA%8C%E3%80%81%E6%BA%90%E7%A0%81%E5%B1%95%E7%A4%BA\">\u4e8c\u3001\u6e90\u7801\u5c55\u793a<\/h2>\n<p>#include &lt;errno.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;poll.h&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;errno.h&gt;<br \/>\n#include &lt;sys\/time.h&gt;<\/p>\n<p>#define BUFFER_LENGTH1024<br \/>\n#define CONNECTION_SIZE    1048576<\/p>\n<p>#define MAX_PORTS20<\/p>\n<p>#define TIME_SUB_MS(tv1, tv2)  ((tv1.tv_sec &#8211; tv2.tv_sec) * 1000 &#043; (tv1.tv_usec &#8211; tv2.tv_usec) \/ 1000)<\/p>\n<p>typedef int (*RCALLBACK)(int fd);<\/p>\n<p>int accept_cb(int fd);<br \/>\nint recv_cb(int fd);<br \/>\nint send_cb(int fd);<\/p>\n<p>int epfd &#061; 0;<br \/>\nstruct timeval begin;<\/p>\n<p>struct conn {<br \/>\nint fd;<\/p>\n<p>char rbuffer[BUFFER_LENGTH];<br \/>\nint rlength;<\/p>\n<p>char wbuffer[BUFFER_LENGTH];<br \/>\nint wlength;<\/p>\n<p>RCALLBACK send_callback;<\/p>\n<p>union {<br \/>\nRCALLBACK recv_callback;<br \/>\nRCALLBACK accept_callback;<br \/>\n} r_action;<\/p>\n<p>};<\/p>\n<p>\/\/fd\u505a\u4e0b\u6807<br \/>\nstruct conn conn_list[CONNECTION_SIZE] &#061; {0};<\/p>\n<p>int set_event(int fd, int event, int flag) {<\/p>\n<p>if (flag) {  \/\/ non-zero add<\/p>\n<p>struct epoll_event ev;<br \/>\nev.events &#061; event;<br \/>\nev.data.fd &#061; fd;<br \/>\nepoll_ctl(epfd, EPOLL_CTL_ADD, fd, &amp;ev);<\/p>\n<p>} else {  \/\/ zero mod<\/p>\n<p>struct epoll_event ev;<br \/>\nev.events &#061; event;<br \/>\nev.data.fd &#061; fd;<br \/>\nepoll_ctl(epfd, EPOLL_CTL_MOD, fd, &amp;ev);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>int event_register(int fd, int event) {<\/p>\n<p>if (fd &lt; 0) return -1;<\/p>\n<p>conn_list[fd].fd &#061; fd;<br \/>\nconn_list[fd].r_action.recv_callback &#061; recv_cb;<br \/>\nconn_list[fd].send_callback &#061; send_cb;<\/p>\n<p>memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH);<br \/>\nconn_list[fd].rlength &#061; 0;<\/p>\n<p>memset(conn_list[fd].wbuffer, 0, BUFFER_LENGTH);<br \/>\nconn_list[fd].wlength &#061; 0;<\/p>\n<p>set_event(fd, event, 1);<br \/>\n}<\/p>\n<p>\/\/ listenfd(sockfd) &#8211;&gt; EPOLLIN &#8211;&gt; accept_cb<br \/>\nint accept_cb(int fd) {<\/p>\n<p>struct sockaddr_in  clientaddr;<br \/>\nsocklen_t len &#061; sizeof(clientaddr);<\/p>\n<p>int clientfd &#061; accept(fd, (struct sockaddr*)&amp;clientaddr, &amp;len);<br \/>\n\/\/printf(&#034;accept finshed: %d\\\\n&#034;, clientfd);<br \/>\nif (clientfd &lt; 0) {<br \/>\nprintf(&#034;accept errno: %d &#8211;&gt; %s\\\\n&#034;, errno, strerror(errno));<br \/>\nreturn -1;<br \/>\n}<\/p>\n<p>event_register(clientfd, EPOLLIN);  \/\/ | EPOLLET<\/p>\n<p>if ((clientfd % 1000) &#061;&#061; 0) {<\/p>\n<p>struct timeval current;<br \/>\ngettimeofday(&amp;current, NULL);<\/p>\n<p>int time_used &#061; TIME_SUB_MS(current, begin);<br \/>\nmemcpy(&amp;begin, &amp;current, sizeof(struct timeval));<\/p>\n<p>printf(&#034;accept finshed: %d, time_used: %d\\\\n&#034;, clientfd, time_used);<\/p>\n<p>}<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<p>int recv_cb(int fd) {<\/p>\n<p>memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH );<br \/>\nint count &#061; recv(fd, conn_list[fd].rbuffer, BUFFER_LENGTH, 0);<br \/>\nif (count &#061;&#061; 0) { \/\/ disconnect<br \/>\nprintf(&#034;client disconnect: %d\\\\n&#034;, fd);<br \/>\nclose(fd);<\/p>\n<p>epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); \/\/ unfinished<\/p>\n<p>return 0;<br \/>\n} else if (count &lt; 0) { \/\/ <\/p>\n<p>printf(&#034;count: %d, errno: %d, %s\\\\n&#034;, count, errno, strerror(errno));<br \/>\nclose(fd);<br \/>\nepoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<p>conn_list[fd].rlength &#061; count;<br \/>\n\/\/printf(&#034;RECV: %s\\\\n&#034;, conn_list[fd].rbuffer);<\/p>\n<p>    \/\/ echo<br \/>\nconn_list[fd].wlength &#061; conn_list[fd].rlength;<br \/>\nmemcpy(conn_list[fd].wbuffer, conn_list[fd].rbuffer, conn_list[fd].wlength);<\/p>\n<p>set_event(fd, EPOLLOUT, 0);<\/p>\n<p>return count;<br \/>\n}<\/p>\n<p>int send_cb(int fd) {<\/p>\n<p>int count &#061; 0;<\/p>\n<p>if (conn_list[fd].wlength !&#061; 0) {<br \/>\ncount &#061; send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);<br \/>\n}<\/p>\n<p>set_event(fd, EPOLLIN, 0);<\/p>\n<p>return count;<br \/>\n}<\/p>\n<p>int init_server(unsigned short port) {<\/p>\n<p>int sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<\/p>\n<p>struct sockaddr_in servaddr;<br \/>\nservaddr.sin_family &#061; AF_INET;<br \/>\nservaddr.sin_addr.s_addr &#061; htonl(INADDR_ANY); \/\/ 0.0.0.0<br \/>\nservaddr.sin_port &#061; htons(port); \/\/ 0-1023, <\/p>\n<p>if (-1 &#061;&#061; bind(sockfd, (struct sockaddr*)&amp;servaddr, sizeof(struct sockaddr))<br \/>\n) {<br \/>\nprintf(&#034;bind failed: %s\\\\n&#034;, strerror(errno));<br \/>\n}<\/p>\n<p>listen(sockfd, 10);<br \/>\n\/\/printf(&#034;listen finshed: %d\\\\n&#034;, sockfd); \/\/ 3 <\/p>\n<p>return sockfd;<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>unsigned short port &#061; 2000;<\/p>\n<p>    epfd &#061; epoll_create(1);<\/p>\n<p>    int i &#061; 0;<\/p>\n<p>for (i &#061; 0;i &lt; MAX_PORTS;i &#043;&#043;) {<\/p>\n<p>int sockfd &#061; init_server(port &#043; i);<\/p>\n<p>conn_list[sockfd].fd &#061; sockfd;<br \/>\nconn_list[sockfd].r_action.recv_callback &#061; accept_cb;<br \/>\nset_event(sockfd, EPOLLIN, 1);<\/p>\n<p>}<\/p>\n<p>gettimeofday(&amp;begin, NULL);<\/p>\n<p>    while (1) { \/\/ mainloop<\/p>\n<p>struct epoll_event events[1024] &#061; {0};<br \/>\nint nready &#061; epoll_wait(epfd, events, 1024, -1);<\/p>\n<p>int i &#061; 0;<br \/>\nfor (i &#061; 0;i &lt; nready;i &#043;&#043;) {<\/p>\n<p>int connfd &#061; events[i].data.fd;<\/p>\n<p>if (events[i].events &amp; EPOLLIN) {<br \/>\nconn_list[connfd].r_action.recv_callback(connfd);<br \/>\n} <\/p>\n<p>if (events[i].events &amp; EPOLLOUT) {<br \/>\nconn_list[connfd].send_callback(connfd);<br \/>\n}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h4><\/h4>\n<h2 id=\"%E4%BA%8C%E3%80%81%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4\">\u4e09\u3001\u4ee3\u7801\u5206\u6790<\/h2>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e epoll \u7684 I\/O \u591a\u8def\u590d\u7528\u7f51\u7edc\u670d\u52a1\u5668\u5b9e\u73b0\u3002\u5b83\u7684\u6838\u5fc3\u529f\u80fd\u662f\u76d1\u542c\u591a\u4e2a\u7aef\u53e3&#xff0c;\u63a5\u53d7\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8fde\u63a5&#xff0c;\u5e76\u4e14\u901a\u8fc7\u56de\u8c03\u673a\u5236\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\u548c\u53d1\u9001\u7684\u6570\u636e\u3002\u5b83\u5229\u7528\u4e86 epoll \u7684\u9ad8\u6548\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u6765\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u8fde\u63a5\u3002<\/p>\n<h3 id=\"1.%E5%BC%95%E5%85%A5%E5%BA%93\">1.\u5b9a\u4e49\u5e38\u91cf\u4e0e\u7ed3\u6784\u4f53<\/h3>\n<p>#define BUFFER_LENGTH        1024<br \/>\n#define CONNECTION_SIZE      1048576<br \/>\n#define MAX_PORTS            20<\/p>\n<p>#define TIME_SUB_MS(tv1, tv2)  ((tv1.tv_sec &#8211; tv2.tv_sec) * 1000 &#043; (tv1.tv_usec &#8211; tv2.tv_usec) \/ 1000)<\/p>\n<p>typedef int (*RCALLBACK)(int fd);<\/p>\n<h3 id=\"2.%E8%AF%BB%E5%85%A5%E6%95%B0%E6%8D%AE\"><\/h3>\n<ul>\n<li>BUFFER_LENGTH&#xff1a;\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u3002<\/li>\n<li>CONNECTION_SIZE&#xff1a;\u6700\u5927\u8fde\u63a5\u6570\u3002<\/li>\n<li>MAX_PORTS&#xff1a;\u6700\u5927\u76d1\u542c\u7684\u7aef\u53e3\u6570\u3002<\/li>\n<li>TIME_SUB_MS \u5b8f\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a struct timeval \u7c7b\u578b\u7684\u65f6\u95f4\u5dee&#xff08;\u5355\u4f4d\u4e3a\u6beb\u79d2&#xff09;\u3002<\/li>\n<li>RCALLBACK \u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7c7b\u578b&#xff0c;\u8868\u793a\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>struct conn {<br \/>\n    int fd;<br \/>\n    char rbuffer[BUFFER_LENGTH];<br \/>\n    int rlength;<br \/>\n    char wbuffer[BUFFER_LENGTH];<br \/>\n    int wlength;<br \/>\n    RCALLBACK send_callback;<br \/>\n    union {<br \/>\n        RCALLBACK recv_callback;<br \/>\n        RCALLBACK accept_callback;<br \/>\n    } r_action;<br \/>\n};<\/p>\n<ul>\n<li>\n<p>conn \u7ed3\u6784\u4f53\u7528\u4e8e\u7ba1\u7406\u6bcf\u4e2a\u8fde\u63a5\u7684\u72b6\u6001\u3002\u5b83\u5305\u542b\u4e86\u4e0e\u8fde\u63a5\u76f8\u5173\u7684\u5404\u79cd\u4fe1\u606f&#xff0c;\u6bd4\u5982\u8bfb\u53d6\u7f13\u51b2\u533a\u3001\u5199\u5165\u7f13\u51b2\u533a\u3001\u8bfb\u53d6\u548c\u5199\u5165\u7684\u6570\u636e\u957f\u5ea6\u3001\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>unino r_action\u662f\u6307\u8bfb\u7f13\u51b2\u533a\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u4e0a\u9762\u7684recallback\u5bf9\u5e94\u5199\u7f13\u51b2\u533a\u7684\u56de\u8c03\u51fd\u6570<\/p>\n<\/li>\n<\/ul>\n<h3 id=\"2.set_event%20%E5%87%BD%E6%95%B0\">2.set_event \u51fd\u6570<\/h3>\n<p>int set_event(int fd, int event, int flag) {<br \/>\n    if (flag) {  \/\/ non-zero add<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; event;<br \/>\n        ev.data.fd &#061; fd;<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &amp;ev);<br \/>\n    } else {  \/\/ zero mod<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; event;<br \/>\n        ev.data.fd &#061; fd;<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &amp;ev);<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>set_event \u51fd\u6570\u7528\u4e8e\u5411 epoll \u6dfb\u52a0\u6216\u4fee\u6539\u4e8b\u4ef6\u3002\u6839\u636e flag \u7684\u503c&#xff0c;\u51b3\u5b9a\u662f\u6dfb\u52a0\u4e8b\u4ef6&#xff08;EPOLL_CTL_ADD&#xff09;\u8fd8\u662f\u4fee\u6539\u4e8b\u4ef6&#xff08;EPOLL_CTL_MOD&#xff09;\u3002\u901a\u8fc7 epoll_ctl \u7cfb\u7edf\u8c03\u7528\u4e0e epoll \u6587\u4ef6\u63cf\u8ff0\u7b26 epfd \u4ea4\u4e92\u6765\u7ba1\u7406\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<h3 id=\"3.event_register%20%E5%87%BD%E6%95%B0\">3.event_register \u51fd\u6570<\/h3>\n<p>int event_register(int fd, int event) {<br \/>\n    if (fd &lt; 0) return -1;<br \/>\n    conn_list[fd].fd &#061; fd;<br \/>\n    conn_list[fd].r_action.recv_callback &#061; recv_cb;<br \/>\n    conn_list[fd].send_callback &#061; send_cb;<br \/>\n    memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH);<br \/>\n    conn_list[fd].rlength &#061; 0;<br \/>\n    memset(conn_list[fd].wbuffer, 0, BUFFER_LENGTH);<br \/>\n    conn_list[fd].wlength &#061; 0;<br \/>\n    set_event(fd, event, 1);<br \/>\n}<\/p>\n<ul>\n<li>event_register \u51fd\u6570\u7528\u4e8e\u4e3a\u4e00\u4e2a\u8fde\u63a5&#xff08;fd&#xff09;\u6ce8\u518c\u4e8b\u4ef6\u5e76\u521d\u59cb\u5316\u8fde\u63a5\u7684\u72b6\u6001(\u5c31\u662f\u6ce8\u518cclientfd)\u3002\u5b83\u8bbe\u7f6e\u63a5\u6536\u56de\u8c03\u51fd\u6570\u3001\u53d1\u9001\u56de\u8c03\u51fd\u6570&#xff0c;\u4ee5\u53ca\u8fde\u63a5\u7684\u8bfb\u53d6\u548c\u5199\u5165\u7f13\u51b2\u533a\u3002<\/li>\n<\/ul>\n<h3 id=\"4.%E8%BF%9E%E6%8E%A5%E6%8E%A5%E6%94%B6%E4%B8%8E%E5%8F%91%E9%80%81%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0\">4.\u8fde\u63a5\u63a5\u6536\u4e0e\u53d1\u9001\u56de\u8c03\u51fd\u6570<\/h3>\n<p>int accept_cb(int fd) {<br \/>\n    struct sockaddr_in clientaddr;<br \/>\n    socklen_t len &#061; sizeof(clientaddr);<br \/>\n    int clientfd &#061; accept(fd, (struct sockaddr*)&amp;clientaddr, &amp;len);<br \/>\n    if (clientfd &lt; 0) {<br \/>\n        printf(&#034;accept errno: %d &#8211;&gt; %s\\\\n&#034;, errno, strerror(errno));<br \/>\n        return -1;<br \/>\n    }<br \/>\n    event_register(clientfd, EPOLLIN);<br \/>\n    if ((clientfd % 1000) &#061;&#061; 0) {<br \/>\n        struct timeval current;<br \/>\n        gettimeofday(&amp;current, NULL);<br \/>\n        int time_used &#061; TIME_SUB_MS(current, begin);<br \/>\n        memcpy(&amp;begin, &amp;current, sizeof(struct timeval));<br \/>\n        printf(&#034;accept finshed: %d, time_used: %d\\\\n&#034;, clientfd, time_used);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>accept_cb&#xff1a;\u8be5\u51fd\u6570\u5904\u7406\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<ul>\n<li>\u8c03\u7528 accept \u51fd\u6570\u63a5\u53d7\u8fde\u63a5&#xff0c;\u8fd4\u56de\u5ba2\u6237\u7aef\u7684\u5957\u63a5\u5b57 clientfd\u3002<\/li>\n<li>\u6ce8\u518c clientfd \u7684\u4e8b\u4ef6&#xff08;\u76d1\u542c EPOLLIN&#xff09;\u3002<\/li>\n<li>\u6253\u5370\u6bcf\u6b21\u63a5\u53d7\u8fde\u63a5\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>int recv_cb(int fd) {<br \/>\n    memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH);<br \/>\n    int count &#061; recv(fd, conn_list[fd].rbuffer, BUFFER_LENGTH, 0);<br \/>\n    if (count &#061;&#061; 0) {<br \/>\n        printf(&#034;client disconnect: %d\\\\n&#034;, fd);<br \/>\n        close(fd);<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);<br \/>\n        return 0;<br \/>\n    } else if (count &lt; 0) {<br \/>\n        printf(&#034;count: %d, errno: %d, %s\\\\n&#034;, count, errno, strerror(errno));<br \/>\n        close(fd);<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);<br \/>\n        return 0;<br \/>\n    }<br \/>\n    conn_list[fd].rlength &#061; count;<br \/>\n    conn_list[fd].wlength &#061; conn_list[fd].rlength;<br \/>\n    memcpy(conn_list[fd].wbuffer, conn_list[fd].rbuffer, conn_list[fd].wlength);<br \/>\n    set_event(fd, EPOLLOUT, 0);<br \/>\n    return count;<br \/>\n}<\/p>\n<p>recv_cb&#xff1a;\u8be5\u51fd\u6570\u5904\u7406\u4ece\u5ba2\u6237\u7aef\u63a5\u6536\u5230\u7684\u6570\u636e\u3002<\/p>\n<ul>\n<li>\u8bfb\u53d6\u6570\u636e\u5230 rbuffer&#xff0c;\u5982\u679c\u8bfb\u53d6\u5931\u8d25\u6216\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5&#xff0c;\u5219\u5173\u95ed\u8fde\u63a5\u3002<\/li>\n<li>\u5c06\u63a5\u6536\u5230\u7684\u6570\u636e\u590d\u5236\u5230 wbuffer&#xff0c;\u51c6\u5907\u53d1\u9001\u3002<\/li>\n<li>\u8bbe\u7f6e EPOLLOUT \u4e8b\u4ef6&#xff0c;\u4ee5\u4fbf\u5728\u4e0b\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5904\u7406\u6570\u636e\u53d1\u9001&#xff08;\u5173\u6ce8\u5199\u4e8b\u4ef6&#xff09;\u3002<\/li>\n<\/ul>\n<p>int send_cb(int fd) {<br \/>\n    int count &#061; 0;<br \/>\n    if (conn_list[fd].wlength !&#061; 0) {<br \/>\n        count &#061; send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);<br \/>\n    }<br \/>\n    set_event(fd, EPOLLIN, 0);<br \/>\n    return count;<br \/>\n}<\/p>\n<p>send_cb&#xff1a;\u8be5\u51fd\u6570\u5904\u7406\u6570\u636e\u53d1\u9001\u3002<\/p>\n<ul>\n<li>\u4ece wbuffer \u4e2d\u53d1\u9001\u6570\u636e\u5230\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u8bbe\u7f6e EPOLLIN \u4e8b\u4ef6&#xff0c;\u4ee5\u4fbf\u5904\u7406\u63a5\u6536\u6570\u636e(\u5173\u6ce8\u8bfb\u4e8b\u4ef6)\u3002<\/li>\n<\/ul>\n<h3 id=\"5.init_server%20%E5%87%BD%E6%95%B0\">5.init_server \u51fd\u6570<\/h3>\n<p>int init_server(unsigned short port) {<br \/>\n    int sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    struct sockaddr_in servaddr;<br \/>\n    servaddr.sin_family &#061; AF_INET;<br \/>\n    servaddr.sin_addr.s_addr &#061; htonl(INADDR_ANY);<br \/>\n    servaddr.sin_port &#061; htons(port);<br \/>\n    if (-1 &#061;&#061; bind(sockfd, (struct sockaddr*)&amp;servaddr, sizeof(struct sockaddr))) {<br \/>\n        printf(&#034;bind failed: %s\\\\n&#034;, strerror(errno));<br \/>\n    }<br \/>\n    listen(sockfd, 10);<br \/>\n    return sockfd;<br \/>\n}<\/p>\n<p>init_server \u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u670d\u52a1\u5668&#xff1a;<\/p>\n<li>\u521b\u5efa\u4e00\u4e2a TCP \u5957\u63a5\u5b57\u3002<\/li>\n<li>\u5c06\u670d\u52a1\u5668\u7ed1\u5b9a\u5230\u6307\u5b9a\u7aef\u53e3\u3002<\/li>\n<li>\u5f00\u59cb\u76d1\u542c\u8fde\u63a5\u3002<\/li>\n<h3 id=\"6.main%20%E5%87%BD%E6%95%B0\">6.main \u51fd\u6570<\/h3>\n<p>int main() {<br \/>\n    unsigned short port &#061; 2000;<br \/>\n    epfd &#061; epoll_create(1);<br \/>\n    int i &#061; 0;<br \/>\n    for (i &#061; 0; i &lt; MAX_PORTS; i&#043;&#043;) {<br \/>\n        int sockfd &#061; init_server(port &#043; i);<br \/>\n        conn_list[sockfd].fd &#061; sockfd;<br \/>\n        conn_list[sockfd].r_action.recv_callback &#061; accept_cb;<br \/>\n        set_event(sockfd, EPOLLIN, 1);<br \/>\n    }<\/p>\n<p>    gettimeofday(&amp;begin, NULL);<\/p>\n<p>    while (1) {<br \/>\n        struct epoll_event events[1024] &#061; {0};<br \/>\n        int nready &#061; epoll_wait(epfd, events, 1024, -1);<br \/>\n        int i &#061; 0;<br \/>\n        for (i &#061; 0; i &lt; nready; i&#043;&#043;) {<br \/>\n            int connfd &#061; events[i].data.fd;<br \/>\n            if (events[i].events &amp; EPOLLIN) {<br \/>\n                conn_list[connfd].r_action.recv_callback(connfd);<br \/>\n            }<br \/>\n            if (events[i].events &amp; EPOLLOUT) {<br \/>\n                conn_list[connfd].send_callback(connfd);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>main \u51fd\u6570\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c&#xff1a;<\/p>\n<li>\u521b\u5efa\u4e00\u4e2a epoll \u5b9e\u4f8b\u3002<\/li>\n<li>\u4e3a\u591a\u4e2a\u7aef\u53e3&#xff08;port \u5230 port &#043; MAX_PORTS&#xff09;\u521d\u59cb\u5316\u670d\u52a1\u5668&#xff0c;\u5e76\u4e3a\u6bcf\u4e2a\u76d1\u542c\u5957\u63a5\u5b57\u6ce8\u518c EPOLLIN \u4e8b\u4ef6\u3002<\/li>\n<li>\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u7b49\u5f85\u548c\u5904\u7406\u4e8b\u4ef6&#xff08;\u901a\u8fc7 epoll_wait&#xff09;\u3002<\/li>\n<h3 id=\"7.%E6%80%BB%E7%BB%93%EF%BC%9A\">7.\u603b\u7ed3&#xff1a;<\/h3>\n<p>\u8be5\u7a0b\u5e8f\u4f7f\u7528 epoll \u8fdb\u884c\u9ad8\u6548\u7684\u591a\u8def\u590d\u7528\u7f51\u7edc I\/O&#xff0c;\u652f\u6301\u591a\u4e2a\u7aef\u53e3\u7684\u76d1\u542c\u3002\u5b83\u4f7f\u7528\u56de\u8c03\u673a\u5236\u6765\u5904\u7406\u6bcf\u4e2a\u8fde\u63a5\u7684\u63a5\u6536\u548c\u53d1\u9001\u64cd\u4f5c\u3002\u7a0b\u5e8f\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u5206\u914d\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u7ba1\u7406\u5176\u7f13\u51b2\u533a\u548c\u56de\u8c03\u51fd\u6570&#xff0c;\u901a\u8fc7 epoll \u5904\u7406\u5f02\u6b65 I\/O \u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%E5%9B%9B%E3%80%81%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98\">\u56db\u3001\u5e38\u89c1\u95ee\u9898<\/h2>\n<h3 id=\"1.%E9%BB%98%E8%AE%A4%E7%9A%84open%20files%E6%95%B0%E9%87%8F%E9%99%90%E5%88%B6%E4%B8%BA1024%E2%80%8B%E7%BC%96%E8%BE%91\">1.\u9ed8\u8ba4\u7684open files\u6570\u91cf\u9650\u5236\u4e3a1024<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"144\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be436eaa.png\" width=\"865\" \/><\/h3>\n<h3 id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A\">\u89e3\u51b3\u65b9\u6848&#xff1a;<\/h3>\n<p>\u8f93\u5165<\/p>\n<p>ulimit -a<\/p>\n<p>\u53ef\u67e5\u770bopen files<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"552\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be44cd98.png\" width=\"865\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u73b0\u5728\u6700\u591a\u5efa\u7acb1024\u4e2a\u8fde\u63a5<\/p>\n<p>\u8f93\u5165<\/p>\n<p>ulimit -n 1048576<\/p>\n<p>\u53ef\u4fee\u6539open files<\/p>\n<p>\u6b64\u65f6\u518d\u8f93\u5165<\/p>\n<p>ulimit -a<\/p>\n<p>\u53ef\u4ee5\u770b\u5230&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"42\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be47b148.png\" width=\"865\" \/><\/p>\n<p>\u5c06\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u7684open files\u90fd\u8bbe\u7f6e\u4e3a1048576,\u8fd9\u662f\u5b9e\u73b0\u767e\u4e07\u7ea7\u5e76\u53d1\u7684\u57fa\u7840<\/p>\n<h3 id=\"2.%E4%B8%8D%E8%83%BD%E5%88%86%E9%85%8D%E5%9C%B0%E5%9D%80\">2.\u4e0d\u80fd\u5206\u914d\u5730\u5740<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"356\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be489f37.png\" width=\"865\" \/><\/p>\n<p style=\"margin-left:0;text-align:justify\">\u539f\u56e0\u662f&#xff1a;\u4e94\u5143\u7ec4\u7684\u6570\u91cf\u4e0d\u591f<\/p>\n<p style=\"margin-left:0;text-align:justify\"><span style=\"color:#fe2c24\">\u4e94\u5143\u7ec4(sip,dip, sport, dport, proto)<\/span>\u6e90ip&#xff08;\u672c\u5730ip&#xff09;&#xff0c;\u76ee\u7684ip&#xff08;\u8fdc\u7a0bip&#xff09;&#xff0c;\u6e90\u7aef\u53e3&#xff08;\u672c\u673a\u7aef\u53e3&#xff09;&#xff0c;\u76ee\u7684\u7aef\u53e3&#xff08;\u8fdc\u7a0b\u7aef\u53e3&#xff09;&#xff0c;\u534f\u8bae<\/p>\n<p style=\"margin-left:0;text-align:justify\">eg:<\/p>\n<p style=\"margin-left:0;text-align:justify\"><span style=\"color:#9c8ec1\">192.168.127.128sip<\/span><\/p>\n<p style=\"margin-left:0;text-align:justify\"><span style=\"color:#9c8ec1\">192.168.127.129dip<\/span><\/p>\n<p style=\"margin-left:0;text-align:justify\"><span style=\"color:#9c8ec1\">\u5efa\u7acb\u8fde\u63a527999\u4e2a&#xff0c;\u5360\u7528\u7aef\u53e31024-29,023<\/span><\/p>\n<h3 id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A%E5%BB%BA%E7%AB%8B%E5%A4%9A%E4%B8%AAserver(%E6%8F%90%E4%BE%9Bsport)\" style=\"margin-left:0px;text-align:justify\">\u89e3\u51b3\u65b9\u6848&#xff1a;\u5efa\u7acb\u591a\u4e2aserver(\u63d0\u4f9bsport)<\/h3>\n<p>\u5bf9\u5e94main\u51fd\u6570\u8fd9\u6bb5\u4ee3\u7801&#xff1a;<\/p>\n<p>#define MAX_PORTS20<\/p>\n<p>int i &#061; 0;<\/p>\n<p>for (i &#061; 0;i &lt; MAX_PORTS;i &#043;&#043;) {<\/p>\n<p>int sockfd &#061; init_server(port &#043; i);<\/p>\n<p>conn_list[sockfd].fd &#061; sockfd;<br \/>\nconn_list[sockfd].r_action.recv_callback &#061; accept_cb;<br \/>\nset_event(sockfd, EPOLLIN, 1);<\/p>\n<p>} <\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u89e3\u51b3\u4ee5\u524d&#xff0c;\u670d\u52a1\u7aef\u4ee3\u7801\u662f\u53ea\u8c03\u7528\u4e86\u4e00\u4e2a\u7aef\u53e3\u7684<\/p>\n<h3 id=\"3.%E7%B3%BB%E7%BB%9F%E7%89%88%E6%9C%AC%E5%AF%BC%E8%87%B4%E7%9A%84%E9%97%AE%E9%A2%98\">3.\u7cfb\u7edf\u7248\u672c\u5bfc\u81f4\u7684\u95ee\u9898<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"769\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be4acf19.png\" width=\"865\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"260\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be4e7a40.png\" width=\"865\" \/><\/p>\n<p style=\"margin-left:0;text-align:justify\">\u8fd9\u4e2a\u7248\u672c\u7684ubuntu\u5728\u5904\u7406\u7f51\u7edc\u9ad8\u5e76\u53d1\u65f6\u5b58\u5728\u95ee\u9898<\/p>\n<h3 style=\"margin-left:0px;text-align:justify\">\u89e3\u51b3\u65b9\u6848&#xff1a;<\/h3>\n<p style=\"margin-left:0;text-align:justify\">\u4fee\u6539\u914d\u7f6e\u6587\u4ef6 \/etc\/sysctl.conf&#xff0c;\u5728\u8fd9\u4e2a\u6587\u4ef6\u7684\u7ed3\u5c3e\u52a0\u4e0a<\/p>\n<p style=\"margin-left:0;text-align:justify\">net.ipv4.tcp_syn_retries &#061; 5 net.ipv4.tcp_syncookies &#061; 1 net.ipv4.tcp_mem &#061; 262144 786432 786432 net.ipv4.tcp_wmem &#061; 1024 1024 2048 net.ipv4.tcp_rmem &#061; 1024 1024 2048 fs.file-max &#061; 1048576 net.nf_conntrack_max &#061; 1048576 net.netfilter.nf_conntrack_tcp_timeout_established &#061; 1200<\/p>\n<p style=\"margin-left:0;text-align:justify\">Linux\u7ec8\u7aef\u4e2d\u8f93\u5165<\/p>\n<p style=\"margin-left:0;text-align:justify\">sudo vim \/etc\/sysctl.conf<\/p>\n<p style=\"margin-left:0;text-align:justify\">\u8fdb\u5165\u914d\u7f6e\u6587\u4ef6&#xff0c;\u5e76\u5c06\u4e0a\u9762\u7684\u5185\u5bb9\u8f93\u5165&#xff0c;\u7136\u540e\u6309\u00a0ESC -&gt; ctrl &#043; : -&gt; wq \u4fdd\u5b58\u5e76\u9000\u51fa<\/p>\n<p style=\"margin-left:0;text-align:justify\">\u518d\u6309\u7167\u4e0b\u56fe\u6267\u884c\u56db\u6761\u6307\u4ee4<\/p>\n<p style=\"margin-left:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"396\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114749-68023be50fafc.png\" width=\"923\" \/><\/p>\n<p style=\"margin-left:0;text-align:justify\">\u82e5\u8f93\u51fa\u5982\u56fe&#xff0c;\u5219\u8bf4\u660e\u95ee\u9898\u5df2\u7ecf\u89e3\u51b3\u3002<\/p>\n<p style=\"margin-left:0;text-align:justify\">\u8bb0\u5f97\u5c06\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u90fd\u6309\u7167\u4ee5\u4e0a\u65b9\u6cd5\u914d\u7f6e<\/p>\n<h2 id=\"%E4%BA%94%E3%80%81%E7%99%BE%E4%B8%87%E7%BA%A7%E5%B9%B6%E5%8F%91%E7%BB%93%E6%9E%9C%E5%B1%95%E7%A4%BA\" style=\"margin-left:0px;text-align:justify\">\u4e94\u3001\u767e\u4e07\u7ea7\u5e76\u53d1\u7ed3\u679c\u5c55\u793a<\/h2>\n<p style=\"margin-left:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1023\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114749-68023be532035.png\" width=\"582\" \/><\/p>\n<hr \/>\n<h2 id=\"%E6%80%BB%E7%BB%93\">\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u57fa\u4e8ereactor\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u5b9e\u73b0\u4e86\u670d\u52a1\u5668\u767e\u4e07\u7ea7\u5e76\u53d1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.7k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf47\u6b21\u3002\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u57fa\u4e8e\u672c\u7cfb\u5217\u4e0a\u4e00\u7bc7\u300a\u7f51\u7edc\u7f16\u7a0b(\u4e94)\u300b\u7684reactor\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u505a\u4e86\u4e00\u4e9b\u6539\u8fdb\u800c\u6210\u3002\uff1a\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u3002\uff1a\u6700\u5927\u8fde\u63a5\u6570\u3002MAX_PORTS\uff1a\u6700\u5927\u76d1\u542c\u7684\u7aef\u53e3\u6570\u3002\u5b8f\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u7c7b\u578b\u7684\u65f6\u95f4\u5dee\uff08\u5355\u4f4d\u4e3a\u6beb\u79d2\uff09\u3002RCALLBACK\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7c7b\u578b\uff0c\u8868\u793a\u56de\u8c03\u51fd\u6570\u3002int fd;conn\u7ed3\u6784\u4f53\u7528\u4e8e\u7ba1\u7406\u6bcf\u4e2a\u8fde\u63a5\u7684\u72b6\u6001\u3002\u5b83\u5305\u542b\u4e86\u4e0e\u8fde\u63a5\u76f8\u5173\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u6bd4\u5982\u8bfb\u53d6\u7f13\u51b2\u533a\u3001\u5199\u5165\u7f13\u51b2\u533a\u3001\u8bfb\u53d6\u548c\u5199\u5165\u7684\u6570\u636e\u957f\u5ea6\u3001\u56de\u8c03\u51fd\u6570\u7b49\u3002_reactor\u6a21\u578b c++\u7f51\u7edc\u7f16\u7a0b<\/p>\n","protected":false},"author":2,"featured_media":12716,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,43,78],"topic":[],"class_list":["post-12724","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 - \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\/12724.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.7k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf47\u6b21\u3002\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u57fa\u4e8e\u672c\u7cfb\u5217\u4e0a\u4e00\u7bc7\u300a\u7f51\u7edc\u7f16\u7a0b(\u4e94)\u300b\u7684reactor\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u505a\u4e86\u4e00\u4e9b\u6539\u8fdb\u800c\u6210\u3002\uff1a\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u3002\uff1a\u6700\u5927\u8fde\u63a5\u6570\u3002MAX_PORTS\uff1a\u6700\u5927\u76d1\u542c\u7684\u7aef\u53e3\u6570\u3002\u5b8f\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u7c7b\u578b\u7684\u65f6\u95f4\u5dee\uff08\u5355\u4f4d\u4e3a\u6beb\u79d2\uff09\u3002RCALLBACK\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7c7b\u578b\uff0c\u8868\u793a\u56de\u8c03\u51fd\u6570\u3002int fd;conn\u7ed3\u6784\u4f53\u7528\u4e8e\u7ba1\u7406\u6bcf\u4e2a\u8fde\u63a5\u7684\u72b6\u6001\u3002\u5b83\u5305\u542b\u4e86\u4e0e\u8fde\u63a5\u76f8\u5173\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u6bd4\u5982\u8bfb\u53d6\u7f13\u51b2\u533a\u3001\u5199\u5165\u7f13\u51b2\u533a\u3001\u8bfb\u53d6\u548c\u5199\u5165\u7684\u6570\u636e\u957f\u5ea6\u3001\u56de\u8c03\u51fd\u6570\u7b49\u3002_reactor\u6a21\u578b c++\u7f51\u7edc\u7f16\u7a0b\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/12724.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T11:47:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be436eaa.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=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/12724.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/12724.html\",\"name\":\"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T11:47:50+00:00\",\"dateModified\":\"2025-04-18T11:47:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/12724.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/12724.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/12724.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668\"}]},{\"@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":"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 - \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\/12724.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.7k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf47\u6b21\u3002\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u57fa\u4e8e\u672c\u7cfb\u5217\u4e0a\u4e00\u7bc7\u300a\u7f51\u7edc\u7f16\u7a0b(\u4e94)\u300b\u7684reactor\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u505a\u4e86\u4e00\u4e9b\u6539\u8fdb\u800c\u6210\u3002\uff1a\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u3002\uff1a\u6700\u5927\u8fde\u63a5\u6570\u3002MAX_PORTS\uff1a\u6700\u5927\u76d1\u542c\u7684\u7aef\u53e3\u6570\u3002\u5b8f\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u7c7b\u578b\u7684\u65f6\u95f4\u5dee\uff08\u5355\u4f4d\u4e3a\u6beb\u79d2\uff09\u3002RCALLBACK\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7c7b\u578b\uff0c\u8868\u793a\u56de\u8c03\u51fd\u6570\u3002int fd;conn\u7ed3\u6784\u4f53\u7528\u4e8e\u7ba1\u7406\u6bcf\u4e2a\u8fde\u63a5\u7684\u72b6\u6001\u3002\u5b83\u5305\u542b\u4e86\u4e0e\u8fde\u63a5\u76f8\u5173\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u6bd4\u5982\u8bfb\u53d6\u7f13\u51b2\u533a\u3001\u5199\u5165\u7f13\u51b2\u533a\u3001\u8bfb\u53d6\u548c\u5199\u5165\u7684\u6570\u636e\u957f\u5ea6\u3001\u56de\u8c03\u51fd\u6570\u7b49\u3002_reactor\u6a21\u578b c++\u7f51\u7edc\u7f16\u7a0b","og_url":"https:\/\/www.wsisp.com\/helps\/12724.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T11:47:50+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418114748-68023be436eaa.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/12724.html","url":"https:\/\/www.wsisp.com\/helps\/12724.html","name":"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T11:47:50+00:00","dateModified":"2025-04-18T11:47:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/12724.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/12724.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/12724.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7f51\u7edc\u7f16\u7a0b(\u516d)\uff1aC++\u5b9e\u73b0\u57fa\u4e8ereactor\u7684\u767e\u4e07\u7ea7\u5e76\u53d1\u670d\u52a1\u5668"}]},{"@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\/12724","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=12724"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/12724\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/12716"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=12724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=12724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=12724"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=12724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}