{"id":774,"date":"2024-11-07T15:18:20","date_gmt":"2024-11-07T07:18:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/774\/"},"modified":"2024-11-07T15:18:20","modified_gmt":"2024-11-07T07:18:20","slug":"c%e8%af%ad%e8%a8%80%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e4%b8%ad%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e7%9a%84%e7%96%91%e9%9a%be%e8%a7%a3%e7%ad%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/774\/","title":{"rendered":"C\u8bed\u8a00\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5e76\u53d1\u7f16\u7a0b\u7684\u7591\u96be\u89e3\u7b54"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1132\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172852560935099.jpg\" width=\"720\" height=\"288\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172852560935099.jpg 720w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172852560935099-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172852560935099-670x268.jpg 670w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" title=\"C\u8bed\u8a00\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5e76\u53d1\u7f16\u7a0b\u7684\u7591\u96be\u89e3\u7b54\u63d2\u56fe\" alt=\"C\u8bed\u8a00\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5e76\u53d1\u7f16\u7a0b\u7684\u7591\u96be\u89e3\u7b54\u63d2\u56fe\" \/><\/p>\n<p><strong>C\u8bed\u8a00\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u7591\u96be\u89e3\u7b54<\/strong><\/p>\n<p>\u5e76\u53d1\u7f16\u7a0b\u662f\u6784\u5efa\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u548c\u5f02\u6b65I\/O\u6765\u5b9e\u73b0\u5e76\u53d1\u6027\u3002\u7136\u800c\uff0c\u5e76\u53d1\u7f16\u7a0b\u4e5f\u4f1a\u5e26\u6765\u4e00\u4e9b\u72ec\u7279\u7684\u6311\u6218\u548c\u7591\u96be\u95ee\u9898\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n<p>\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u53d1\u751f\u5728\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u65f6\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u6765\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cC\u8bed\u8a00\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<p><strong>\u6b7b\u9501<\/strong><\/p>\n<p>\u6b7b\u9501\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u4e14\u6c38\u8fdc\u65e0\u6cd5\u5b8c\u6210\u65f6\u3002\u4e3a\u4e86\u907f\u514d\u6b7b\u9501\uff0c\u9700\u8981\u5c0f\u5fc3\u7ba1\u7406\u8d44\u6e90\u548c\u907f\u514d\u5faa\u73af\u7b49\u5f85\u3002<\/p>\n<p><strong>\u7ade\u4e89\u6761\u4ef6<\/strong><\/p>\n<p>\u7ade\u4e89\u6761\u4ef6\u53d1\u751f\u5728\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5171\u4eab\u6570\u636e\u65f6\uff0c\u5bfc\u81f4\u4e0d\u786e\u5b9a\u7684\u7ed3\u679c\u3002\u4e3a\u4e86\u89e3\u51b3\u7ade\u4e89\u6761\u4ef6\uff0c\u53ef\u4ee5\u91c7\u7528\u539f\u5b50\u64cd\u4f5c\u3001\u4e92\u65a5\u9501\u6216\u65e0\u9501\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1aWeb\u670d\u52a1\u5668<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2aWeb\u670d\u52a1\u5668\u7684\u4f8b\u5b50\uff0c\u5b83\u9700\u8981\u5904\u7406\u6765\u81ea\u591a\u4e2a\u5ba2\u6237\u7aef\u7684\u5e76\u884c\u8bf7\u6c42\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u7ebf\u7a0bWeb\u670d\u52a1\u5668\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;pthread.h&gt;\n#include &lt;sys&gt;\n#include &lt;netinet&gt;\n\ntypedef struct thread_data {\n    int client_socket;\n} thread_data_t;\n\nvoid* thread_func(void* arg) {\n    thread_data_t* data = (thread_data_t*)arg;\n    \/\/ \u5904\u7406\u8bf7\u6c42\u5e76\u53d1\u9001\u54cd\u5e94\n    close(data-&amp;gt;client_socket);\n    free(data);\n    return NULL;\n}\n\nint main() {\n    int server_socket = socket(AF_INET, SOCK_STREAM, 0);\n    if (server_socket == -1) {\n        perror(\"socket\");\n        return EXIT_FAILURE;\n    }\n\n    struct sockaddr_in server_addr;\n    server_addr.sin_family = AF_INET;\n    server_addr.sin_port = htons(8080);\n    server_addr.sin_addr.s_addr = INADDR_ANY;\n    if (bind(server_socket, (struct sockaddr*)&amp;amp;server_addr, sizeof(server_addr)) == -1) {\n        perror(\"bind\");\n        return EXIT_FAILURE;\n    }\n\n    if (listen(server_socket, 10) == -1) {\n        perror(\"listen\");\n        return EXIT_FAILURE;\n    }\n\n    while (1) {\n        struct sockaddr_in client_addr;\n        socklen_t client_addr_len = sizeof(client_addr);\n        int client_socket = accept(server_socket, (struct sockaddr*)&amp;amp;client_addr, &amp;amp;client_addr_len);\n        if (client_socket == -1) {\n            perror(\"accept\");\n            continue;\n        }\n\n        pthread_t thread;\n        thread_data_t* data = malloc(sizeof(thread_data_t));\n        data-&amp;gt;client_socket = client_socket;\n        if (pthread_create(&amp;amp;thread, NULL, thread_func, data) != 0) {\n            perror(\"pthread_create\");\n            close(client_socket);\n            free(data);\n        }\n    }\n\n    close(server_socket);\n    return EXIT_SUCCESS;\n}&lt;\/netinet&gt;&lt;\/sys&gt;&lt;\/pthread.h&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2aWeb\u670d\u52a1\u5668\u5728\u6536\u5230\u5ba2\u6237\u7aef\u8bf7\u6c42\u540e\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u6765\u5904\u7406\u8bf7\u6c42\u3002\u4f7f\u7528\u4e92\u65a5\u9501\u6765\u4fdd\u62a4\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\uff0c\u4ee5\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fC\u8bed\u8a00\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5e76\u53d1\u7f16\u7a0b\u7684\u7591\u96be\u89e3\u7b54\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C\u8bed\u8a00\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u7591\u96be\u89e3\u7b54 \u5e76\u53d1\u7f16\u7a0b\u662f\u6784\u5efa\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u548c\u5f02\u6b65I\/O\u6765\u5b9e\u73b0\u5e76\u53d1\u6027\u3002\u7136\u800c\uff0c\u5e76\u53d1\u7f16\u7a0b\u4e5f\u4f1a\u5e26\u6765\u4e00\u4e9b\u72ec\u7279\u7684\u6311\u6218\u548c\u7591\u96be\u95ee\u9898\u3002 \u7ebf\u7a0b\u5b89\u5168 \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u53d1\u751f\u5728\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u65f6\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u6765\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002 \u7acb\u5373\u5b66\u4e60\u201cC\u8bed\u8a00\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b \u6b7b\u9501 \u6b7b\u9501\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u4e14\u6c38\u8fdc\u65e0\u6cd5\u5b8c\u6210\u65f6\u3002\u4e3a\u4e86\u907f\u514d\u6b7b\u9501\uff0c\u9700\u8981\u5c0f\u5fc3\u7ba1\u7406\u8d44\u6e90\u548c\u907f\u514d\u5faa\u73af\u7b49\u5f85\u3002 \u7ade\u4e89\u6761\u4ef6 \u7ade\u4e89\u6761\u4ef6\u53d1\u751f\u5728\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5171\u4eab\u6570\u636e\u65f6\uff0c\u5bfc\u81f4\u4e0d\u786e\u5b9a\u7684\u7ed3\u679c\u3002\u4e3a\u4e86\u89e3\u51b3\u7ade\u4e89\u6761\u4ef6\uff0c\u53ef\u4ee5\u91c7\u7528\u539f\u5b50\u64cd\u4f5c\u3001\u4e92\u65a5\u9501\u6216\u65e0\u9501\u6570\u636e\u7ed3\u6784\u3002 \u5b9e\u6218\u6848\u4f8b\uff1aWeb\u670d\u52a1\u5668 \u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2aWeb\u670d\u52a1\u5668\u7684\u4f8b\u5b50\uff0c\u5b83\u9700\u8981\u5904\u7406\u6765\u81ea\u591a\u4e2a\u5ba2\u6237\u7aef\u7684\u5e76\u884c\u8bf7\u6c42\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u7ebf\u7a0bWeb\u670d\u52a1\u5668\uff1a #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;pthread.h&gt; #include &lt;sys&gt; #include &lt;netinet&gt; typedef struct thread_data { int client_socket; } thread_data_t; void* thread_func(void* arg) { thread_data_t* data = (thread_data_t*)arg; \/\/ \u5904\u7406\u8bf7\u6c42\u5e76\u53d1\u9001\u54cd\u5e94 close(data-&amp;gt;client_socket); free(data); return NULL; } int main() { int server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-774","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=774"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/774\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}