/
epoll-9-wait.cpp
73 lines (59 loc) · 1.3 KB
/
epoll-9-wait.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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(){
sockaddr_in server;
epoll_event ev, ev_ret[EVENTS];
int sock, epfd;
char buf[65536];
int nfds;
int n;
sock = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_port = htons(12345);
inet_pton(AF_INET, "127.0.0.1", &server.sin_addr.s_addr);
n = connect(sock, (sockaddr*)&server, sizeof(server));
if(n != 0){
perror("connect");
return 1;
}
epfd = epoll_create(2);
if(epfd < 0){
perror("epfd");
return 1;
}
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLOUT;//可写
ev.data.fd = sock;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ev) != 0){
perror("epoll_clt");
return 1;
}
int cnt = 0;
while(1){
cnt++;
printf("before epoll wait\n");
nfds = epoll_wait(epfd, ev_ret, EVENTS, -1);
if(nfds < 0){
perror("epoll_wait");
return 1;
}
printf("after epoll_wait\n");
if(ev_ret[0].data.fd == sock){
printf("[%d]write %ld types\n", cnt, sizeof(buf));
n = write(sock, buf, sizeof(buf));
if(n <= 0){
printf("write error:%d\n", n);
break;
}
}
}
close(sock);
return 0;
}