{"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

malloc linux\u5934\u6587\u4ef6_\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b_\u5934\u6587\u4ef6\u662f\u4ec0\u4e48<\/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

malloc linux\u5934\u6587\u4ef6_\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b_\u5934\u6587\u4ef6\u662f\u4ec0\u4e48<\/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

malloc linux\u5934\u6587\u4ef6_\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b_\u5934\u6587\u4ef6\u662f\u4ec0\u4e48<\/p>\n

\u597d\u53cb\u4e0b\u7ebf\u63d0\u793a:<\/p>\n

\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b_\u5934\u6587\u4ef6\u662f\u4ec0\u4e48_malloc linux\u5934\u6587\u4ef6<\/p>\n

\u4e8c\u3001select\u51fd\u6570\u529f\u80fd\u3001\u53c2\u6570\u4ecb\u7ecd<\/p>\n

\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b_malloc linux\u5934\u6587\u4ef6_\u5934\u6587\u4ef6\u662f\u4ec0\u4e48<\/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

\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b_\u5934\u6587\u4ef6\u662f\u4ec0\u4e48_malloc linux\u5934\u6587\u4ef6<\/p>\n

\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

\u4e09\u3001\u804a\u5929\u7a0b\u5e8f\u4ee3\u78013.1client.c\u987e\u5ba2\u7aef\u4ee3\u7801<\/p>\n

\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>
#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> 0<\/span>;<\/span>
}<\/span>
<\/span>
\/*3. \u53d1\u9001\u6d88\u606f\u8868\u793a\u4e0a\u7ebf*\/<\/span><\/span>
msg_data.type=1<\/span>;<\/span>
strcpy<\/span>(msg_data.name,argv[3<\/span>]);<\/span>
write<\/span>(sockfd,&msg_data,sizeof<\/span>(struct<\/span> MSG_DATA));<\/span>
<\/span>
int<\/span> cnt;<\/span>
fd_set readfds;<\/span>
while<\/span>(1<\/span>)<\/span>
{<\/span>
FD_ZERO<\/span>(&readfds); \/\/\u6e05\u7a7a\u96c6\u5408<\/span><\/span>
FD_SET<\/span>(sockfd,&readfds); \/\/\u6dfb\u52a0\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26---\u53ef\u4ee5\u591a\u6b21\u8c03\u7528<\/span><\/span>
FD_SET<\/span>(0<\/span>,&readfds); \/\/\u6dfb\u52a0\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26---\u53ef\u4ee5\u591a\u6b21\u8c03\u7528<\/span><\/span>
\/\/ 0\u8868\u793a\u8bfb 1\u5199 2\u9519\u8bef<\/span><\/span>
<\/span>
\/\/\u76d1\u542c\u8bfb\u4e8b\u4ef6<\/span><\/span>
cnt=select<\/span>(sockfd+1<\/span>,&readfds,NULL<\/span>,NULL<\/span>,NULL<\/span>);<\/span>
if<\/span>(cnt)<\/span>
{<\/span>
if<\/span>(FD_ISSET<\/span>(sockfd,&readfds)) \/\/\u5224\u65ad\u6536\u5230\u670d\u52a1\u5668\u7684\u6d88\u606f<\/span><\/span>
{<\/span>
cnt=read<\/span>(sockfd,&msg_data,sizeof<\/span>(struct<\/span> MSG_DATA));<\/span>
if<\/span>(cnt<=0<\/span>) \/\/\u5224\u65ad\u670d\u52a1\u5668\u662f\u5426\u65ad\u5f00\u4e86\u8fde\u63a5<\/span><\/span>
{<\/span>
printf<\/span>(\"\u670d\u52a1\u5668\u5df2\u7ecf\u9000\u51fa.n\"<\/span>);<\/span>
break<\/span>;<\/span>
}<\/span>
else<\/span> if<\/span>(cnt>0<\/span>)<\/span>
{<\/span>
if<\/span>(msg_data.type==0<\/span>)<\/span>
{<\/span>
printf<\/span>(\"%s:%s \u5728\u7ebf\u4eba\u6570:%dn\"<\/span>,msg_data.name,msg_data.buff,msg_data.number);<\/span>
}<\/span>
else<\/span> if<\/span>(msg_data.type==1<\/span>)<\/span>
{<\/span>
printf<\/span>(\"%s \u597d\u53cb\u4e0a\u7ebf. \u5728\u7ebf\u4eba\u6570:%dn\"<\/span>,msg_data.name,msg_data.number);<\/span>
}<\/span>
else<\/span> if<\/span>(msg_data.type==2<\/span>)<\/span>
{<\/span>
printf<\/span>(\"%s \u597d\u53cb\u4e0b\u7ebf. \u5728\u7ebf\u4eba\u6570:%dn\"<\/span>,msg_data.name,msg_data.number);<\/span>
}<\/span>
}<\/span>
}<\/span>
<\/span>
if<\/span>(FD_ISSET<\/span>(0<\/span>,&readfds)) \/\/\u5224\u65ad\u952e\u76d8\u4e0a\u6709\u8f93\u5165<\/span><\/span>
{<\/span>
gets<\/span>(msg_data.buff); \/\/\u8bfb\u53d6\u952e\u76d8\u4e0a\u7684\u6d88\u606f<\/span><\/span>
msg_data.type=0<\/span>; \/\/\u8868\u793a\u6b63\u5e38\u6d88\u606f<\/span><\/span>
strcpy<\/span>(msg_data.name,argv[3<\/span>]); \/\/\u540d\u79f0<\/span><\/span>
write<\/span>(sockfd,&msg_data,sizeof<\/span>(struct<\/span> MSG_DATA));<\/span>
}<\/span>
}<\/span>
}<\/span>
close<\/span>(sockfd);<\/span>
return<\/span> 0<\/span>;<\/span>
}<\/span>
<\/span>
<\/code><\/pre>\n<\/p>\n

3.2select.c\u670d\u52a1\u5668\u7aef\u4ee3\u7801<\/p>\n

\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>
#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>
pthread_mutex_t<\/span> mutex; \/\/\u5b9a\u4e49\u4e92\u65a5\u9501<\/span><\/span>
int<\/span> sockfd;<\/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>
<\/span>
\/\/\u5b58\u653e\u5f53\u524d\u670d\u52a1\u5668\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<\/span><\/span>
struct<\/span> CLIENT_FD<\/span><\/span>
{<\/span>
int<\/span> fd;<\/span>
struct<\/span> CLIENT_FD<\/span> *next;<\/span>
};<\/span>
<\/span>
\/\/\u5b9a\u4e49\u94fe\u8868\u5934<\/span><\/span>
struct<\/span> CLIENT_FD<\/span> *list_head=NULL<\/span>;<\/span>
struct<\/span> CLIENT_FD<\/span> *List_CreateHead<\/span>(struct<\/span> CLIENT_FD *list_head);<\/span>
void<\/span> List_AddNode<\/span>(struct<\/span> CLIENT_FD *list_head,int<\/span> fd)<\/span><\/span>;<\/span>
void<\/span> List_DelNode<\/span>(struct<\/span> CLIENT_FD *list_head,int<\/span> fd)<\/span><\/span>;<\/span>
int<\/span> List_GetNodeCnt<\/span>(struct<\/span> CLIENT_FD *list_head)<\/span><\/span>;<\/span>
void<\/span> Server_SendMsgData<\/span>(struct<\/span> CLIENT_FD *list_head,struct<\/span> MSG_DATA *msg_data,int<\/span> client_fd)<\/span><\/span>;<\/span>
<\/span>
\/*<\/span><\/span>
\u7ebf\u7a0b\u5de5\u4f5c\u51fd\u6570<\/span>
*\/<\/span>
void<\/span> *thread_work_func<\/span>(void<\/span> *argv)<\/span><\/span><\/span>
{<\/span>
int<\/span> client_fd=*(int<\/span>*)argv;<\/span>
free<\/span>(argv);<\/span>
<\/span>
struct<\/span> MSG_DATA<\/span> msg_data;<\/span>
\/\/1. \u5c06\u65b0\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d<\/span><\/span>
List_AddNode<\/span>(list_head,client_fd);<\/span>
\/\/2. \u63a5\u6536\u5ba2\u6237\u7aef\u4fe1\u606f<\/span><\/span>
fd_set readfds;<\/span>
int<\/span> cnt;<\/span>
while<\/span>(1<\/span>)<\/span>
{<\/span>
FD_ZERO<\/span>(&readfds); \/\/\u6e05\u7a7a\u6574\u4e2a\u96c6\u5408\u3002<\/span><\/span>
FD_SET<\/span>(client_fd,&readfds); \/\/\u6dfb\u52a0\u8981\u76d1\u542c\u7684\u63cf\u8ff0\u7b26<\/span><\/span>
cnt=select<\/span>(client_fd+1<\/span>,&readfds,NULL<\/span>,NULL<\/span>,NULL<\/span>);<\/span>
if<\/span>(cnt>0<\/span>)<\/span>
{<\/span>
\/\/\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6d88\u606f<\/span><\/span>
cnt=read<\/span>(client_fd,&msg_data,sizeof<\/span>(struct<\/span> MSG_DATA));<\/span>
if<\/span>(cnt<=0<\/span>) \/\/\u8868\u793a\u5f53\u524d\u5ba2\u6237\u7aef\u65ad\u5f00\u4e86\u8fde\u63a5<\/span><\/span>
{<\/span>
List_DelNode<\/span>(list_head,client_fd); \/\/\u5220\u9664\u8282\u70b9<\/span><\/span>
msg_data.type=2<\/span>;<\/span>
}<\/span>
\/\/\u8f6c\u53d1\u6d88\u606f\u7ed9\u5176\u4ed6\u597d\u53cb<\/span><\/span>
msg_data.number=List_GetNodeCnt<\/span>(list_head); \/\/\u5f53\u524d\u5728\u7ebf\u597d\u53cb\u4eba\u6570<\/span><\/span>
Server_SendMsgData<\/span>(list_head,&msg_data,client_fd);<\/span>
if<\/span>(msg_data.type==2<\/span>)break<\/span>;<\/span>
}<\/span>
}<\/span>
close<\/span>(client_fd);<\/span>
}<\/span>
<\/span>
\/*<\/span><\/span>
\u4fe1\u53f7\u5de5\u4f5c\u51fd\u6570<\/span>
*\/<\/span>
void<\/span> signal_work_func<\/span>(int<\/span> sig)<\/span><\/span><\/span>
{<\/span>
\/\/\u9500\u6bc1\u4e92\u65a5\u9501<\/span><\/span>
pthread_mutex_destroy<\/span>(&mutex);<\/span>
close<\/span>(sockfd);<\/span>
exit<\/span>(0<\/span>); \/\/\u7ed3\u675f\u8fdb\u7a0b<\/span><\/span>
}<\/span>
<\/span>
int<\/span> main<\/span>(int<\/span> argc,char<\/span> **argv)<\/span><\/span><\/span>
{ <\/span>
if<\/span>(argc!=2<\/span>)<\/span>
{<\/span>
printf<\/span>(\".\/app n\"<\/span>);<\/span>
return<\/span> 0<\/span>;<\/span>
}<\/span>
\/\/\u521d\u59cb\u5316\u4e92\u65a5\u9501<\/span><\/span>
pthread_mutex_init<\/span>(&mutex,NULL<\/span>);<\/span>
signal<\/span>(SIGPIPE,SIG_IGN); \/\/\u5ffd\u7565 SIGPIPE \u4fe1\u53f7--\u9632\u6b62\u670d\u52a1\u5668\u5f02\u5e38\u9000\u51fa<\/span><\/span>
signal<\/span>(SIGINT,signal_work_func);<\/span>
<\/span>
\/\/\u521b\u5efa\u94fe\u8868\u5934<\/span><\/span>
list_head=List_CreateHead<\/span>(list_head);<\/span>
<\/span>
\/*1. \u521b\u5efasocket\u5957\u63a5\u5b57*\/<\/span><\/span>
sockfd=socket<\/span>(AF_INET,SOCK_STREAM,0<\/span>);<\/span>
<\/span>
\/\/\u8bbe\u7f6e\u7aef\u53e3\u53f7\u7684\u590d\u7528\u529f\u80fd<\/span><\/span>
int<\/span> on = 1<\/span>;<\/span>
setsockopt<\/span>(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof<\/span>(on));<\/span>
<\/span>
\/*2. \u7ed1\u5b9a\u7aef\u53e3\u53f7\u4e0eIP\u5730\u5740*\/<\/span><\/span>
struct<\/span> sockaddr_in<\/span> addr;<\/span>
addr.sin_family=AF_INET;<\/span>
addr.sin_port=htons<\/span>(atoi<\/span>(argv[1<\/span>])); \/\/ \u7aef\u53e3\u53f70~65535<\/span><\/span>
addr.sin_addr.s_addr=INADDR_ANY; \/\/inet_addr(\"0.0.0.0\"); \/\/IP\u5730\u5740<\/span><\/span>
if<\/span>(bind<\/span>(sockfd,(const<\/span> struct<\/span> sockaddr *)&addr,sizeof<\/span>(struct<\/span> sockaddr))!=0<\/span>)<\/span>
{<\/span>
printf<\/span>(\"\u670d\u52a1\u5668:\u7aef\u53e3\u53f7\u7ed1\u5b9a\u5931\u8d25.n\"<\/span>);<\/span>
}<\/span>
\/*3. \u8bbe\u7f6e\u76d1\u542c\u7684\u6570\u91cf*\/<\/span><\/span>
listen<\/span>(sockfd,20<\/span>);<\/span>
\/*4. \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5*\/<\/span><\/span>
int<\/span> *client_fd;<\/span>
struct<\/span> sockaddr_in<\/span> client_addr;<\/span>
socklen_t<\/span> addrlen;<\/span>
pthread_t<\/span> thread_id;<\/span>
while<\/span>(1<\/span>)<\/span>
{<\/span>
addrlen=sizeof<\/span>(struct<\/span> sockaddr_in);<\/span>
client_fd=malloc<\/span>(sizeof<\/span>(int<\/span>));<\/span>
*client_fd=accept<\/span>(sockfd,(struct<\/span> sockaddr *)&client_addr,&addrlen);<\/span>
if<\/span>(*client_fd<0<\/span>)<\/span>
{<\/span>
printf<\/span>(\"\u5ba2\u6237\u7aef\u8fde\u63a5\u5931\u8d25.n\"<\/span>);<\/span>
return<\/span> 0<\/span>;<\/span>
}<\/span>
printf<\/span>(\"\u8fde\u63a5\u7684\u5ba2\u6237\u7aefIP\u5730\u5740:%sn\"<\/span>,inet_ntoa<\/span>(client_addr.sin_addr));<\/span>
printf<\/span>(\"\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u7aef\u53e3\u53f7:%dn\"<\/span>,ntohs<\/span>(client_addr.sin_port));<\/span>
<\/span>
\/*\u521b\u5efa\u7ebf\u7a0b*\/<\/span><\/span>
if<\/span>(pthread_create<\/span>(&thread_id,NULL<\/span>,thread_work_func,client_fd))<\/span>
{<\/span>
printf<\/span>(\"\u7ebf\u7a0b\u521b\u5efa\u5931\u8d25.n\"<\/span>);<\/span>
break<\/span>;<\/span>
}<\/span>
\/*\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u5206\u79bb\u5c5e\u6027*\/<\/span><\/span>
pthread_detach<\/span>(thread_id);<\/span>
} <\/span>
\/\/\u9000\u51fa\u8fdb\u7a0b<\/span><\/span>
signal_work_func<\/span>(0<\/span>);<\/span>
return<\/span> 0<\/span>;<\/span>
}<\/span>
<\/span>
<\/span>
\/*<\/span><\/span>
\u51fd\u6570\u529f\u80fd: \u521b\u5efa\u94fe\u8868\u5934<\/span>
*\/<\/span>
struct<\/span> CLIENT_FD<\/span> *List_CreateHead<\/span>(struct<\/span> CLIENT_FD *list_head)<\/span>
{<\/span>
if<\/span>(list_head==NULL<\/span>)<\/span>
{<\/span>
list_head=malloc<\/span>(sizeof<\/span>(struct<\/span> CLIENT_FD));<\/span>
list_head->next=NULL<\/span>;<\/span>
}<\/span>
return<\/span> list_head;<\/span>
}<\/span>
<\/span>
\/*<\/span><\/span>
\u51fd\u6570\u529f\u80fd: \u6dfb\u52a0\u8282\u70b9<\/span>
*\/<\/span>
void<\/span> List_AddNode<\/span>(struct<\/span> CLIENT_FD *list_head,int<\/span> fd)<\/span><\/span><\/span>
{<\/span>
struct<\/span> CLIENT_FD<\/span> *p=list_head;<\/span>
struct<\/span> CLIENT_FD<\/span> *new_p;<\/span>
pthread_mutex_lock<\/span>(&mutex);<\/span>
while<\/span>(p->next!=NULL<\/span>)<\/span>
{<\/span>
p=p->next;<\/span>
}<\/span>
new_p=malloc<\/span>(sizeof<\/span>(struct<\/span> CLIENT_FD));<\/span>
new_p->next=NULL<\/span>;<\/span>
new_p->fd=fd;<\/span>
p->next=new_p;<\/span>
pthread_mutex_unlock<\/span>(&mutex);<\/span>
}<\/span>
<\/span>
\/*<\/span><\/span>
\u51fd\u6570\u529f\u80fd: \u5220\u9664\u8282\u70b9<\/span>
*\/<\/span>
void<\/span> List_DelNode<\/span>(struct<\/span> CLIENT_FD *list_head,int<\/span> fd)<\/span><\/span><\/span>
{<\/span>
struct<\/span> CLIENT_FD<\/span> *p=list_head;<\/span>
struct<\/span> CLIENT_FD<\/span> *tmp;<\/span>
pthread_mutex_lock<\/span>(&mutex);<\/span>
while<\/span>(p->next!=NULL<\/span>)<\/span>
{<\/span>
tmp=p;<\/span>
p=p->next;<\/span>
if<\/span>(p->fd==fd) \/\/\u627e\u5230\u4e86\u8981\u5220\u9664\u7684\u8282\u70b9<\/span><\/span>
{<\/span>
tmp->next=p->next;<\/span>
free<\/span>(p);<\/span>
break<\/span>;<\/span>
}<\/span>
}<\/span>
pthread_mutex_unlock<\/span>(&mutex);<\/span>
}<\/span>
<\/span>
\/*<\/span><\/span>
\u51fd\u6570\u529f\u80fd: \u83b7\u53d6\u5f53\u524d\u94fe\u8868\u4e2d\u6709\u591a\u5c11\u4e2a\u8282\u70b9<\/span>
*\/<\/span>
int<\/span> List_GetNodeCnt<\/span>(struct<\/span> CLIENT_FD *list_head)<\/span><\/span><\/span>
{<\/span>
int<\/span> cnt=0<\/span>;<\/span>
struct<\/span> CLIENT_FD<\/span> *p=list_head;<\/span>
pthread_mutex_lock<\/span>(&mutex);<\/span>
while<\/span>(p->next!=NULL<\/span>)<\/span>
{<\/span>
p=p->next;<\/span>
cnt++;<\/span>
}<\/span>
pthread_mutex_unlock<\/span>(&mutex);<\/span>
return<\/span> cnt;<\/span>
}<\/span>
<\/span>
\/*<\/span><\/span>
\u51fd\u6570\u529f\u80fd: \u8f6c\u53d1\u6d88\u606f<\/span>
*\/<\/span>
void<\/span> Server_SendMsgData<\/span>(struct<\/span> CLIENT_FD *list_head,struct<\/span> MSG_DATA *msg_data,int<\/span> client_fd)<\/span><\/span><\/span>
{<\/span>
struct<\/span> CLIENT_FD<\/span> *p=list_head;<\/span>
pthread_mutex_lock<\/span>(&mutex);<\/span>
while<\/span>(p->next!=NULL<\/span>)<\/span>
{<\/span>
p=p->next;<\/span>
if<\/span>(p->fd!=client_fd)<\/span>
{<\/span>
write<\/span>(p->fd,msg_data,sizeof<\/span>(struct<\/span> MSG_DATA));<\/span>
}<\/span>
}<\/span>
pthread_mutex_unlock<\/span>(&mutex);<\/span>
}<\/span>
<\/code>


<\/span><\/div><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fd9\u662f\u57fa\u4e8eLinux\u4e0b\u547d\u4ee4\u884c\u8bbe\u8ba1\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u7fa4\u804a\u5929\u7a0b\u5e8f\u3002\u7fa4\u804a\u7a0b\u5e8f\u5206\u4e3a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e24\u4e2a\u7a0b\u5e8f\u5ba2\u6237\u7aef\u76f8\u5f53\u4e8e\u4e00\u4e2a\u7528\u6237\uff0c\u5ba2\u6237\u7aef\u4ee3\u7801\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\uff0c\u8fde\u63a5\u5230\u670d\u52a1\u5668\u4e4b\u540e\uff0c\u4e92\u76f8\u53d1\u9001\u6d88\u606f\u8fdb\u884c\u804a\u5929\u3002<\/p>\n","protected":false},"author":1,"featured_media":294362,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[4264,4260,4263,4262,4261],"class_list":["post-294361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-cyylb","tag-lmlh-2","tag-shs-4","tag-dlijc","tag-qltcx"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/294361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=294361"}],"version-history":[{"count":0,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/294361\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/294362"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=294361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=294361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=294361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}