{"id":59,"date":"2025-04-21T22:20:48","date_gmt":"2025-04-21T14:20:48","guid":{"rendered":"http:\/\/120.76.99.214\/?p=59"},"modified":"2025-11-24T16:43:24","modified_gmt":"2025-11-24T08:43:24","slug":"io%e5%a4%9a%e8%b7%af%e5%a4%8d%e7%94%a8%e5%ae%9e%e7%8e%b0%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%b9%b6%e5%8f%91%ef%bc%882%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=59","title":{"rendered":"IO\u591a\u8def\u590d\u7528\u5b9e\u73b0\u670d\u52a1\u5668\u5e76\u53d1\uff082\uff09"},"content":{"rendered":"\n<p>\u4f7f\u7528select\u8fdb\u884c\u8fdb\u884c\u5904\u7406<\/p>\n\n\n\n<p>     \u8fd9\u6b21\u535a\u4e3b\u5728\u4e0a\u4e00\u6b21\u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u884c\u4e86\u5347\u7ea7\uff0c\u4e0a\u4e2a\u7248\u672c\u662f\u4f7f\u7528\u5355\u4e2a\u7ebf\u7a0b\u5904\u7406\uff0c\u5f53\u5e76\u53d1\u91cf\u6bd4\u8f83\u9ad8\u7684\u65f6\u5019\uff0c\u6bd4\u8f83\u6162\uff0c\u4eca\u5929\u535a\u4e3b\u8fdb\u884c\u4e86\u5347\u7ea7\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5f53\u68c0\u6d4b\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26\u7b26\u5408\u6761\u4ef6\u65f6\u5c31\u4f1a\u5f00\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u53bb\u5904\u7406\uff0c\u7136\u540e\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u5904\u7406\u65f6\uff0c\u4e5f\u4f1a\u5f00\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u662f\u6700\u540e\u4e5f\u522b\u5fd8\u8bb0\u4e86\u7ebf\u7a0b\u540c\u6b65\uff0c\u907f\u514d\u6570\u636e\u7ade\u4e89\u5bfc\u81f4\u6570\u636e\u6df7\u4e71\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u901f\u5ea6\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u6539\u8fdb\u540e\u7684\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#include<pthread.h>\n#include<sys\/select.h>\npthread_mutex_t mutex;\ntypedef struct fdinfo\n{\n        int fd;\n        int *maxfd;\n        fd_set* redset;\n}FDInfo;\nvoid* acceptConn(void *arg){\n        std::cout<<\"pd_id: \"<<pthread_self()<<std::endl;\n        FDInfo *info=(FDInfo*)arg;\n        int cfd = accept(info->fd, NULL, NULL);\n        pthread_mutex_lock(&mutex);\n            if (cfd == -1) {\n                std::cout << \"accept error\" << std::endl;\n                exit(1);\n            }\n            std::cout << \"New client connected: \" << std::endl;\n            FD_SET(cfd, info->redset);\n            *info->maxfd=std::max(cfd,*info->maxfd);\n        pthread_mutex_unlock(&mutex);\n            free(info);\n            return NULL;\n}\nvoid *communication(void *arg){\n         std::cout<<\"pd_id:\"<<pthread_self()<<std::endl;\n          FDInfo *info=(FDInfo*)arg;\n         char buf[1024];\n\n                int len = recv(info->fd, buf, sizeof(buf), 0);\n                if (len == -1) {\n                    std::cout << \"recv error\" << std::endl;\n                    exit(1);\n                    free(info);\n                    return NULL;\n                } else if (len == 0) {\n                   pthread_mutex_lock(&#038;mutex);\n                    FD_CLR(info->fd, info->redset);\n                    close(info->fd);\n                    pthread_mutex_unlock(&mutex);\n                    std::cout << \"client is closed\" << std::endl;\n                    free(info);\n                    return NULL;\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              int ret = send(info->fd, buf, strlen(buf) + 1, 0);\n                if (ret == -1) {\n                    std::cout << \"send error\" << std::endl;\n                    free(info);\n                    return NULL;\n                }\n                free(info);\n                return NULL;\n\n}\nint main(int argc, const char* argv[]) {\n        pthread_mutex_init(&#038;mutex,NULL);\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(8888);\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   while (1) {\n        pthread_mutex_lock(&#038;mutex);\n        fd_set tmp = redset;\n        int ret = select(maxfd + 1, &#038;tmp, NULL, NULL, NULL);\n         pthread_mutex_unlock(&#038;mutex);\n        \/\/ \u5904\u7406\u76d1\u542c\u5957\u63a5\u5b57\n        if (FD_ISSET(lfd, &#038;tmp)) {\n\n            pthread_t tid;\n            FDInfo* info= new FDInfo();\n            info->fd=lfd;\n            info->maxfd = &maxfd;\n            info->redset=&redset;\n            pthread_create(&tid,NULL,acceptConn,info);\n            pthread_detach(tid);\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                pthread_t tid;\n                FDInfo* info= new FDInfo();\n                info->fd=i;\n                info->redset=&redset;\n                pthread_create(&tid,NULL,communication,info);\n                pthread_detach(tid);\n            }\n        }\n    }\n    pthread_mutex_destroy(&mutex);\n    close(lfd);\n    return 0;\n}\n                                                                                                                                                                                                                                                                                                                                                       \u4e0b\u9762\u662f\u8fd0\u884c\u7ed3\u679c\uff0c\u5ba2\u6237\u7aef\u4f1a\u53d1\u9001\u4e00\u4e2ajson\u6587\u4ef6\uff0c\u4ed6\u4f1a\u5168\u90e8\u8f6c\u5316\u4e3a\u5927\u5199\u8fd4\u56de\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"418\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/04\/image-7-1024x418.png\" alt=\"\" class=\"wp-image-60\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-7-1024x418.png 1024w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-7-300x123.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-7-768x314.png 768w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-7.png 1038w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u535a\u4e3b\u53d1\u9001\u4e86\u5bc6\u7801\u548c\u8d26\u53f7\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"659\" src=\"http:\/\/120.76.99.214\/wp-content\/uploads\/2025\/04\/image-8.png\" alt=\"\" class=\"wp-image-61\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-8.png 510w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/04\/image-8-232x300.png 232w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u5168\u90e8\u8f6c\u5316\u4e3a\u5927\u5199\u5e76\u53d1\u9001\u56de\u53bb\uff0c\u5f00\u4e86\u5927\u69828\u4e2a\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u4e86\u3002\u4e0b\u6b21\u89c1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528select\u8fdb\u884c\u8fdb\u884c\u5904\u7406 \u8fd9\u6b21\u535a\u4e3b\u5728\u4e0a\u4e00\u6b21\u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u884c\u4e86\u5347\u7ea7\uff0c\u4e0a\u4e2a\u7248\u672c\u662f\u4f7f\u7528\u5355\u4e2a\u7ebf\u7a0b\u5904\u7406\uff0c\u5f53\u5e76\u53d1\u91cf\u6bd4\u8f83\u9ad8&#8230;<\/p>\n","protected":false},"author":1,"featured_media":60,"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-59","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\/59","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=59"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":891,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/60"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}