{"id":37002,"date":"2025-05-13T08:11:03","date_gmt":"2025-05-13T00:11:03","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/37002.html"},"modified":"2025-05-13T08:11:03","modified_gmt":"2025-05-13T00:11:03","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%a4%9a%e5%ae%a2%e6%88%b7%e7%ab%af%e8%bf%9e%e6%8e%a5%e6%a0%b8%e5%bf%83%e8%a6%81%e7%82%b9%ef%bc%881%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/37002.html","title":{"rendered":"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09"},"content":{"rendered":"<p><span style=\"color:#956fe7\">\u5237\u9898<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"423\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250513001102-68228e165c65e.png\" width=\"543\" \/><\/p>\n<h2>\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9<\/h2>\n<hr \/>\n<h3>\u591a\u8fdb\u7a0b\u670d\u52a1\u5668<\/h3>\n<h4>\u5b9e\u73b0\u539f\u7406<\/h4>\n<ul>\n<li>fork\u5b50\u8fdb\u7a0b&#xff1a;\u6bcf\u6b21accept\u65b0\u5ba2\u6237\u7aef\u540e&#xff0c;\u8c03\u7528fork\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u72ec\u7acb\u5904\u7406&#xff1a;\u5b50\u8fdb\u7a0b\u8d1f\u8d23\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1&#xff08;\u5982read\/write&#xff09;&#xff0c;\u7236\u8fdb\u7a0b\u7ee7\u7eed\u76d1\u542c\u65b0\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<h4>\u7279\u70b9<\/h4>\n<ul>\n<li>\u9694\u79bb\u6027\u9ad8&#xff1a;\u8fdb\u7a0b\u95f4\u8d44\u6e90\u72ec\u7acb&#xff0c;\u907f\u514d\u6570\u636e\u7ade\u4e89\u3002<\/li>\n<li>\u8d44\u6e90\u6d88\u8017\u5927&#xff1a;\u9891\u7e41\u521b\u5efa\u8fdb\u7a0b\u5bfc\u81f4\u5185\u5b58\u548cCPU\u5f00\u9500\u9ad8\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u591a\u7ebf\u7a0b\u670d\u52a1\u5668<\/h3>\n<h4>\u5b9e\u73b0\u539f\u7406<\/h4>\n<ul>\n<li>\u521b\u5efa\u65b0\u7ebf\u7a0b&#xff1a;accept\u540e\u751f\u6210\u7ebf\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/li>\n<li>\u7ebf\u7a0b\u51fd\u6570&#xff1a;\u5728\u7ebf\u7a0b\u5185\u6267\u884cread\/write\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h4>\u7279\u70b9<\/h4>\n<ul>\n<li>\u8f7b\u91cf\u7ea7&#xff1a;\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u8d44\u6e90&#xff0c;\u5f00\u9500\u4f4e\u4e8e\u8fdb\u7a0b\u3002<\/li>\n<li>\u9700\u540c\u6b65\u673a\u5236&#xff1a;\u9700\u4f7f\u7528\u4e92\u65a5\u9501&#xff08;mutex&#xff09;\u6216\u4fe1\u53f7\u91cf\u907f\u514d\u6570\u636e\u7ade\u4e89\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u591a\u8defI\/O\u6a21\u578b<\/h3>\n<h4>\u6838\u5fc3\u95ee\u9898<\/h4>\n<ul>\n<li>\u963b\u585e\u51b2\u7a81&#xff1a;\u4f20\u7edfaccept\u548cread\u76f8\u4e92\u963b\u585e&#xff0c;\u65e0\u6cd5\u540c\u65f6\u5904\u7406\u591a\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\u901a\u8fc7\u975e\u963b\u585e\u6a21\u578b&#xff08;select\/poll&#xff09;\u7edf\u4e00\u76d1\u542c\u6240\u6709\u63cf\u8ff0\u7b26\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>Select\u6a21\u578b<\/h4>\n<h5>\u6d41\u7a0b<\/h5>\n<li>\u521d\u59cb\u5316\u76d1\u89c6\u5217\u8868&#xff1a;<\/li>\n<p>fd_set read_fds;<br \/>\nFD_ZERO(&amp;read_fds);          \/\/ \u6e05\u7a7a\u5217\u8868<br \/>\nFD_SET(server_fd, &amp;read_fds); \/\/ \u6dfb\u52a0\u670d\u52a1\u7aef\u5957\u63a5\u5b57 <\/p>\n<li>\u5faa\u73af\u76d1\u542c&#xff1a; select(max_fd &#043; 1, &amp;read_fds, NULL, NULL, NULL); \/\/ \u963b\u585e\u76f4\u81f3\u63cf\u8ff0\u7b26\u6fc0\u6d3b <\/li>\n<li>\u5904\u7406\u6fc0\u6d3b\u63cf\u8ff0\u7b26&#xff1a;\n<ul>\n<li>\u904d\u5386\u63cf\u8ff0\u7b26&#xff0c;\u901a\u8fc7FD_ISSET\u5224\u65ad\u662f\u5426\u6fc0\u6d3b\u3002<\/li>\n<li>\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u6fc0\u6d3b&#xff1a;\u8c03\u7528accept\u63a5\u53d7\u65b0\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u6fc0\u6d3b&#xff1a;\u8c03\u7528read\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>\u7f3a\u70b9<\/h5>\n<ul>\n<li>\u6548\u7387\u4f4e&#xff1a;\u6bcf\u6b21\u8c03\u7528\u9700\u904d\u5386\u6240\u6709\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>\u76d1\u89c6\u5217\u8868\u4e0a\u9650&#xff1a;\u9ed8\u8ba4\u9650\u52361024\u4e2a\u63cf\u8ff0\u7b26\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>Poll\u6a21\u578b<\/h4>\n<h5>\u6d41\u7a0b<\/h5>\n<li>\u521d\u59cb\u5316\u76d1\u89c6\u5217\u8868&#xff1a;<\/li>\n<p>struct pollfd fds[MAX_CLIENTS];<br \/>\nfds[0].fd &#061; server_fd;<br \/>\nfds[0].events &#061; POLLIN; \/\/ \u76d1\u89c6\u53ef\u8bfb\u4e8b\u4ef6 <\/p>\n<li>\u5faa\u73af\u76d1\u542c&#xff1a; poll(fds, nfds, -1); \/\/ \u6c38\u4e45\u963b\u585e\u76f4\u81f3\u4e8b\u4ef6\u89e6\u53d1 <\/li>\n<li>\u5904\u7406\u6fc0\u6d3b\u63cf\u8ff0\u7b26&#xff1a;\n<ul>\n<li>\u904d\u5386fds\u6570\u7ec4&#xff0c;\u68c0\u67e5revents\u662f\u5426\u4e3aPOLLIN\u3002<\/li>\n<li>\u670d\u52a1\u7aef\u6fc0\u6d3b&#xff1a;accept\u65b0\u5ba2\u6237\u7aef\u5e76\u52a0\u5165\u5217\u8868\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u6fc0\u6d3b&#xff1a;read\u6570\u636e\u5e76\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>\u4f18\u70b9<\/h5>\n<ul>\n<li>\u65e0\u63cf\u8ff0\u7b26\u6570\u91cf\u9650\u5236&#xff1a;\u652f\u6301\u52a8\u6001\u6269\u5c55\u3002<\/li>\n<li>\u4e8b\u4ef6\u7c7b\u578b\u7075\u6d3b&#xff1a;\u53ef\u540c\u65f6\u76d1\u89c6\u8bfb\u5199\u548c\u5f02\u5e38\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u6a21\u578b\u5bf9\u6bd4<\/h3>\n<table>\n<tr>\u7279\u6027SelectPoll<\/tr>\n<tbody>\n<tr>\n<td>\u63cf\u8ff0\u7b26\u4e0a\u9650<\/td>\n<td>1024&#xff08;\u7cfb\u7edf\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u65e0\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td>\u6548\u7387<\/td>\n<td>O(n)\u904d\u5386<\/td>\n<td>O(n)\u904d\u5386<\/td>\n<\/tr>\n<tr>\n<td>\u4e8b\u4ef6\u7c7b\u578b<\/td>\n<td>\u4ec5\u53ef\u8bfb\/\u53ef\u5199\/\u5f02\u5e38<\/td>\n<td>\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6&#xff08;\u5982POLLIN&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u5e73\u53f0\u652f\u6301<\/td>\n<td>\u5e7f\u6cdb\u652f\u6301<\/td>\n<td>Linux\u4e13\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u4f5c\u4e1a\u8981\u6c42&#xff1a;\n<ul>\n<li>\u670d\u52a1\u5668\u7aef&#xff1a;\u4f7f\u7528poll\u6a21\u578b\u5904\u7406\u591a\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u5ba2\u6237\u7aef1&#xff1a;select\u6a21\u578b\u5b9e\u73b0\u975e\u963b\u585e\u901a\u4fe1\u3002<\/li>\n<li>\u5ba2\u6237\u7aef2&#xff1a;\u591a\u7ebf\u7a0b\u5904\u7406\u8f93\u5165\/\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5178\u578b\u573a\u666f&#xff1a;\n<ul>\n<li>\u5b9e\u65f6\u804a\u5929\u7cfb\u7edf&#xff1a;\u670d\u52a1\u5668\u901a\u8fc7poll\u7ba1\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u4f1a\u8bdd\u3002<\/li>\n<li>\u9ad8\u5e76\u53d1\u670d\u52a1\u5668&#xff1a;select\/poll\u9002\u7528\u4e8e\u4f4e\u5e76\u53d1\u573a\u666f&#xff0c;epoll\u9002\u5408\u9ad8\u5e76\u53d1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>pollk.c #include &#034;head.h&#034;\n<p>#define MAX_CLIENTS 10 \/\/ \u6700\u591a10\u4e2a\u5ba2\u6237\u7aef<\/p>\n<p>\/\/ \u5c06\u4e00\u4e2a\u62e5\u6709{\u63cf\u8ff0\u7b26&#xff0c;\u76d1\u89c6\u65b9\u5f0f&#xff0c;\u6fc0\u6d3b\u5f62\u5f0f}\u7684\u7ed3\u6784\u4f53\u53d8\u91cffd&#xff0c;\u5b58\u5165\u76d1\u89c6\u5217\u8868arr\u4e2d&#xff0c;\u5e76\u4e14\u76d1\u89c6\u5217\u8868\u7684\u957f\u5ea6len\u81ea\u589e<br \/>\nvoid insert_fd(struct pollfd arr[], struct pollfd newfd, int* len) {<br \/>\n    arr[*len] &#061; newfd; \/\/ \u5c06\u65b0\u7684pollfd\u7ed3\u6784\u4f53\u5b58\u5165\u6570\u7ec4\u6307\u5b9a\u4f4d\u7f6e<br \/>\n    (*len)&#043;&#043;; \/\/ \u76d1\u89c6\u5217\u8868\u957f\u5ea6\u52a01<br \/>\n}<\/p>\n<p>\/\/ \u5c06\u8981\u79fb\u9664\u7684\u63cf\u8ff0\u7b26fd&#xff0c;\u4ece\u76d1\u89c6\u5217\u8868arr\u4e2d\u5220\u9664&#xff0c;\u5e76\u4e14\u76d1\u89c6\u5217\u8868\u957f\u5ea6len\u81ea\u51cf1<br \/>\nvoid remove_fd(struct pollfd* arr, int fd, int* len) {<br \/>\n    for (int i &#061; 0; i &lt; *len; i&#043;&#043;) {<br \/>\n        if (arr[i].fd &#061;&#061; fd) { \/\/ \u67e5\u627e\u8981\u79fb\u9664\u7684\u63cf\u8ff0\u7b26\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e<br \/>\n            for (int j &#061; i; j &lt; *len &#8211; 1; j&#043;&#043;) {<br \/>\n                arr[j] &#061; arr[j &#043; 1]; \/\/ \u540e\u9762\u7684\u5143\u7d20\u5411\u524d\u79fb\u52a8&#xff0c;\u8986\u76d6\u8981\u79fb\u9664\u7684\u5143\u7d20<br \/>\n            }<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    (*len)&#8211;; \/\/ \u76d1\u89c6\u5217\u8868\u957f\u5ea6\u51cf1<br \/>\n}<\/p>\n<p>int main(int argc, const char *argv[]) {<br \/>\n    if (argc &lt; 2) { \/\/ \u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u786e\u4fdd\u8f93\u5165\u4e86\u7aef\u53e3\u53f7<br \/>\n        printf(&#034;\u8bf7\u8f93\u5165\u7aef\u53e3\u53f7\\\\n&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    short port &#061; atoi(argv[1]); \/\/ \u5c06\u547d\u4ee4\u884c\u8f93\u5165\u7684\u7aef\u53e3\u53f7\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3ashort\u7c7b\u578b<\/p>\n<p>    \/\/ \u521b\u5efaTCP\u7c7b\u578b\u7684\u670d\u52a1\u5668\u5957\u63a5\u5b57<br \/>\n    int server &#061; socket(AF_INET, SOCK_STREAM, 0);<\/p>\n<p>    struct sockaddr_in addr &#061; {0};<br \/>\n    addr.sin_family &#061; AF_INET; \/\/ \u8bbe\u7f6e\u5730\u5740\u65cf\u4e3aIPv4<br \/>\n    addr.sin_port &#061; htons(port); \/\/ \u5c06\u7aef\u53e3\u53f7\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n    addr.sin_addr.s_addr &#061; inet_addr(&#034;0.0.0.0&#034;); \/\/ \u76d1\u542c\u6240\u6709\u53ef\u7528\u7684\u672c\u5730IP\u5730\u5740<\/p>\n<p>    \/\/ \u5c06\u5957\u63a5\u5b57\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u5730\u5740\u548c\u7aef\u53e3<br \/>\n    if (bind(server, (struct sockaddr*)&amp;addr, sizeof(addr)) &#061;&#061; -1) {<br \/>\n        perror(&#034;bind&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    listen(server, MAX_CLIENTS); \/\/ \u5f00\u59cb\u76d1\u542c&#xff0c;\u8bbe\u7f6e\u6700\u5927\u7b49\u5f85\u8fde\u63a5\u6570<\/p>\n<p>    struct pollfd list[MAX_CLIENTS &#043; 1] &#061; {0}; \/\/ \u5b9a\u4e49pollfd\u7ed3\u6784\u4f53\u6570\u7ec4&#xff0c;\u7528\u4e8e\u5b58\u50a8\u8981\u76d1\u89c6\u7684\u63cf\u8ff0\u7b26\u53ca\u5176\u76f8\u5173\u4fe1\u606f<br \/>\n    int list_len &#061; 0; \/\/ \u8bb0\u5f55\u76d1\u89c6\u5217\u8868\u4e2d\u63cf\u8ff0\u7b26\u7684\u6570\u91cf<\/p>\n<p>    \/\/ \u51c6\u5907\u8981\u76d1\u89c6\u7684\u670d\u52a1\u5668\u63cf\u8ff0\u7b26&#xff08;\u5173\u6ce8\u8bfb\u4e8b\u4ef6&#xff09;<br \/>\n    struct pollfd poll_server &#061; {.fd &#061; server,.events &#061; POLLIN,.revents &#061; 0};<\/p>\n<p>    insert_fd(list, poll_server, &amp;list_len); \/\/ \u5c06\u670d\u52a1\u5668\u63cf\u8ff0\u7b26\u52a0\u5165\u76d1\u89c6\u5217\u8868<\/p>\n<p>    printf(&#034;\u670d\u52a1\u5668\u542f\u52a8&#xff0c;\u76d1\u542c\u7aef\u53e3 %d&#8230;\\\\n&#034;, port);<\/p>\n<p>    while (1) {<br \/>\n        \/\/ \u8c03\u7528poll\u51fd\u6570&#xff0c;\u963b\u585e\u7b49\u5f85\u76d1\u89c6\u5217\u8868\u4e2d\u7684\u63cf\u8ff0\u7b26\u6709\u4e8b\u4ef6\u53d1\u751f<br \/>\n        poll(list, list_len, -1);<\/p>\n<p>        for (int i &#061; 0; i &lt; list_len; i&#043;&#043;) {<br \/>\n            int fd &#061; list[i].fd;<br \/>\n            \/\/ \u68c0\u67e5\u63cf\u8ff0\u7b26\u7684revents\u662f\u5426\u6709\u8bfb\u4e8b\u4ef6&#xff08;POLLIN&#xff09;\u53d1\u751f<br \/>\n            if (list[i].revents &amp; POLLIN) {<br \/>\n                if (fd &#061;&#061; server) { \/\/ \u670d\u52a1\u5668\u63cf\u8ff0\u7b26\u6709\u8bfb\u4e8b\u4ef6&#xff0c;\u8bf4\u660e\u6709\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n                    struct sockaddr_in client_addr;<br \/>\n                    socklen_t client_addr_len &#061; sizeof(client_addr);<br \/>\n                    int client &#061; accept(server, (struct sockaddr *)&amp;client_addr, &amp;client_addr_len);<br \/>\n                    if (client &#061;&#061; -1) {<br \/>\n                        perror(&#034;accept&#034;);<br \/>\n                        continue;<br \/>\n                    }<br \/>\n                    printf(&#034;\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5: %d\\\\n&#034;, client);<br \/>\n                    \/\/ \u51c6\u5907\u8981\u76d1\u89c6\u7684\u65b0\u5ba2\u6237\u7aef\u63cf\u8ff0\u7b26&#xff08;\u5173\u6ce8\u8bfb\u4e8b\u4ef6&#xff09;<br \/>\n                    struct pollfd poll_client &#061; {.fd &#061; client,.events &#061; POLLIN,.revents &#061; 0};<br \/>\n                    insert_fd(list, poll_client, &amp;list_len); \/\/ \u5c06\u65b0\u5ba2\u6237\u7aef\u63cf\u8ff0\u7b26\u52a0\u5165\u76d1\u89c6\u5217\u8868<br \/>\n                } else { \/\/ \u5ba2\u6237\u7aef\u63cf\u8ff0\u7b26\u6709\u8bfb\u4e8b\u4ef6&#xff0c;\u8bf4\u660e\u5ba2\u6237\u7aef\u53d1\u9001\u4e86\u6570\u636e<br \/>\n                    char buf[1024] &#061; {0};<br \/>\n                    int res &#061; read(fd, buf, sizeof(buf));<br \/>\n                    if (res &lt;&#061; 0) { \/\/ \u5904\u7406\u5ba2\u6237\u7aef\u65ad\u5f00<br \/>\n                        if (res &#061;&#061; 0) {<br \/>\n                            printf(&#034;\u5ba2\u6237\u7aef %d \u65ad\u5f00\u8fde\u63a5\\\\n&#034;, fd);<br \/>\n                        } else {<br \/>\n                            perror(&#034;read&#034;);<br \/>\n                        }<br \/>\n                        close(fd);<br \/>\n                        remove_fd(list, fd, &amp;list_len); \/\/ \u4ece\u76d1\u89c6\u5217\u8868\u4e2d\u79fb\u9664\u5ba2\u6237\u7aef\u63cf\u8ff0\u7b26<br \/>\n                    } else { \/\/ \u8f6c\u53d1\u6570\u636e\u7ed9\u5176\u4ed6\u5ba2\u6237\u7aef<br \/>\n                        for (int j &#061; 0; j &lt; list_len; j&#043;&#043;) {<br \/>\n                            if (list[j].fd !&#061; fd &amp;&amp; list[j].fd !&#061; server) {<br \/>\n                                write(list[j].fd, buf, res);<br \/>\n                            }<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>selectk.c<\/p>\n<p> #include &#034;head.h&#034;<\/p>\n<p>int main(int argc, const char *argv[]) {<br \/>\n    if (argc &lt; 2) {<br \/>\n        printf(&#034;\u8bf7\u8f93\u5165\u7aef\u53e3\u53f7\\\\n&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    int port &#061; atoi(argv[1]);<\/p>\n<p>    int client_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    struct sockaddr_in addr &#061; {0};<br \/>\n    addr.sin_family &#061; AF_INET;<br \/>\n    addr.sin_port &#061; htons(port);<br \/>\n    addr.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<p>    if (connect(client_fd, (struct sockaddr *)&amp;addr, sizeof(addr)) &#061;&#061; -1) {<br \/>\n        perror(&#034;connect&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    fd_set rset;<br \/>\n    int max_fd &#061; client_fd;<\/p>\n<p>    while (1) {<br \/>\n        FD_ZERO(&amp;rset);<br \/>\n        FD_SET(client_fd, &amp;rset);<br \/>\n        FD_SET(0, &amp;rset); \/\/ \u76d1\u63a7\u6807\u51c6\u8f93\u5165<\/p>\n<p>        int ret &#061; select(max_fd &#043; 1, &amp;rset, NULL, NULL, NULL);<br \/>\n        if (ret &#061;&#061; -1) {<br \/>\n            perror(&#034;select&#034;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        if (FD_ISSET(0, &amp;rset)) { \/\/ \u5904\u7406\u952e\u76d8\u8f93\u5165<br \/>\n            char buf[1024] &#061; {0};<br \/>\n            fgets(buf, sizeof(buf), stdin);<br \/>\n            buf[strcspn(buf, &#034;\\\\n&#034;)] &#061; 0; \/\/ \u53bb\u9664\u6362\u884c\u7b26<br \/>\n            write(client_fd, buf, strlen(buf));<br \/>\n        }<\/p>\n<p>        if (FD_ISSET(client_fd, &amp;rset)) { \/\/ \u5904\u7406\u63a5\u6536\u6d88\u606f<br \/>\n            char buf[1024] &#061; {0};<br \/>\n            int res &#061; read(client_fd, buf, sizeof(buf));<br \/>\n            if (res &lt;&#061; 0) {<br \/>\n                if (res &#061;&#061; 0) {<br \/>\n                    printf(&#034;\u670d\u52a1\u5668\u65ad\u5f00\u8fde\u63a5\\\\n&#034;);<br \/>\n                } else {<br \/>\n                    perror(&#034;read&#034;);<br \/>\n                }<br \/>\n                close(client_fd);<br \/>\n                return 1;<br \/>\n            }<br \/>\n            printf(&#034;\u6536\u5230\u6d88\u606f: %s\\\\n&#034;, buf);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(client_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>xiank.c<\/p>\n<p> #include &#034;head.h&#034;<\/p>\n<p>void *send_msg(void *arg) {<br \/>\n    int client_fd &#061; *(int *)arg;<br \/>\n    while (1) {<br \/>\n        char buf[1024] &#061; {0};<br \/>\n        fgets(buf, sizeof(buf), stdin);<br \/>\n        buf[strcspn(buf, &#034;\\\\n&#034;)] &#061; 0; \/\/ \u53bb\u9664\u6362\u884c\u7b26<br \/>\n        write(client_fd, buf, strlen(buf));<br \/>\n    }<br \/>\n}<\/p>\n<p>void *recv_msg(void *arg) {<br \/>\n    int client_fd &#061; *(int *)arg;<br \/>\n    while (1) {<br \/>\n        char buf[1024] &#061; {0};<br \/>\n        int res &#061; read(client_fd, buf, sizeof(buf));<br \/>\n        if (res &lt;&#061; 0) { \/\/ \u5904\u7406\u670d\u52a1\u5668\u65ad\u5f00<br \/>\n            if (res &#061;&#061; 0) {<br \/>\n                printf(&#034;\u670d\u52a1\u5668\u65ad\u5f00\u8fde\u63a5\\\\n&#034;);<br \/>\n            } else {<br \/>\n                perror(&#034;read&#034;);<br \/>\n            }<br \/>\n            close(client_fd);<br \/>\n            pthread_exit(NULL);<br \/>\n        }<br \/>\n        printf(&#034;\u6536\u5230\u6d88\u606f: %s\\\\n&#034;, buf);<br \/>\n    }<br \/>\n}<\/p>\n<p>int main(int argc, const char *argv[]) {<br \/>\n    if (argc &lt; 2) {<br \/>\n        printf(&#034;\u8bf7\u8f93\u5165\u7aef\u53e3\u53f7\\\\n&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    int port &#061; atoi(argv[1]);<\/p>\n<p>    int client_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    struct sockaddr_in addr &#061; {0};<br \/>\n    addr.sin_family &#061; AF_INET;<br \/>\n    addr.sin_port &#061; htons(port);<br \/>\n    addr.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<p>    if (connect(client_fd, (struct sockaddr *)&amp;addr, sizeof(addr)) &#061;&#061; -1) {<br \/>\n        perror(&#034;connect&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    pthread_t send_tid, recv_tid;<br \/>\n    pthread_create(&amp;send_tid, NULL, send_msg, &amp;client_fd); \/\/ \u542f\u52a8\u53d1\u9001\u7ebf\u7a0b<br \/>\n    pthread_create(&amp;recv_tid, NULL, recv_msg, &amp;client_fd); \/\/ \u542f\u52a8\u63a5\u6536\u7ebf\u7a0b<\/p>\n<p>    pthread_join(send_tid, NULL);<br \/>\n    pthread_join(recv_tid, NULL);<\/p>\n<p>    close(client_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<p>\u603b\u7ed3&#xff1a;\u591a\u8fdb\u7a0b\/\u7ebf\u7a0b\u9002\u5408\u7b80\u5355\u573a\u666f&#xff0c;\u591a\u8defI\/O\u6a21\u578b&#xff08;select\/poll&#xff09;\u901a\u8fc7\u975e\u963b\u585e\u76d1\u542c\u63d0\u5347\u6548\u7387&#xff0c;\u9700\u6839\u636e\u5e76\u53d1\u9700\u6c42\u9009\u62e9\u6a21\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb929\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u591a\u8fdb\u7a0b\/\u7ebf\u7a0b\u9002\u5408\u7b80\u5355\u573a\u666f\uff0c\u591a\u8defI\/O\u6a21\u578b\uff08<\/p>\n","protected":false},"author":2,"featured_media":37001,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,190],"topic":[],"class_list":["post-37002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09 - \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\/37002.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb929\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u591a\u8fdb\u7a0b\/\u7ebf\u7a0b\u9002\u5408\u7b80\u5355\u573a\u666f\uff0c\u591a\u8defI\/O\u6a21\u578b\uff08\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/37002.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-13T00:11:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250513001102-68228e165c65e.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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37002.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/37002.html\",\"name\":\"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-13T00:11:03+00:00\",\"dateModified\":\"2025-05-13T00:11:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/37002.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/37002.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37002.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09\"}]},{\"@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":"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09 - \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\/37002.html","og_locale":"zh_CN","og_type":"article","og_title":"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb929\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u591a\u8fdb\u7a0b\/\u7ebf\u7a0b\u9002\u5408\u7b80\u5355\u573a\u666f\uff0c\u591a\u8defI\/O\u6a21\u578b\uff08","og_url":"https:\/\/www.wsisp.com\/helps\/37002.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-13T00:11:03+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250513001102-68228e165c65e.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/37002.html","url":"https:\/\/www.wsisp.com\/helps\/37002.html","name":"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-13T00:11:03+00:00","dateModified":"2025-05-13T00:11:03+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/37002.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/37002.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/37002.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u670d\u52a1\u5668\u591a\u5ba2\u6237\u7aef\u8fde\u63a5\u6838\u5fc3\u8981\u70b9\uff081\uff09"}]},{"@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\/37002","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=37002"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/37002\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/37001"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=37002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=37002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=37002"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=37002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}