{"id":44161,"date":"2025-06-17T16:00:41","date_gmt":"2025-06-17T08:00:41","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44161.html"},"modified":"2025-06-17T16:00:41","modified_gmt":"2025-06-17T08:00:41","slug":"c%e8%af%ad%e8%a8%80%e5%a4%9a%e8%bf%9b%e7%a8%8btcp%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44161.html","title":{"rendered":"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef"},"content":{"rendered":"<h2>\u4e00\u3001\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u7684\u521b\u5efa<\/h2>\n<h3>\u00a0 \u00a0 \u00a0 \u00a0 \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/h3>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;signal.h&gt;<\/p>\n<p>#define PORT 8080<br \/>\n#define MAX_CLIENTS 10<br \/>\n#define BUFFER_SIZE 1024<\/p>\n<p>\/\/ \u50f5\u5c38\u8fdb\u7a0b\u5904\u7406<br \/>\nvoid zombie_handler(int sig) {<br \/>\n    while (waitpid(-1, NULL, WNOHANG) &gt; 0);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int server_fd, new_socket;<br \/>\n    struct sockaddr_in address;<br \/>\n    int addrlen &#061; sizeof(address);<br \/>\n    char buffer[BUFFER_SIZE] &#061; {0};<\/p>\n<p>    \/\/ \u521b\u5efaTCP\u5957\u63a5\u5b57<br \/>\n    if ((server_fd &#061; socket(AF_INET, SOCK_STREAM, 0)) &#061;&#061; 0) {<br \/>\n        perror(&#034;socket failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6eSO_REUSEADDR<br \/>\n    int opt &#061; 1;<br \/>\n    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt))) {<br \/>\n        perror(&#034;setsockopt failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    address.sin_family &#061; AF_INET;<br \/>\n    address.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    address.sin_port &#061; htons(PORT);<\/p>\n<p>    \/\/ \u7ed1\u5b9a\u5957\u63a5\u5b57<br \/>\n    if (bind(server_fd, (struct sockaddr *)&amp;address, sizeof(address)) &lt; 0) {<br \/>\n        perror(&#034;bind failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u5f00\u59cb\u76d1\u542c<br \/>\n    if (listen(server_fd, MAX_CLIENTS) &lt; 0) {<br \/>\n        perror(&#034;listen failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    printf(&#034;Server listening on port %d\\\\n&#034;, PORT);<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u50f5\u5c38\u8fdb\u7a0b\u5904\u7406\u5668<br \/>\n    struct sigaction sa;<br \/>\n    sa.sa_handler &#061; zombie_handler;<br \/>\n    sigemptyset(&amp;sa.sa_mask);<br \/>\n    sa.sa_flags &#061; SA_RESTART;<br \/>\n    if (sigaction(SIGCHLD, &amp;sa, NULL) &#061;&#061; -1) {<br \/>\n        perror(&#034;sigaction failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    while (1) {<br \/>\n        \/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\n        if ((new_socket &#061; accept(server_fd, (struct sockaddr *)&amp;address, (socklen_t*)&amp;addrlen)) &lt; 0) {<br \/>\n            perror(&#034;accept failed&#034;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        printf(&#034;New connection from %s:%d\\\\n&#034;,<br \/>\n               inet_ntoa(address.sin_addr), ntohs(address.sin_port));<\/p>\n<p>        \/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u8fde\u63a5<br \/>\n        pid_t pid &#061; fork();<br \/>\n        if (pid &lt; 0) {<br \/>\n            perror(&#034;fork failed&#034;);<br \/>\n            close(new_socket);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        if (pid &#061;&#061; 0) { \/\/ \u5b50\u8fdb\u7a0b<br \/>\n            close(server_fd); \/\/ \u5173\u95ed\u4e0d\u9700\u8981\u7684\u76d1\u542c\u5957\u63a5\u5b57<\/p>\n<p>            \/\/ \u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42<br \/>\n            while (1) {<br \/>\n                memset(buffer, 0, BUFFER_SIZE);<br \/>\n                ssize_t bytes_read &#061; read(new_socket, buffer, BUFFER_SIZE &#8211; 1);<\/p>\n<p>                if (bytes_read &lt;&#061; 0) {<br \/>\n                    if (bytes_read &#061;&#061; 0)<br \/>\n                        printf(&#034;Client disconnected\\\\n&#034;);<br \/>\n                    else<br \/>\n                        perror(&#034;read error&#034;);<br \/>\n                    break;<br \/>\n                }<\/p>\n<p>                printf(&#034;Received: %s&#034;, buffer);<\/p>\n<p>                \/\/ \u5904\u7406\u8bf7\u6c42&#xff08;\u793a\u4f8b&#xff1a;\u56de\u663e&#xff09;<br \/>\n                char *response &#061; &#034;HTTP\/1.1 200 OK\\\\r\\\\n&#034;<br \/>\n                                 &#034;Content-Type: text\/plain\\\\r\\\\n&#034;<br \/>\n                                 &#034;Connection: close\\\\r\\\\n&#034;<br \/>\n                                 &#034;\\\\r\\\\n&#034;<br \/>\n                                 &#034;Hello from server!&#034;;<\/p>\n<p>                send(new_socket, response, strlen(response), 0);<br \/>\n            }<\/p>\n<p>            close(new_socket);<br \/>\n            exit(EXIT_SUCCESS);<br \/>\n        } else { \/\/ \u7236\u8fdb\u7a0b<br \/>\n            close(new_socket); \/\/ \u5173\u95ed\u4e0d\u9700\u8981\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h2 id=\"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u5b9e\u73b0\u6307\u5357\">\u4e8c\u3001\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u76f8\u5173\u77e5\u8bc6<\/h2>\n<li>\n<h3>\u8fdb\u7a0b\u7ba1\u7406<\/h3>\n<li>\n<h3>fork()\u00a0\u521b\u5efa\u5b50\u8fdb\u7a0b<\/h3>\n<p> #include &lt;unistd.h&gt;<\/p>\n<p>pid_t fork(void);<br \/>\n\/*<br \/>\n    \u6210\u529f\u8fd4\u56de\u8fdb\u7a0b ID&#xff0c; \u5931\u8d25\u8fd4\u56de -1\u3002<br \/>\n*\/ <\/p>\n<h4>\u00a0\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<\/li>\n<\/li>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\npid_t mypid &#061; fork();\/\/mypid \u8fd4\u56de\u503c\u4e3a\u5b50\u8fdb\u7a0bpid<br \/>\nif(mypid &#061;&#061; 0) \/\/ pid  &#061;&#061; 0 \u8868\u793a\u5b50\u8fdb\u7a0b<br \/>\nprintf(&#034;I am child process\\\\n&#034;);<br \/>\nelse \/\/\u7236\u8fdb\u7a0b<br \/>\n{<br \/>\nprintf(&#034;Child process ID is %d\\\\n&#034;, mypid);<br \/>\nsleep(30);<br \/>\n}<\/p>\n<p>if(mypid &#061;&#061; 0)<br \/>\nputs(&#034;END Child process&#034;);<br \/>\nelse<br \/>\nputs(&#034;end parent process&#034;);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>\/*<br \/>\n\u6b64\u5b50\u8fdb\u7a0b\u4e3a\u50f5\u5c38\u8fdb\u7a0b\u3002\u50f5\u5c38\u8fdb\u7a0b&#xff1a;\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5&#xff0c;\u4f46\u7236\u8fdb\u7a0b\u672a\u8c03\u7528wait&#xff08;&#xff09;\u51fd\u6570\u6216\u8005waitpid&#xff08;&#xff09;\u51fd\u6570\u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684\u7ec8\u6b62\u72b6\u6001\u3002<br \/>\n\u6b64\u51fd\u6570\u4e2d&#xff0c;if(pid &#061;&#061; 0)\u65f6&#xff0c;\u6267\u884c\u7684\u65f6\u5b50\u8fdb\u7a0b&#xff0c;else\u4ee3\u8868\u7236\u8fdb\u7a0b<br \/>\n\u6253\u5370mypid\u7684\u503c\u5373\u4e3a\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\u3002<br \/>\n*\/ <\/p>\n<p>\u6267\u884c\u60c5\u51b5&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"307\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250617080040-685120a8518bd.png\" width=\"1423\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5c06\u51fd\u6570\u6267\u884c\u8d77\u6765\u4e4b\u540e&#xff0c;\u53ef\u4ee5\u770b\u89c1\u5b50\u8fdb\u7a0b\u4ee5\u53ca\u6267\u884c\u5b8c\u6bd5&#xff0c;\u4f46\u662f\u7531\u4e8e\u7236\u8fdb\u7a0b\u672a\u8c03\u7528wait\u51fd\u6570\u6216\u8005waitpid\u51fd\u6570&#xff0c;\u6545\u5b50\u8fdb\u7a0b\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u5728\u7236\u8fdb\u7a0b\u6267\u884c\u671f\u95f4\u4e00\u76f4\u5b58\u5728&#xff0c;\u5982\u4e0a\u56fe\u5728Linux\u7cfb\u7edf\u5185ps \u67e5\u770b\u4fe1\u606f\u6240\u5c55\u793a&#xff0c;\u7236\u8fdb\u7a0bID\u4e3a2252&#xff0c;\u5b50\u8fdb\u7a0bID\u4e3a2253&#xff0c;\u5728\u7236\u8fdb\u7a0b\u5b58\u7eed\u671f\u95f4&#xff0c;\u5b50\u8fdb\u7a0b\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4e5f\u4e00\u76f4\u5b58\u5728\u3002<\/p>\n<h4 style=\"background-color:transparent\">\u00a0\u00a0SIGCHLD\u00a0\u4fe1\u53f7\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<p>#include &lt;signal.h&gt;<\/p>\n<p>void (*signal(int signo, void(*func)(int)))(int);<br \/>\n\/*<br \/>\n    \u5728\u4ea7\u751f\u4fe1\u53f7\u65f6\u8c03\u7528&#xff0c;\u8fd4\u56de\u4e4b\u524d\u6ce8\u518c\u7684\u51fd\u6570\u6307\u9488<br \/>\n    \u53c2\u6570\u4e3aint \u7c7b\u578b&#xff0c;\u8fd4\u56devoid \u7c7b\u578b\u7684\u51fd\u6570\u6307\u9488<br \/>\nsigno\u5e38\u7528\u53c2\u6570&#xff1a;{<br \/>\n    SIGINT (2) &#8211; \u4e2d\u65ad\u4fe1\u53f7 (Ctrl&#043;C)<br \/>\n    SIGSEGV (11) &#8211; \u6bb5\u9519\u8bef (\u65e0\u6548\u5185\u5b58\u8bbf\u95ee)<br \/>\n    SIGTERM (15) &#8211; \u7ec8\u6b62\u4fe1\u53f7 (kill\u9ed8\u8ba4)<br \/>\n    SIGCHLD (17) &#8211; \u5b50\u8fdb\u7a0b\u72b6\u6001\u6539\u53d8<br \/>\n    SIGALRM (14) &#8211; \u5b9a\u65f6\u5668\u5230\u671f<br \/>\n    }<br \/>\n*\/ <\/p>\n<p>\u4f7f\u7528\u793a\u4f8b&#xff1a;&#xff08;\u6ce8\u518c\u4fe1\u53f7\u548c\u5904\u7406\u51fd\u6570&#xff09;<\/p>\n<p>signal(SIGCHLD, myfunc);<br \/>\n\/\/\u5b50\u8fdb\u7a0b\u7ed3\u675f\u4fe1\u53f7\u4ea7\u751f\u5219\u8c03\u7528myfunc\u51fd\u6570\u3002 <\/p>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 alarm()\u51fd\u6570<\/h4>\n<p>#include &lt;unistd.h&gt;<\/p>\n<p>unsigned int alarm(unsigned int seconds);<br \/>\n\/\/\u8fd4\u56de0&#xff0c;\u6216\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u8dddSIGALRM\u4fe1\u53f7\u53d1\u751f\u6240\u5269\u65f6\u95f4\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5982\u679c\u8c03\u7528\u8be5\u51fd\u6570\u7684\u540c\u65f6\u4f20\u9012\u4e00\u4e2a\u6b63\u6574\u5f62\u53c2\u6570&#xff0c;\u76f8\u5e94\u65f6\u95f4\u540e&#xff08;\u4ee5\u79d2\u4e3a\u5355\u4f4d&#xff09;\u5c06\u4ea7\u751fSIGALRM\u4fe1\u53f7&#xff0c;\u82e5\u5411\u8be5\u51fd\u6570\u4f20\u9012 0&#xff0c;\u5219\u4e4b\u524d\u5bf9SIGALRM\u4fe1\u53f7\u7684\u9884\u7ea6\u5c06\u53d6\u6d88\u3002\u5982\u679c\u901a\u8fc7\u8be5\u51fd\u6570\u9884\u7ea6\u4fe1\u53f7\u540e\u672a\u6307\u5b9a\u8be5\u4fe1\u53f7\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570&#xff0c;\u5219&#xff08;\u901a\u8fc7\u8c03\u7528signal\u51fd\u6570&#xff09;\u7ec8\u6b62\u8fdb\u7a0b&#xff0c;\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002\u6ce8\u610f&#xff01;&#xff01;<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;signal.h&gt;<\/p>\n<p>void time_out(int sig)\/\/alarm\u51fd\u6570\u65f6\u95f4\u5230\u8fbe\u5904\u7406\u51fd\u6570<br \/>\n{<br \/>\nif(sig &#061;&#061; SIGALRM)<br \/>\n{<br \/>\nprintf(&#034;time_out\\\\n&#034;);<br \/>\n}<br \/>\nalarm(2);<br \/>\n}<\/p>\n<p>void func(int sig) \/\/ctrl&#043;c \u4fe1\u53f7\u5904\u7406\u51fd\u6570<br \/>\n{<br \/>\nif(sig &#061;&#061; SIGINT)<br \/>\nputs(&#034;CTRL &#043; C is prossed&#034;);<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\nint i;<br \/>\nsignal(SIGALRM, time_out);<br \/>\nsignal(SIGINT, func);<br \/>\nalarm(2);<br \/>\nfor(i &#061; 0;i&lt;3;i&#043;&#043;) \/\/\u6bcf\u969450s\u8f93\u51fa\u4e00\u6b21&#xff0c;\u7406\u8bba\u7a0b\u5e8f\u4f1a\u6267\u884c150s\u3002<br \/>\n{<br \/>\nputs(&#034;loading&gt;&gt;&gt;&gt;&#034;);<br \/>\nsleep(50);<br \/>\n}<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u8f93\u51fa\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"538\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250617080040-685120a87d03e.png\" width=\"509\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u53ef\u89c1&#xff0c;\u4e0a\u8ff0\u7a0b\u5e8f\u5b9e\u9645\u8fd0\u884c\u65f6\u95f4\u4e0d\u5230\u5341\u79d2&#xff0c;\u5982\u679c\u6309\u4e0bctrl&#043;c\u5219\u66f4\u5feb\u7ed3\u675f\u3002\u8fd9\u662f\u56e0\u4e3a\u201c\u53d1\u751f\u4fe1\u53f7\u65f6\u5c06\u5524\u9192\u7531\u4e8e\u8c03\u7528sleep\u51fd\u6570\u800c\u8fdb\u5165\u963b\u585e\u72b6\u6001\u7684\u8fdb\u7a0b\u3002\u201d<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5373&#xff1a;\u5982\u679c\u7a0b\u5e8f\u81ea\u7136\u6267\u884c&#xff0c;\u4e0d\u8f93\u5165ctrl&#043;c&#xff0c;\u5219\u7a0b\u5e8f\u6bcf\u4e24\u79d2\u4ea7\u751fSIGALR\u4fe1\u53f7&#xff0c;\u540c\u65f6\u5524\u9192sleep\u8fdb\u7a0b&#xff0c;\u5373\u9000\u51fasleep\u72b6\u6001&#xff0c;\u4e3b\u7a0b\u5e8f\u4e2dfor\u5faa\u73af\u6267\u884c\u4e86\u4e0a\u6b21&#xff0c;\u7a0b\u5e8f\u4e09\u6b21\u8fdb\u5165sleep&#xff0c;\u540c\u65f6\u6ca1\u4e24\u79d2\u4e5f\u88ab\u5524\u9192\u3002\u6240\u4ee5\u6bcf2s\u88ab\u5524\u9192\u7684\u65f6\u5019\u4e5f\u4f1a\u9000\u51fasleep&#xff0c;\u6240\u4ee5\u7a0b\u5e8f\u53ea\u4f1a\u8f93\u51fa\u5982\u56fe\u7b2c\u4e00\u79cd\u7ed3\u679c\u3002\u6309\u4e0bctrl&#043;c\u65f6\u4e5f\u4f1a\u4ea7\u751f\u4fe1\u53f7SIGINT\u4fe1\u53f7\u5524\u9192sleep\u7684\u8fdb\u7a0b&#xff0c;\u6240\u4ee5\u4e5f\u53ea\u4f1a\u8f93\u51fa\u4e09\u6b21\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u603b\u7684\u6765\u8bf4&#xff0c;\u7a0b\u5e8f\u6709\u4e09\u6b21\u5faa\u73af&#xff0c;\u800c\u6bcf\u6b21\u4fe1\u53f7\u7684\u4ea7\u751f\u90fd\u4f1a\u6253\u65adsleep\u72b6\u6001&#xff0c;\u5524\u9192\u8fdb\u7a0b&#xff0c;\u6240\u4ee5\u7a0b\u5e8f\u53ea\u80fd\u88ab\u4fe1\u53f7\u5524\u9192\u4e0a\u6b21&#xff0c;\u4e5f\u53ea\u4f1a\u6267\u884c\u4e09\u6b21\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002\u00a0<\/p>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 sigaction()\u51fd\u6570<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6b64\u5904\u4ecb\u7ecd\u4e00\u4e2asigaction\u51fd\u6570&#xff0c;\u5b83\u7c7b\u4f3c\u4e8esignal\u51fd\u6570&#xff0c;\u4e14\u5b8c\u5168\u53ef\u4ee5\u66ff\u6362signal\u51fd\u6570&#xff0c;\u4e14\u66f4\u7a33\u5b9a\u3002<\/p>\n<p>\u201csigaction\u51fd\u6570\u5728UNIX\u7cfb\u5217\u7684\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5b8c\u5168\u76f8\u540c&#xff0c;\u800csignal\u51fd\u6570\u53ef\u80fd\u5b58\u5728\u533a\u522b\u201d\u3002<\/p>\n<p>#include &lt;signal.h&gt;<\/p>\n<p>int sigaction(int signo, const struct sigaction *act, struct sigaction*oldact);<br \/>\n\/\/\u6210\u529f\u8fd4\u56de 0&#xff0c; \u5931\u8d25\u65f6\u8fd4\u56de-1.<br \/>\n\/*<br \/>\n    signo: \u4e0esignal\u51fd\u6570\u76f8\u540c&#xff0c;\u4f20\u9012\u4fe1\u53f7\u4fe1\u606f<br \/>\n    act&#xff1a;\u5bf9\u5e94\u4e8e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4fe1\u606f<br \/>\n    oldact:\u901a\u8fc7\u6b64\u53c2\u6570\u83b7\u53d6\u4e4b\u524d\u6ce8\u518c\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6307\u9488&#xff0c;\u82e5\u4e0d\u9700\u8981\u5219\u4f20\u9012 0<br \/>\n*\/<\/p>\n<p>struct sigaction<br \/>\n{<br \/>\n    void (*sa_handler)(int);<br \/>\n    sigset_t sa_mask;<br \/>\n    int sa_flags;<br \/>\n};<br \/>\n\/*<br \/>\n    sa_mask\u548csa_flags\u7684\u6240\u6709\u4f4d\u5747\u521d\u59cb\u5316\u4e3a 0 \u5373\u53ef&#xff0c;\u8fd9\u4e24\u4e2a\u6210\u5458\u7528\u4e8e\u6307\u5b9a\u4fe1\u53f7\u76f8\u5173\u7684 \u9009\u9879 \u548c \u7279\u6027&#xff0c;\u800c\u6211\u4eec\u7684\u76ee\u7684\u4e3b\u8981\u662f\u9632\u6b62\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u6545\u7701\u7565\u3002<br \/>\n    sa_handler&#xff1a;\u4fdd\u5b58\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u6307\u9488\u503c&#xff08;\u5730\u5740\u503c&#xff09;\u3002<br \/>\n*\/ <\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5219\u4e0a\u9762\u7684\u51fd\u6570\u53ef\u4fee\u6539\u4e3a&#xff1a;<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;signal.h&gt;<\/p>\n<p>void time_out(int sig)\/\/alarm\u51fd\u6570\u65f6\u95f4\u5230\u8fbe\u5904\u7406\u51fd\u6570<br \/>\n{<br \/>\nif(sig &#061;&#061; SIGALRM)<br \/>\n{<br \/>\nprintf(&#034;time_out\\\\n&#034;);<br \/>\n}<br \/>\nalarm(2);<br \/>\n}<\/p>\n<p>void func(int sig) \/\/ctrl&#043;c \u4fe1\u53f7\u5904\u7406\u51fd\u6570<br \/>\n{<br \/>\nif(sig &#061;&#061; SIGINT)<br \/>\nputs(&#034;CTRL &#043; C is prossed&#034;);<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\nint i;<br \/>\n    struct sigaction act;<br \/>\n    act.sa_handler &#061; time_out;<br \/>\n    sigemptyset(&amp;act.sa_mask);<br \/>\n    act.sa_flags &#061; 0;<br \/>\n    sigaction(SIGALRM, &amp;act, 0);<br \/>\n    alarm(2);<\/p>\n<p>struct sigaction act_pro;<br \/>\n    act_pro.sa_handler &#061; func;<br \/>\n    sigemptyset(&amp;act_pro.sa_mask);<br \/>\n    act_pro.sa_flags &#061; 0;<br \/>\n    sigaction(SIGINT, &amp;act_pro, 0);<\/p>\n<p>for(i &#061; 0;i&lt;3;i&#043;&#043;) \/\/\u6bcf\u969450s\u8f93\u51fa\u4e00\u6b21&#xff0c;\u7406\u8bba\u7a0b\u5e8f\u4f1a\u6267\u884c150s\u3002<br \/>\n{<br \/>\nputs(&#034;loading&gt;&gt;&gt;&gt;&#034;);<br \/>\nsleep(50);<br \/>\n}<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6267\u884c\u60c5\u51b5\u5982\u4e0b&#xff1a;<\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"556\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250617080040-685120a898a63.png\" width=\"576\" \/><\/p>\n<h3>\u5957\u63a5\u5b57\u7ba1\u7406<\/h3>\n<p>\/\/ \u7236\u5b50\u8fdb\u7a0b\u8d44\u6e90\u5206\u79bb<br \/>\nif (pid &#061;&#061; 0) {<br \/>\n    close(server_fd); \/\/ \u5b50\u8fdb\u7a0b\u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n} else {<br \/>\n    close(new_socket); \/\/ \u7236\u8fdb\u7a0b\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n} <\/p>\n<h3 id=\"\u591a\u8fdb\u7a0bTCP\u5ba2\u6237\u7aef\">\u591a\u8fdb\u7a0bTCP\u5ba2\u6237\u7aef<\/h3>\n<h4 id=\"\u5b8c\u6574\u5ba2\u6237\u7aef\u4ee3\u7801\">\u5b8c\u6574\u5ba2\u6237\u7aef\u4ee3\u7801<\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>#define SERVER_IP &#034;127.0.0.1&#034;<br \/>\n#define SERVER_PORT 8080<br \/>\n#define NUM_CLIENTS 3<br \/>\n#define BUFFER_SIZE 1024<\/p>\n<p>void client_process(int client_id) {<br \/>\n    int sock &#061; 0;<br \/>\n    struct sockaddr_in serv_addr;<br \/>\n    char buffer[BUFFER_SIZE] &#061; {0};<\/p>\n<p>    \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    if ((sock &#061; socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {<br \/>\n        perror(&#034;socket creation failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    serv_addr.sin_family &#061; AF_INET;<br \/>\n    serv_addr.sin_port &#061; htons(SERVER_PORT);<\/p>\n<p>    \/\/ \u8f6c\u6362IP\u5730\u5740<br \/>\n    if (inet_pton(AF_INET, SERVER_IP, &amp;serv_addr.sin_addr) &lt;&#061; 0) {<br \/>\n        perror(&#034;invalid address&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8fde\u63a5\u670d\u52a1\u5668<br \/>\n    if (connect(sock, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<br \/>\n        perror(&#034;connection failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    printf(&#034;Client %d connected to server\\\\n&#034;, client_id);<\/p>\n<p>    \/\/ \u53d1\u9001\u8bf7\u6c42<br \/>\n    char message[BUFFER_SIZE];<br \/>\n    snprintf(message, sizeof(message),<br \/>\n             &#034;GET \/ HTTP\/1.1\\\\r\\\\n&#034;<br \/>\n             &#034;Host: localhost\\\\r\\\\n&#034;<br \/>\n             &#034;User-Agent: Client\/%d\\\\r\\\\n&#034;<br \/>\n             &#034;\\\\r\\\\n&#034;, client_id);<\/p>\n<p>    send(sock, message, strlen(message), 0);<br \/>\n    printf(&#034;Client %d sent request\\\\n&#034;, client_id);<\/p>\n<p>    \/\/ \u63a5\u6536\u54cd\u5e94<br \/>\n    ssize_t bytes_read;<br \/>\n    while ((bytes_read &#061; read(sock, buffer, BUFFER_SIZE &#8211; 1)) &gt; 0) {<br \/>\n        buffer[bytes_read] &#061; &#039;\\\\0&#039;;<br \/>\n        printf(&#034;Client %d received:\\\\n%s\\\\n&#034;, client_id, buffer);<br \/>\n    }<\/p>\n<p>    close(sock);<br \/>\n    printf(&#034;Client %d disconnected\\\\n&#034;, client_id);<br \/>\n    exit(EXIT_SUCCESS);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    pid_t pids[NUM_CLIENTS];<\/p>\n<p>    \/\/ \u521b\u5efa\u591a\u4e2a\u5ba2\u6237\u7aef\u8fdb\u7a0b<br \/>\n    for (int i &#061; 0; i &lt; NUM_CLIENTS; i&#043;&#043;) {<br \/>\n        pids[i] &#061; fork();<\/p>\n<p>        if (pids[i] &lt; 0) {<br \/>\n            perror(&#034;fork failed&#034;);<br \/>\n            exit(EXIT_FAILURE);<br \/>\n        }<\/p>\n<p>        if (pids[i] &#061;&#061; 0) { \/\/ \u5b50\u8fdb\u7a0b<br \/>\n            client_process(i &#043; 1);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u7236\u8fdb\u7a0b\u7b49\u5f85\u6240\u6709\u5b50\u8fdb\u7a0b\u7ed3\u675f<br \/>\n    for (int i &#061; 0; i &lt; NUM_CLIENTS; i&#043;&#043;) {<br \/>\n        waitpid(pids[i], NULL, 0);<br \/>\n    }<\/p>\n<p>    printf(&#034;All clients completed\\\\n&#034;);<br \/>\n    return 0;<br \/>\n} <\/p>\n<h4 id=\"\u591a\u8fdb\u7a0b\u5ba2\u6237\u7aef\u5173\u952e\u6280\u672f\">\u591a\u8fdb\u7a0b\u5ba2\u6237\u7aef\u5173\u952e\u6280\u672f<\/h4>\n<li>\n<h5>\u5e76\u53d1\u8fde\u63a5<\/h5>\n<p> for (int i &#061; 0; i &lt; NUM_CLIENTS; i&#043;&#043;) {<br \/>\n    pids[i] &#061; fork();<br \/>\n    if (pids[i] &#061;&#061; 0) {<br \/>\n        client_process(i &#043; 1);<br \/>\n    }<br \/>\n} <\/li>\n<li>\n<h5>\u8bf7\u6c42\u5b9a\u5236<\/h5>\n<p> snprintf(message, sizeof(message),<br \/>\n         &#034;GET \/ HTTP\/1.1\\\\r\\\\n&#034;<br \/>\n         &#034;Host: localhost\\\\r\\\\n&#034;<br \/>\n         &#034;User-Agent: Client\/%d\\\\r\\\\n&#034;<br \/>\n         &#034;\\\\r\\\\n&#034;, client_id); <\/li>\n<li>\n<h5>\u54cd\u5e94\u5904\u7406<\/h5>\n<p> while ((bytes_read &#061; read(sock, buffer, BUFFER_SIZE &#8211; 1)) &gt; 0) {<br \/>\n    buffer[bytes_read] &#061; &#039;\\\\0&#039;;<br \/>\n    printf(&#034;Client %d received:\\\\n%s\\\\n&#034;, client_id, buffer);<br \/>\n} <\/li>\n<h3 id=\"\u7cfb\u7edf\u6d4b\u8bd5\u4e0e\u4f18\u5316\">\u7cfb\u7edf\u6d4b\u8bd5\u4e0e\u4f18\u5316<\/h3>\n<h4 id=\"\u6d4b\u8bd5\u65b9\u6cd5\">\u6d4b\u8bd5\u65b9\u6cd5<\/h4>\n<p># \u7f16\u8bd1\u670d\u52a1\u5668<br \/>\ngcc server.c -o server<\/p>\n<p># \u7f16\u8bd1\u5ba2\u6237\u7aef<br \/>\ngcc client.c -o client<\/p>\n<p># \u542f\u52a8\u670d\u52a1\u5668<br \/>\n.\/server<\/p>\n<p># \u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u542f\u52a8\u5ba2\u6237\u7aef<br \/>\n.\/client <\/p>\n<h4 id=\"\u6027\u80fd\u4f18\u5316\u6280\u672f\">\u6027\u80fd\u4f18\u5316\u6280\u672f<\/h4>\n<li>\n<h5>\u8fdb\u7a0b\u6c60\u6280\u672f<\/h5>\n<p> #define POOL_SIZE 5<\/p>\n<p>\/\/ \u9884\u5148\u521b\u5efa\u8fdb\u7a0b<br \/>\nfor (int i &#061; 0; i &lt; POOL_SIZE; i&#043;&#043;) {<br \/>\n    pid_t pid &#061; fork();<br \/>\n    if (pid &#061;&#061; 0) {<br \/>\n        worker_process(); \/\/ \u5de5\u4f5c\u8fdb\u7a0b\u5faa\u73af\u5904\u7406\u8bf7\u6c42<br \/>\n    }<br \/>\n} <\/li>\n<li>\n<h5>\u8fde\u63a5\u590d\u7528<\/h5>\n<p> \/\/ \u4fdd\u6301\u8fde\u63a5\u800c\u975e\u6bcf\u6b21\u65b0\u5efa<br \/>\nwhile (1) {<br \/>\n    \/\/ \u5904\u7406\u591a\u4e2a\u8bf7\u6c42<br \/>\n    process_request(socket);<br \/>\n} <\/li>\n<li>\n<h5>\u8d1f\u8f7d\u76d1\u63a7<\/h5>\n<p> void monitor_load() {<br \/>\n    struct rusage usage;<br \/>\n    getrusage(RUSAGE_SELF, &amp;usage);<br \/>\n    printf(&#034;CPU usage: %ld.%06ld sec\\\\n&#034;,<br \/>\n           usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);<br \/>\n} <\/li>\n<h4 id=\"\u5b89\u5168\u589e\u5f3a\">\u5b89\u5168\u589e\u5f3a<\/h4>\n<li>\n<h5>\u6743\u9650\u964d\u7ea7<\/h5>\n<p> if (setuid(getuid()) &lt; 0) {<br \/>\n    perror(&#034;setuid failed&#034;);<br \/>\n    exit(EXIT_FAILURE);<br \/>\n} <\/li>\n<li>\n<h5>\u8d44\u6e90\u9650\u5236<\/h5>\n<p> #include &lt;sys\/resource.h&gt;<\/p>\n<p>struct rlimit limit &#061; {<br \/>\n    .rlim_cur &#061; 100, \/\/ 100\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    .rlim_max &#061; 100<br \/>\n};<br \/>\nsetrlimit(RLIMIT_NOFILE, &amp;limit); <\/li>\n<li>\n<h5>\u8f93\u5165\u9a8c\u8bc1<\/h5>\n<p> \/\/ \u9a8c\u8bc1\u63a5\u6536\u7684\u6570\u636e<br \/>\nif (strstr(buffer, &#034;malicious&#034;) !&#061; NULL) {<br \/>\n    close(socket);<br \/>\n    return;<br \/>\n} <\/li>\n<h3 id=\"\u5e94\u7528\u573a\u666f\u4e0e\u6269\u5c55\">\u5e94\u7528\u573a\u666f\u4e0e\u6269\u5c55<\/h3>\n<h4 id=\"\u9002\u7528\u573a\u666f\">\u9002\u7528\u573a\u666f<\/h4>\n<li>\u9ad8\u5e76\u53d1\u7f51\u7edc\u670d\u52a1&#xff08;HTTP\u670d\u52a1\u5668&#xff09;<\/li>\n<li>\u5e76\u884c\u6570\u636e\u5904\u7406\u7cfb\u7edf<\/li>\n<li>\u5b9e\u65f6\u901a\u4fe1\u5e94\u7528<\/li>\n<li>\u5206\u5e03\u5f0f\u8ba1\u7b97\u8282\u70b9<\/li>\n<li>\u538b\u529b\u6d4b\u8bd5\u5de5\u5177<\/li>\n<h4 id=\"\u6269\u5c55\u65b9\u5411\">\u6269\u5c55\u65b9\u5411<\/h4>\n<li>\n<p>\u6dfb\u52a0SSL\/TLS\u52a0\u5bc6<\/p>\n<p> #include &lt;openssl\/ssl.h&gt;<\/p>\n<p>SSL_CTX *ctx &#061; SSL_CTX_new(TLS_server_method());<br \/>\nSSL *ssl &#061; SSL_new(ctx);<br \/>\nSSL_set_fd(ssl, socket);<br \/>\nSSL_accept(ssl);<br \/>\nSSL_read(ssl, buffer, sizeof(buffer)); <\/li>\n<li>\n<p>\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/p>\n<p> \/\/ \u4f7f\u7528\u7ba1\u9053<br \/>\nint pipefd[2];<br \/>\npipe(pipefd);<br \/>\nwrite(pipefd[1], data, size); <\/li>\n<li>\n<p>\u6dfb\u52a0\u65e5\u5fd7\u7cfb\u7edf<\/p>\n<p> void log_message(const char *msg) {<br \/>\n    FILE *log &#061; fopen(&#034;server.log&#034;, &#034;a&#034;);<br \/>\n    fprintf(log, &#034;[%ld] %s\\\\n&#034;, time(NULL), msg);<br \/>\n    fclose(log);<br \/>\n} <\/li>\n<li>\n<p>\u914d\u7f6e\u70ed\u91cd\u8f7d<\/p>\n<p> \/\/ \u4f7f\u7528SIGHUP\u4fe1\u53f7<br \/>\nsignal(SIGHUP, reload_config); <\/li>\n<h3 id=\"\u603b\u7ed3\u5bf9\u6bd4\">\u603b\u7ed3\u5bf9\u6bd4<\/h3>\n<table>\n<tr>\u7279\u6027\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u591a\u8fdb\u7a0b\u5ba2\u6237\u7aef<\/tr>\n<tbody>\n<tr>\n<td>\u4e3b\u8981\u76ee\u7684<\/td>\n<td>\u5904\u7406\u5e76\u53d1\u8fde\u63a5<\/td>\n<td>\u6a21\u62df\u5e76\u53d1\u8bf7\u6c42<\/td>\n<\/tr>\n<tr>\n<td>\u8fdb\u7a0b\u89d2\u8272<\/td>\n<td>\u7236\u8fdb\u7a0b\u7ba1\u7406&#xff0c;\u5b50\u8fdb\u7a0b\u5904\u7406<\/td>\n<td>\u7236\u8fdb\u7a0b\u534f\u8c03&#xff0c;\u5b50\u8fdb\u7a0b\u6267\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u8d44\u6e90\u6d88\u8017<\/td>\n<td>\u8f83\u9ad8&#xff08;\u6bcf\u4e2a\u8fde\u63a5\u4e00\u4e2a\u8fdb\u7a0b&#xff09;<\/td>\n<td>\u53ef\u63a7&#xff08;\u53ef\u914d\u7f6e\u8fdb\u7a0b\u6570&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u957f\u671f\u8fd0\u884c\u7684\u670d\u52a1<\/td>\n<td>\u6d4b\u8bd5\/\u6279\u91cf\u4efb\u52a1<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u6742\u5ea6<\/td>\n<td>\u9ad8&#xff08;\u9700\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b&#xff09;<\/td>\n<td>\u4e2d&#xff08;\u8f83\u7b80\u5355&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\u5c55\u793a\u4e86C\u8bed\u8a00\u5728\u7cfb\u7edf\u7f16\u7a0b\u4e2d\u7684\u5f3a\u5927\u80fd\u529b\u3002\u901a\u8fc7\u5408\u7406\u8fd0\u7528\u8fdb\u7a0b\u7ba1\u7406\u3001\u5957\u63a5\u5b57\u7f16\u7a0b\u548c\u5e76\u53d1\u63a7\u5236\u6280\u672f&#xff0c;\u53ef\u4ee5\u6784\u5efa\u51fa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002\u5173\u952e\u70b9\u5305\u62ec&#xff1a;<\/p>\n<li>\u6b63\u786e\u7684\u8fdb\u7a0b\u7ba1\u7406&#xff1a;\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u9732<\/li>\n<li>\u9ad8\u6548\u7684\u8d44\u6e90\u5206\u914d&#xff1a;\u53ca\u65f6\u5173\u95ed\u4e0d\u9700\u8981\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/li>\n<li>\u5065\u58ee\u7684\u9519\u8bef\u5904\u7406&#xff1a;\u5e94\u5bf9\u5404\u79cd\u7f51\u7edc\u5f02\u5e38<\/li>\n<li>\u53ef\u6269\u5c55\u7684\u67b6\u6784&#xff1a;\u652f\u6301\u8fdb\u7a0b\u6c60\u7b49\u4f18\u5316\u6280\u672f<\/li>\n<p>\u8fd9\u79cd\u6a21\u5f0f\u867d\u7136\u8d44\u6e90\u6d88\u8017\u5927\u4e8e\u7ebf\u7a0b\u6a21\u578b&#xff0c;\u4f46\u5728\u7a33\u5b9a\u6027\u3001\u5b89\u5168\u6027\u548c\u9694\u79bb\u6027\u65b9\u9762\u5177\u6709\u4f18\u52bf&#xff0c;\u7279\u522b\u9002\u5408\u9700\u8981\u9ad8\u53ef\u9760\u6027\u7684\u670d\u52a1\u7aef\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf16\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u5173\u952e\u6280\u672f\u3002\u670d\u52a1\u5668\u7aef\u901a\u8fc7fork()\u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u5e76\u53d1\u8fde\u63a5\uff0c\u4f7f\u7528SIGCHLD\u4fe1\u53f7\u548csigaction()\u51fd\u6570\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\uff0c\u5e76\u8be6\u7ec6\u8bf4\u660e\u4e86\u5957\u63a5\u5b57\u7ba1\u7406\u673a\u5236\u3002\u5ba2\u6237\u7aef\u5219\u5229\u7528\u591a\u8fdb\u7a0b\u6a21\u62df\u5e76\u53d1\u8bf7\u6c42\uff0c\u5c55\u793a\u8bf7\u6c42\u5b9a\u5236\u548c\u54cd\u5e94\u5904\u7406\u6d41\u7a0b\u3002\u6587\u7ae0\u8fd8\u6db5\u76d6\u4e86\u7cfb\u7edf\u6d4b\u8bd5\u4f18\u5316\u65b9\u6cd5\uff0c\u5305\u62ec\u8fdb\u7a0b\u6c60\u3001\u8fde\u63a5\u590d\u7528\u7b49\u6280\u672f\uff0c\u5e76\u63a2\u8ba8\u4e86\u5b89\u5168\u589e\u5f3a\u63aa\u65bd\u548c\u5e94\u7528\u6269\u5c55\u65b9\u5411\u3002\u8fd9\u79cd\u591a\u8fdb\u7a0b\u6a21\u578b\u867d\u7136\u8d44\u6e90\u6d88\u8017\u8f83\u5927\uff0c\u4f46\u5728\u7a33\u5b9a\u6027\u548c\u9694\u79bb\u6027\u65b9\u9762\u5177\u6709\u4f18\u52bf\uff0c\u9002\u5408\u9ad8\u53ef\u9760\u6027\u7f51\u7edc\u670d\u52a1\u5f00\u53d1\u3002<\/p>\n","protected":false},"author":2,"featured_media":44158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[174,205,43,1714],"topic":[],"class_list":["post-44161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-tcp-ip","tag-43","tag-1714"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \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\/44161.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf16\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u5173\u952e\u6280\u672f\u3002\u670d\u52a1\u5668\u7aef\u901a\u8fc7fork()\u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u5e76\u53d1\u8fde\u63a5\uff0c\u4f7f\u7528SIGCHLD\u4fe1\u53f7\u548csigaction()\u51fd\u6570\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\uff0c\u5e76\u8be6\u7ec6\u8bf4\u660e\u4e86\u5957\u63a5\u5b57\u7ba1\u7406\u673a\u5236\u3002\u5ba2\u6237\u7aef\u5219\u5229\u7528\u591a\u8fdb\u7a0b\u6a21\u62df\u5e76\u53d1\u8bf7\u6c42\uff0c\u5c55\u793a\u8bf7\u6c42\u5b9a\u5236\u548c\u54cd\u5e94\u5904\u7406\u6d41\u7a0b\u3002\u6587\u7ae0\u8fd8\u6db5\u76d6\u4e86\u7cfb\u7edf\u6d4b\u8bd5\u4f18\u5316\u65b9\u6cd5\uff0c\u5305\u62ec\u8fdb\u7a0b\u6c60\u3001\u8fde\u63a5\u590d\u7528\u7b49\u6280\u672f\uff0c\u5e76\u63a2\u8ba8\u4e86\u5b89\u5168\u589e\u5f3a\u63aa\u65bd\u548c\u5e94\u7528\u6269\u5c55\u65b9\u5411\u3002\u8fd9\u79cd\u591a\u8fdb\u7a0b\u6a21\u578b\u867d\u7136\u8d44\u6e90\u6d88\u8017\u8f83\u5927\uff0c\u4f46\u5728\u7a33\u5b9a\u6027\u548c\u9694\u79bb\u6027\u65b9\u9762\u5177\u6709\u4f18\u52bf\uff0c\u9002\u5408\u9ad8\u53ef\u9760\u6027\u7f51\u7edc\u670d\u52a1\u5f00\u53d1\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44161.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-17T08:00:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250617080040-685120a8518bd.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=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44161.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44161.html\",\"name\":\"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-17T08:00:41+00:00\",\"dateModified\":\"2025-06-17T08:00:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44161.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44161.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44161.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\"}]},{\"@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":"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \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\/44161.html","og_locale":"zh_CN","og_type":"article","og_title":"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf16\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u5173\u952e\u6280\u672f\u3002\u670d\u52a1\u5668\u7aef\u901a\u8fc7fork()\u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u5e76\u53d1\u8fde\u63a5\uff0c\u4f7f\u7528SIGCHLD\u4fe1\u53f7\u548csigaction()\u51fd\u6570\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\uff0c\u5e76\u8be6\u7ec6\u8bf4\u660e\u4e86\u5957\u63a5\u5b57\u7ba1\u7406\u673a\u5236\u3002\u5ba2\u6237\u7aef\u5219\u5229\u7528\u591a\u8fdb\u7a0b\u6a21\u62df\u5e76\u53d1\u8bf7\u6c42\uff0c\u5c55\u793a\u8bf7\u6c42\u5b9a\u5236\u548c\u54cd\u5e94\u5904\u7406\u6d41\u7a0b\u3002\u6587\u7ae0\u8fd8\u6db5\u76d6\u4e86\u7cfb\u7edf\u6d4b\u8bd5\u4f18\u5316\u65b9\u6cd5\uff0c\u5305\u62ec\u8fdb\u7a0b\u6c60\u3001\u8fde\u63a5\u590d\u7528\u7b49\u6280\u672f\uff0c\u5e76\u63a2\u8ba8\u4e86\u5b89\u5168\u589e\u5f3a\u63aa\u65bd\u548c\u5e94\u7528\u6269\u5c55\u65b9\u5411\u3002\u8fd9\u79cd\u591a\u8fdb\u7a0b\u6a21\u578b\u867d\u7136\u8d44\u6e90\u6d88\u8017\u8f83\u5927\uff0c\u4f46\u5728\u7a33\u5b9a\u6027\u548c\u9694\u79bb\u6027\u65b9\u9762\u5177\u6709\u4f18\u52bf\uff0c\u9002\u5408\u9ad8\u53ef\u9760\u6027\u7f51\u7edc\u670d\u52a1\u5f00\u53d1\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/44161.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-17T08:00:41+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250617080040-685120a8518bd.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44161.html","url":"https:\/\/www.wsisp.com\/helps\/44161.html","name":"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-17T08:00:41+00:00","dateModified":"2025-06-17T08:00:41+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44161.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44161.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44161.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C\u8bed\u8a00\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef"}]},{"@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\/44161","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=44161"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44161\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44158"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44161"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}