{"id":51,"date":"2025-04-20T19:38:52","date_gmt":"2025-04-20T11:38:52","guid":{"rendered":"http:\/\/120.76.99.214\/?p=51"},"modified":"2025-11-24T16:43:30","modified_gmt":"2025-11-24T08:43:30","slug":"io%e5%a4%9a%e8%b7%af%e5%a4%8d%e7%94%a8%e4%bd%bf%e7%94%a8select%e5%ae%9e%e7%8e%b0%e6%9c%8d%e5%8a%a1%e7%ab%af%e7%9a%84%e5%b9%b6%e5%8f%91","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=51","title":{"rendered":"IO\u591a\u8def\u590d\u7528\u4f7f\u7528select\u5b9e\u73b0\u670d\u52a1\u7aef\u7684\u5e76\u53d1"},"content":{"rendered":"\n<p>\u4e00.\u5e95\u5c42\u539f\u7406\uff1a\u672c\u8d28\u662f\u4e0a\u59d4\u6258\u5185\u6838\u53bb\u68c0\u6d4b\u901a\u4fe1\u8fc7\u7a0b\u4e2d\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e00\u7cfb\u5217\u72b6\u6001<\/p>\n\n\n\n<p>\u4e8c.select \u51fd\u6570\u8be6\u89e3\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\u6211\u5728Ubuntu\u4e0a\u4f7f\u7528man\u67e5\u770b\u4e00\u4e0b\u5e2e\u52a9\u624b\u518c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"484\" height=\"175\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/04\/image-3.png\" alt=\"\" class=\"wp-image-52\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-3.png 484w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-3-300x108.png 300w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/figure>\n\n\n\n<p>\u51fd\u6570\u4e00\u5171\u6709\u4e94\u4e2a\u53c2\u6570\uff1a1.nfds:\u68c0\u6d4b\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u4e2d\u6700\u5927\u63cf\u8ff0\u7b26+1\uff1b<\/p>\n\n\n\n<p>                                     2.readfds:\u8bfb\u96c6\u5408<\/p>\n\n\n\n<p>                                    3.writefds:\u5199\u96c6\u5408<\/p>\n\n\n\n<p>                                    4.exceptfds:\u5f02\u5e38\u96c6\u5408<\/p>\n\n\n\n<p>                                    5.timeout:\u6700\u957f\u65f6\u95f4<\/p>\n\n\n\n<p>\u51fd\u6570\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fd_set<\/code> \u662f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\uff0c\u7528\u4e8e <code>select<\/code> \u51fd\u6570\u3002<\/li>\n\n\n\n<li><code>FD_ZERO<\/code> \u521d\u59cb\u5316\u96c6\u5408\uff0c\u6e05\u7a7a\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n\n\n\n<li><code>FD_SET<\/code> \u5c06\u76d1\u542c\u5957\u63a5\u5b57 <code>fd<\/code> \u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<p> <\/p>\n\n\n\n<p>\u670d\u52a1\u5668\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>include <stdio.h>\n#include <iostream>\n#include <unistd.h>\n#include <ctype.h>\n#include <stdlib.h>\n#include <sys\/types.h>\n#include <sys\/stat.h>\n#include <string.h>\n#include <arpa\/inet.h>\n\nint main(int argc, const char* argv[]) {\n    int lfd = socket(AF_INET, SOCK_STREAM, 0); \/\/ \u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57\n    if (lfd == -1) {\n        std::cout << \"socket error\" << std::endl;\n        exit(0);\n    }\n\n    struct sockaddr_in ser_addr;\n    memset(&#038;ser_addr, 0, sizeof(ser_addr));\n    ser_addr.sin_family = AF_INET;\n    ser_addr.sin_port = htons(9999);\n    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);\n\n    \/\/ \u7ed1\u5b9a\u7aef\u53e3\n    int ret = bind(lfd, (struct sockaddr*)&#038;ser_addr, sizeof(ser_addr));\n    if (ret == -1) {\n        std::cout << \"bind error\" << std::endl;\n        exit(1);\n    }\n\n    \/\/ \u76d1\u542c\n    ret = listen(lfd, 64);\n    if (ret == -1) {\n        std::cout << \"listen error\" << std::endl;\n        exit(1);\n    }\n\n    fd_set redset;\n    FD_ZERO(&#038;redset);\n    FD_SET(lfd, &#038;redset);\n    int maxfd = lfd;\n\n    while (1) {\n        fd_set tmp = redset;\n        int ret = select(maxfd + 1, &#038;tmp, NULL, NULL, NULL);\n\n        \/\/ \u5904\u7406\u76d1\u542c\u5957\u63a5\u5b57\n        if (FD_ISSET(lfd, &#038;tmp)) {\n            int cfd = accept(lfd, NULL, NULL);\n            if (cfd == -1) {\n                std::cout << \"accept error\" << std::endl;\n                exit(1);\n            }\n            FD_SET(cfd, &#038;redset);\n  if (cfd > maxfd) {\n                maxfd = cfd;\n            }\n        }\n\n        \/\/ \u5904\u7406\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\n        for (int i = 0; i <= maxfd; i++) {\n            if (i != lfd &#038;&#038; FD_ISSET(i, &#038;tmp)) {\n                char buf[1024];\n                int len = recv(i, buf, sizeof(buf), 0);\n                if (len == -1) {\n                    std::cout << \"recv error\" << std::endl;\n                    exit(1);\n                } else if (len == 0) {\n                    FD_CLR(i, &#038;redset);\n                    close(i);\n                    std::cout << \"client is closed\" << std::endl;\n                    break;\n                }\n\n                \/\/ \u8f6c\u6362\u4e3a\u5927\u5199\n                for (int j = 0; j < len; j++) {\n                    buf[j] = toupper(buf[j]);\n                    std::cout << \"alter buf: \" << buf[j] << std::endl;\n                }\n\n                ret = send(i, buf, strlen(buf) + 1, 0);\n                if (ret == -1) {\n                    std::cout << \"send error\" << std::endl;\n                }\n            }\n        }\n    }\n\n    close(lfd);\n    return 0;\n}\n\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"306\" height=\"70\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/04\/image-5.png\" alt=\"\" class=\"wp-image-54\" style=\"width:490px;height:auto\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-5.png 306w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-5-300x69.png 300w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/><\/figure>\n\n\n\n<p>\u7531\u4e8e\u6ca1\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u7a0b\u5e8f\u4e00\u76f4\u963b\u585e\uff0c\u73b0\u5728\u6211\u4eec\u627e\u4e00\u4e2a\u535a\u4e3b\u6700\u8fd1\u5199\u7684\u9879\u76ee\uff08\u540e\u7eed\u505a\u5b8c\u4e5f\u4f1a\u5728\u672c\u7f51\u7ad9\u4e0a\u5f00\u6e90\u548c\u53d1\u5e03\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"259\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/04\/image-6-1024x259.png\" alt=\"\" class=\"wp-image-55\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-6-1024x259.png 1024w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-6-300x76.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-6-768x194.png 768w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-6-1536x388.png 1536w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-6.png 1796w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u535a\u4e3b\u5728\u8fd9\u91cc\u8bbe\u7f6e\u5f53\u8fde\u63a5\u6210\u529f\u540e\u7a0b\u5e8f\u624d\u80fd\u8dd1\u8d77\u6765\uff0c\u663e\u7136\u535a\u4e3b\u5199\u7684\u5927\u6982\u6ca1\u5565\u95ee\u9898\u3002\u4eca\u5929\u7684\u6587\u7ae0\u5c31\u66f4\u65b0\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u5e95\u5c42\u539f\u7406\uff1a\u672c\u8d28\u662f\u4e0a\u59d4\u6258\u5185\u6838\u53bb\u68c0\u6d4b\u901a\u4fe1\u8fc7\u7a0b\u4e2d\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e00\u7cfb\u5217\u72b6\u6001 \u4e8c.select \u51fd\u6570\u8be6\u89e3\uff1a \u9996\u5148\u6211\u5728U&#8230;<\/p>\n","protected":false},"author":1,"featured_media":52,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":892,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/52"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}