c/c++ linux epoll系列1 建立epoll
阿新 • • 發佈:2018-11-08
linux epoll系列1 建立epoll
據說select和poll的弱點是,隨著連線(socket)的增加,效能會直線下降。
epoll不會隨著連線(socket)的增加,效能直線下降。
知識點:
1,epoll_wait函式是阻塞的,直到有socket發生變化。
2,epoll使用流程,先建立(epoll_create),再把socket新增到epoll裡(epoll_ctl),然後等待socket的變化(epoll_wait)
接收端,接收2個socket
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/epoll.h> #include <arpa/inet.h> #define EVENTS 12 int main(){ int sock1, sock2; sockaddr_in addr1, addr2; int epfd; epoll_event ev, ev_ret[EVENTS]; char buf[2048]; int i; int nfds; int n; //建立2個接受訊息的socket sock1 = socket(AF_INET, SOCK_DGRAM, 0); sock2 = socket(AF_INET, SOCK_DGRAM, 0); addr1.sin_family = AF_INET; addr2.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &addr1.sin_addr.s_addr); inet_pton(AF_INET, "127.0.0.1", &addr2.sin_addr.s_addr); addr1.sin_port = htons(11111); addr2.sin_port = htons(22222); bind(sock1, (sockaddr*)&addr1, sizeof(addr1)); bind(sock2, (sockaddr*)&addr2, sizeof(addr2)); //引數不小於0就行 epfd = epoll_create(1); if(epfd < 0){ perror("epoll_create"); return 1; } memset(&ev, 0, sizeof(ev)); ev.events = EPOLLIN;//只讀 ev.data.fd = sock1;//把sock1加到epoll if(epoll_ctl(epfd, EPOLL_CTL_ADD, sock1, &ev) != 0){ perror("epoll_ctl"); return 1; } memset(&ev, 0, sizeof(ev)); ev.events = EPOLLIN;//只讀 ev.data.fd = sock2;//把sock2加到epoll if(epoll_ctl(epfd, EPOLL_CTL_ADD, sock2, &ev) != 0){ perror("epoll_ctl"); return 1; } while(1){ printf("before epoll_wait\n"); //在這裡會阻塞,直到有socket進來. nfds = epoll_wait(epfd, ev_ret, EVENTS, -1); if(nfds <= 0){ perror("epoll_wait"); return 1; } printf("after epoll_wait\n"); for(i = 0; i < nfds; ++i){ //判斷進來的socket是哪個socket if(ev_ret[i].data.fd == sock1){ //從sock1讀取資料,並寫入到標準輸出 n = recv(sock1, buf, sizeof(buf), 0); write(fileno(stdout), buf, n); } //判斷進來的socket是哪個socket else if(ev_ret[i].data.fd == sock2){ //從sock1讀取資料,並寫入到標準輸出 n = recv(sock2, buf, sizeof(buf), 0); write(fileno(stdout), buf, n); } } } close(sock1); close(sock2); return 0; }
傳送端,向2個地址傳送資訊
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/epoll.h> #include <arpa/inet.h> int main(){ int sock; sockaddr_in dest1, dest2; char buf[1024]; sock = socket(AF_INET, SOCK_DGRAM, 0); dest1.sin_family = AF_INET; dest2.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &dest1.sin_addr.s_addr); inet_pton(AF_INET, "127.0.0.1", &dest2.sin_addr.s_addr); dest1.sin_port = htons(11111); dest2.sin_port = htons(22222); strcpy(buf, "data to port 11111\n"); //給地址1(dest1)送信 sendto(sock, buf, strlen(buf), 0, (sockaddr*)&dest1, sizeof(dest1)); strcpy(buf, "data to port 22222\n"); //給地址2(dest2)送信 sendto(sock, buf, strlen(buf), 0, (sockaddr*)&dest2, sizeof(dest1)); close(sock); return 0; }
執行方法:
先執行接收端,結果如下:
before epoll_wait
再執行傳送端,結果如下:
before epoll_wait
after epoll_wait
data to port 11111
before epoll_wait
after epoll_wait
data to port 22222
before epoll_wait