{"id":294361,"date":"2024-11-14T20:08:08","date_gmt":"2024-11-14T12:08:08","guid":{"rendered":"https:\/\/gulass.cn\/jylmlhdqltcx.html"},"modified":"2024-11-14T20:08:08","modified_gmt":"2024-11-14T12:08:08","slug":"jylmlhdqltcx","status":"publish","type":"post","link":"https:\/\/gulass.cn\/jylmlhdqltcx.html","title":{"rendered":"\u57fa\u4e8e Linux \u547d\u4ee4\u884c\u7684\u7fa4\u804a\u5929\u7a0b\u5e8f\u529f\u80fd\u4ecb\u7ecd\u4e0e\u6280\u672f\u7ec3\u4e60"},"content":{"rendered":"
\u4e00\u3001\u529f\u80fd\u4ecb\u7ecd<\/p>\n
\u8fd9\u662f\u57fa\u4e8eLinux<\/a>\u4e0b\u547d\u4ee4<\/a>\u884c\u8bbe\u8ba1\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u7fa4\u804a\u5929\u7a0b\u5e8f\u3002<\/p>\n \u8fd9\u4e2a\u4e8b\u4f8b\u53ef\u4ee5\u5b66\u4e60\u3001\u5de9\u56faLinux\u4e0b\u7f51\u8def\u7f16\u7a0b\u76f8\u5173\u77e5\u8bc6\u70b9<\/p>\n \u7ec3\u4e60Linux\u4e0bsocket\u3001TCP\u7f16\u7a0b<\/p>\n \u7ec3\u4e60Linux\u4e0bpthread\u3001\u7ebf\u7a0b\u7f16\u7a0b<\/p>\n <\/p>\n \u7ec3\u4e60Linux\u4e0b\u591a\u8defIO\u68c0\u67e5\u3001select\u51fd\u6570\u4f7f\u7528<\/p>\n \u7ec3\u4e60C\u8bed\u8a00\u6570\u7ec4\u4f7f\u7528<\/p>\n \u7ec3\u4e60\u7ebf\u7a0b\u95f4\u540c\u6b65\u4e0e\u4e92\u65a5\u3001\u4e92\u65a5\u9501mutex\u7684\u4f7f\u7528<\/p>\n \u7fa4\u804a\u7a0b\u5e8f\u5206\u4e3a\u987e\u5ba2\u7aef\u548c\u670d\u52a1\u5668\u4e24\u4e2a\u7a0b\u5e8f<\/p>\n <\/p>\n \u670d\u52a1\u5668\u7aef:\u8fd0\u884c\u6574\u4e2a\u4e8b\u4f8b\u8981\u5148\u8fd0\u884c\u670d\u52a1\u5668linux\u670d\u52a1\u5668\u7ef4\u62a4\uff0c\u670d\u52a1\u5668\u4e3b\u8981\u7528\u4e8e\u63a5\u6536\u987e\u5ba2\u7aef\u7684\u6d88\u606f\uff0c\u518d\u8f6c\u53d1\u7ed9\u5176\u4ed6\u5728\u7ebf\u7684\u987e\u5ba2\u7aef\u3002\u670d\u52a1\u5668\u91cc\u91c7\u7528\u591a\u7ebf\u7a0b\u7684\u65b9\u5f0f\uff0c\u6bcf\u8054\u63a5\u4e0a\u4e00\u4e2a\u987e\u5ba2\u7aef\u5c31\u521b\u5efa\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u5355\u72ec\u5904\u7406\uff1b\u7528\u4e86\u4e00\u4e2a\u5168\u5c40\u6570\u7ec4\u50a8\u5b58\u65e9\u5df2\u8054\u63a5\u4e0a\u6765\u7684\u987e\u5ba2\u7aef\uff0c\u5f53\u4e00\u4e2a\u987e\u5ba2\u7aef\u53d1\u6765\u6d88\u606f\u540emalloc linux\u5934\u6587\u4ef6<\/strong>\uff0c\u5c31\u6328\u4e2a\u8f6c\u53d1\u7ed9\u5176\u4ed6\u987e\u5ba2\u7aef\uff0c\u987e\u5ba2\u7aef\u65ad\u6389\u8054\u63a5\u4e0b\u7ebf\u540emalloc linux\u5934\u6587\u4ef6<\/strong>\uff0c\u5c31\u5220\u6389\u5bf9\u5e94\u7684\u8282\u70b9\uff1b\u6570\u7ec4\u6dfb\u52a0\u8282\u70b9\u3001\u5220\u9664\u8282\u70b9\u91c7\u7528\u4e92\u65a5\u9501\u4fdd\u62a4\u3002<\/p>\n \u987e\u5ba2\u7aef:\u987e\u5ba2\u7aef\u76f8\u5f53\u4e8e\u4e00\u4e2a\u7528\u6237linux \u8f6f\u4ef6\uff0c\u987e\u5ba2\u7aef\u4ee3\u7801\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\uff0c\u8054\u63a5\u5230\u670d\u52a1\u5668\u4ee5\u540e\uff0c\u76f8\u4e92\u53d1\u9001\u6d88\u606f\u8fdb\u884c\u804a\u5929\u3002\u53d1\u9001\u7684\u6d88\u606f\u91c7\u7528\u4e00\u4e2a\u7ed3\u6784\u4f53\u5c01\u88c5\uff0c\u4e0a\u9762\u5305\u542b\u4e86\u7528\u6237\u540d\u3001\u72b6\u6001\u3001\u6d88\u606f\u672c\u8eab\u3002<\/p>\n \u529f\u80fd\u603b\u7ed3:\u652f\u6301\u597d\u53cb\u4e0a\u7ebf\u63d0\u9192\u3001\u597d\u53cb\u4e0b\u7ebf\u63d0\u9192\u3001\u5f53\u524d\u5728\u7ebf\u603b\u4eba\u6570\u63d0\u793a\u3001\u804a\u5929\u6d88\u606f\u6587\u672c\u8f6c\u53d1\u3002<\/p>\n \u597d\u53cb\u4e0a\u7ebf\u901a\u77e5\u3001\u6b63\u5e38\u804a\u5929\u7597\u6548:<\/p>\n <\/p>\n \u597d\u53cb\u4e0b\u7ebf\u63d0\u793a:<\/p>\n <\/p>\n \u4e8c\u3001select\u51fd\u6570\u529f\u80fd\u3001\u53c2\u6570\u4ecb\u7ecd<\/p>\n <\/p>\n \u5728linux\u547d\u4ee4\u884c\u53ef\u4ee5\u76f4\u63a5man\u67e5\u770bselect\u51fd\u6570\u7684\u539f\u578b\u3001\u5934\u6587\u4ef6\u3001\u5e2e\u52a9\u3001\u4f8b\u5b50\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n select\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u7a83\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\uff0c\u5728socket\u7f16\u7a0b\u91cc\uff0c\u53ef\u4ee5\u62ff\u6765\u7a83\u542c\u987e\u5ba2\u7aef\u6216\u5219\u670d\u52a1\u5668\u6709\u6ca1\u6709\u53d1\u6765\u6d88\u606f\u3002<\/p>\n Linux\u4e0b\u7a83\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26\u72b6\u6001\u7684\u51fd\u6570\u67093\u4e2a\uff1aselect\u3001poll\u3001epoll\uff0c\u8fd93\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u7528\u5728socket\u7f51\u8def\u7f16\u7a0b\u91cc\u7a83\u542c\u987e\u5ba2\u7aef\u3001\u670d\u52a1\u5668\u7684\u72b6\u6001\u3002\u8fd9\u7bc7\u6587\u7ae0\u7684\u4e8b\u4f8b\u91cc\u4f7f\u7528\u7684\u662fselect\uff0c\u524d\u9762\u6587\u7ae0\u4f1a\u7ee7\u7eed\u4ecb\u7ecdpoll\u3001epoll\u51fd\u6570\u7684\u4f7f\u7528\u4e8b\u4f8b\u3002<\/p>\n select\u51fd\u6570\u539f\u578b\u3001\u53c2\u6570\u4ecb\u7ecd<\/p>\n <\/p>\n \n cpp<\/span><\/p><\/div> \u4e09\u3001\u804a\u5929\u7a0b\u5e8f\u4ee3\u78013.1client.c\u987e\u5ba2\u7aef\u4ee3\u7801<\/p>\n \n cpp<\/span><\/p><\/div>#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
<\/span>
int<\/span> select<\/span>(int<\/span> nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct<\/span> timeval *timeout)<\/span><\/span>;<\/span>
\u51fd\u6570\u529f\u80fd: \u76d1\u542c\u6307\u5b9a\u6570\u91cf\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u3002<\/span>
\u51fd\u6570\u53c2\u6570:<\/span>
int<\/span> nfds : \u76d1\u542c\u6700\u5927\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26+1<\/span>\u7684\u503c<\/span>
fd_set *readfds :\u76d1\u542c\u8bfb\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408,\u4e0d\u60f3\u76d1\u542c\u8bfb\u4e8b\u4ef6\u8fd9\u91cc\u53ef\u4ee5\u586bNULL<\/span><\/span>
fd_set *writefds :\u76d1\u542c\u5199\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408,\u4e0d\u60f3\u76d1\u542c\u4e8b\u4ef6\u8fd9\u91cc\u53ef\u4ee5\u586bNULL<\/span><\/span>
fd_set *exceptfds :\u76d1\u542c\u5176\u4ed6\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408,\u4e0d\u60f3\u76d1\u542c\u4e8b\u4ef6\u8fd9\u91cc\u53ef\u4ee5\u586bNULL<\/span><\/span>
struct<\/span> timeval<\/span> *timeout : \u6307\u5b9a\u7b49\u5f85\u7684\u65f6\u95f4\u3002 \u5982\u679c\u586bNULL<\/span>\u8868\u793a\u6c38\u4e45\u7b49\u5f85\uff0c\u76f4\u5230\u4efb\u610f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ea7\u751f\u4e8b\u4ef6\u518d\u8fd4\u56de\u3002<\/span>
\u5982\u679c\u586b\u6b63\u5e38\u65f6\u95f4\uff0c\u5982\u679c\u5728\u7b49\u5f85\u7684\u65f6\u95f4\u5185\u6ca1\u6709\u4e8b\u4ef6\u4ea7\u751f\uff0c\u4e5f\u4f1a\u8fd4\u56de\u3002<\/span>
struct<\/span> timeval<\/span> {<\/span>
long<\/span> tv_sec; \/* seconds *\/<\/span><\/span>
long<\/span> tv_usec; \/* microseconds *\/<\/span><\/span>
};<\/span>
<\/span>
\u8fd4\u56de\u503c: \u8868\u793a\u4ea7\u751f\u4e8b\u4ef6\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u3002 ==0<\/span>\u8868\u793a\u6ca1\u6709\u4e8b\u4ef6\u4ea7\u751f\u3002 >0<\/span>\u8868\u793a\u4e8b\u4ef6\u6570\u91cf <0<\/span>\u8868\u793a\u9519\u8bef\u3002<\/span>
<\/span>
void<\/span> FD_CLR<\/span>(int<\/span> fd, fd_set *set); \/\/\u6e05\u9664\u67d0\u4e2a\u96c6\u5408\u91cc\u7684\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><\/span>
int<\/span> FD_ISSET<\/span>(int<\/span> fd, fd_set *set)<\/span><\/span>; \/\/\u5224\u65ad\u6307\u5b9a\u96c6\u5408\u91cc\u7684\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u4ea7\u751f\u4e86\u67d0\u4e2a\u4e8b\u4ef6\u3002 \u4e3a\u771f\u5c31\u8868\u793a\u4ea7\u751f\u4e86\u4e8b\u4ef6<\/span><\/span>
void<\/span> FD_SET<\/span>(int<\/span> fd, fd_set *set)<\/span><\/span>; \/\/\u5c06\u6307\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230\u6307\u5b9a\u7684\u96c6\u5408<\/span><\/span>
void<\/span> FD_ZERO<\/span>(fd_set *set)<\/span><\/span>; \/\/\u6e05\u7a7a\u6574\u4e2a\u96c6\u5408\u3002<\/span><\/span>
<\/span>
<\/code><\/pre>\n<\/p>\n#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/a><\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
#include<\/span> <\/span><\/span><\/span>
<\/span>
\/\/\u6d88\u606f\u7ed3\u6784\u4f53<\/span><\/span>
struct<\/span> MSG_DATA<\/span><\/span>
{<\/span>
char<\/span> type; \/\/\u6d88\u606f\u7c7b\u578b. 0\u8868\u793a\u6709\u804a\u5929\u7684\u6d88\u606f\u6570\u636e 1\u8868\u793a\u597d\u53cb\u4e0a\u7ebf 2\u8868\u793a\u597d\u53cb\u4e0b\u7ebf<\/span><\/span>
char<\/span> name[50<\/span>]; \/\/\u597d\u53cb\u540d\u79f0<\/span><\/span>
int<\/span> number; \/\/\u5728\u7ebf\u4eba\u6570\u7684\u6570\u91cf<\/span><\/span>
unsigned<\/span> char<\/span> buff[100<\/span>]; \/\/\u53d1\u9001\u7684\u804a\u5929\u6570\u636e\u6d88\u606f<\/span><\/span>
};<\/span>
struct<\/span> MSG_DATA<\/span> msg_data;<\/span>
<\/span>
\/\/\u6587\u4ef6\u63a5\u6536\u7aef<\/span><\/span>
int<\/span> main<\/span>(int<\/span> argc,char<\/span> **argv)<\/span><\/span><\/span>
{ <\/span>
if<\/span>(argc!=4<\/span>)<\/span>
{<\/span>
printf<\/span>(\".\/app n\"<\/span>);<\/span>
return<\/span> 0<\/span>;<\/span>
}<\/span>
int<\/span> sockfd;<\/span>
\/\/\u5ffd\u7565 SIGPIPE \u4fe1\u53f7--\u65b9\u5f0f\u670d\u52a1\u5668\u5411\u65e0\u6548\u7684\u5957\u63a5\u5b57\u5199\u6570\u636e\u5bfc\u81f4\u8fdb\u7a0b\u9000\u51fa<\/span><\/span>
signal<\/span>(SIGPIPE,SIG_IGN); <\/span>
<\/span>
\/*1. \u521b\u5efasocket\u5957\u63a5\u5b57*\/<\/span><\/span>
sockfd=socket<\/span>(AF_INET,SOCK_STREAM,0<\/span>);<\/span>
\/*2. \u8fde\u63a5\u670d\u52a1\u5668*\/<\/span><\/span>
struct<\/span> sockaddr_in<\/span> addr;<\/span>
addr.sin_family=AF_INET;<\/span>
addr.sin_port=htons<\/span>(atoi<\/span>(argv[2<\/span>])); \/\/ \u7aef\u53e3\u53f70~65535<\/span><\/span>
addr.sin_addr.s_addr=inet_addr<\/span>(argv[1<\/span>]); \/\/IP\u5730\u5740<\/span><\/span>
if<\/span>(connect<\/span>(sockfd,(const<\/span> struct<\/span> sockaddr *)&addr,sizeof<\/span>(struct<\/span> sockaddr_in))!=0<\/span>)<\/span>
{<\/span>
printf<\/span>(\"\u5ba2\u6237\u7aef:\u670d\u52a1\u5668\u8fde\u63a5\u5931\u8d25.n\"<\/span>);<\/span>
return<\/span>