c/c++ linux epoll系列3 利用epoll_wait設置timeout時間長度
阿新 • • 發佈:2018-10-21
取數據 超過 lse 返回 creat git out des poll
linux epoll系列3 利用epoll_wait設置timeout時間長度
epoll_wait函數的第四個參數可以設置,epoll_wait函數的等待時間(timeout時間長度)。
例子1,是接收端。
例子2,是發送端。
例子1,接收端
#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"); //在這裏會阻塞,只等待10秒,超過10秒,epoll_wait函數結束,返回0給nfds. nfds = epoll_wait(epfd, ev_ret, EVENTS, 10*1000); if(nfds < 0){ perror("epoll_wait"); return 1; } printf("after epoll_wait\n"); if(nfds == 0){ printf("timeout\n"); break; } 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; }
github源代碼
例子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; }
github源代碼
運行方法:先運行接收端,再運行發送端。
c/c++ 學習互助QQ群:877684253
本人微信:xiaoshitou5854
c/c++ linux epoll系列3 利用epoll_wait設置timeout時間長度