亚洲国产成人,色呦呦内射午夜,无码一级片,无码人妻少妇色欲AV一区二区

<samp id="jg8hh"></samp>

<p id="jg8hh"></p><delect id="jg8hh"><em id="jg8hh"><blockquote id="jg8hh"></blockquote></em></delect><acronym id="jg8hh"><dd id="jg8hh"></dd></acronym><button id="jg8hh"><dd id="jg8hh"><acronym id="jg8hh"></acronym></dd></button><samp id="jg8hh"><em id="jg8hh"><blockquote id="jg8hh"></blockquote></em></samp>

<p id="jg8hh"></p>

<samp id="jg8hh"><legend id="jg8hh"></legend></samp>
<samp id="jg8hh"><legend id="jg8hh"><samp id="jg8hh"></samp></legend></samp>

<samp id="jg8hh"></samp>

<p id="jg8hh"></p><acronym id="jg8hh"></acronym><p id="jg8hh"><dd id="jg8hh"><acronym id="jg8hh"></acronym></dd></p><p id="jg8hh"></p>

<p id="jg8hh"></p><delect id="jg8hh"><legend id="jg8hh"><var id="jg8hh"></var></legend></delect><button id="jg8hh"><listing id="jg8hh"><i id="jg8hh"></i></listing></button>
<delect id="jg8hh"><legend id="jg8hh"><var id="jg8hh"></var></legend></delect>

Linux IO多路復(fù)用之epoll網(wǎng)絡(luò)編程

發(fā)布時間:2024-04-14
本文是用基本的linux基本函數(shù)加上epoll調(diào)用編寫一個完整的服務(wù)器和客戶端例子,可在linux上運行,客戶端和服務(wù)端的功能如下:
客戶端從標準輸入讀入一行,發(fā)送到服務(wù)端服務(wù)端從網(wǎng)絡(luò)讀取一行,然后輸出到客戶端客戶端收到服務(wù)端的響應(yīng),輸出這一行到標準輸出服務(wù)端
代碼如下:
#include <unistd.h> #include <sys/types.h> /* basic system data types */ #include <sys/socket.h> /* basic socket definitions */ #include <netinet/in.h> /* sockaddr_in{} and other internet defns */ #include <arpa/inet.h> /* inet(3) functions */ #include <sys/epoll.h> /* epoll function */ #include <fcntl.h> /* nonblocking */ #include <sys/resource.h> /*setrlimit */ #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> #define maxepollsize 10000 #define maxline 10240 int handle(int connfd); int setnonblocking(int sockfd) { if (fcntl(sockfd, f_setfl, fcntl(sockfd, f_getfd, 0)|o_nonblock) == -1) { return -1; } return 0; } int main(int argc, char argv) { int servport = 6888; int listenq = 1024; int listenfd, connfd, kdpfd, nfds, n, nread, curfds,acceptcount = 0; struct sockaddr_in servaddr, cliaddr; socklen_t socklen = sizeof(struct sockaddr_in); struct epoll_event ev; struct epoll_event events[maxepollsize]; struct rlimit rt; char buf[maxline]; /* 設(shè)置每個進程允許打開的最大文件數(shù) */ rt.rlim_max = rt.rlim_cur = maxepollsize; if (setrlimit(rlimit_nofile, &rt) == -1) { perror(setrlimit error); return -1; } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = af_inet; servaddr.sin_addr.s_addr = htonl (inaddr_any); servaddr.sin_port = htons (servport); listenfd = socket(af_inet, sock_stream, 0); if (listenfd == -1) { perror(can't create socket file); return -1; } int opt = 1; setsockopt(listenfd, sol_socket, so_reuseaddr, &opt, sizeof(opt)); if (setnonblocking(listenfd) < 0) { perror(setnonblock error); } if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) == -1) { perror(bind error); return -1; } if (listen(listenfd, listenq) == -1) { perror(listen error); return -1; } /* 創(chuàng)建 epoll 句柄,把監(jiān)聽 socket 加入到 epoll 集合里 */ kdpfd = epoll_create(maxepollsize); ev.events = epollin | epollet; ev.data.fd = listenfd; if (epoll_ctl(kdpfd, epoll_ctl_add, listenfd, &ev) < 0) { fprintf(stderr, epoll set insertion error: fd=%d\n, listenfd); return -1; } curfds = 1; printf(epollserver startup,port %d, max connection is %d, backlog is %d\n, servport, maxepollsize, listenq); for (;;) { /* 等待有事件發(fā)生 */ nfds = epoll_wait(kdpfd, events, curfds, -1); if (nfds == -1) { perror(epoll_wait); continue; } /* 處理所有事件 */ for (n = 0; n < nfds; ++n) { if (events[n].data.fd == listenfd) { connfd = accept(listenfd, (struct sockaddr *)&cliaddr,&socklen); if (connfd < 0) { perror(accept error); continue; } sprintf(buf, accept form %s:%d\n, inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port); printf(%d:%s, ++acceptcount, buf); if (curfds >= maxepollsize) { fprintf(stderr, too many connection, more than %d\n, maxepollsize); close(connfd); continue; } if (setnonblocking(connfd) < 0) { perror(setnonblocking error); } ev.events = epollin | epollet; ev.data.fd = connfd; if (epoll_ctl(kdpfd, epoll_ctl_add, connfd, &ev) < 0) { fprintf(stderr, add socket '%d' to epoll failed: %s\n, connfd, strerror(errno)); return -1; } curfds++; continue; } // 處理客戶端請求 if (handle(events[n].data.fd) < 0) { epoll_ctl(kdpfd, epoll_ctl_del, events[n].data.fd,&ev); curfds--; } } } close(listenfd); return 0; } int handle(int connfd) { int nread; char buf[maxline]; nread = read(connfd, buf, maxline);//讀取客戶端socket流 if (nread == 0) { printf(client close the connection\n); close(connfd); return -1; } if (nread < 0) { perror(read error); close(connfd); return -1; } write(connfd, buf, nread);//響應(yīng)客戶端 return 0; }編譯
編譯和啟動服務(wù)端
gcc epollserver.c -o epollserver ./epollserver
上一個:電腦主機配置怎么搭配,電腦配置要怎么配最好
下一個:華為nova5pro是不是曲屏(華為nova 5 pro是曲屏嗎)

橋上或隧道內(nèi)的管線敷設(shè)應(yīng)符合哪些規(guī)定?
免拆巖棉一體板生產(chǎn)廠家
觸摸屏高低溫試驗箱在各個行業(yè)試驗設(shè)備中起到了什么作用
聚氨酯硬泡復(fù)合保溫板生產(chǎn)步驟
聚醚砜
工業(yè)廢水處理設(shè)備的發(fā)展機遇
1-20公斤五谷雜糧顆粒自動稱重包裝機廠家
浮筒的6大特色介紹
今夜喝茶不設(shè)防
oiltech公司的電機馬達應(yīng)用舉例