复制代码
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134#include <sys/wait.h> #include <stdint.h> #include <sys/types.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <signal.h> #include <time.h> #include <string.h> #include <sys/times.h> #include <sys/epoll.h> #include <fcntl.h> #include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> /** * https://man7.org/linux/man-pages/man7/epoll.7.html The following system calls are provided to create and manage an epoll instance: • epoll_create(2) creates a new epoll instance and returns a file descriptor referring to that instance. (The more recent epoll_create1(2) extends the functionality of epoll_create(2).) • Interest in particular file descriptors is then registered via epoll_ctl(2), which adds items to the interest list of the epoll instance. • epoll_wait(2) waits for I/O events, blocking the calling thread if no events are currently available. (This system call can be thought of as fetching items from the ready list of the epoll instance.) */ #define MAX_EVENTS 10 int setnonblocking(int fd) { int flags; if (-1 == (flags = fcntl(fd, F_GETFL, 0))) flags = 0; return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } void do_use_fd(int fd, char *buffer) { printf("do_use_fd n"); while (1) { int length = read(fd, buffer, 1023); if (length <= 0) { printf("read error n"); perror("read error"); break; } buffer[length] = ''; printf("%s", buffer); write(fd, buffer, length); } } int main() { char buffer[1024] = { 0 }; int listenfd = 0; int conn_sock = 0; struct sockaddr_in address; int addrlen = sizeof(address); listenfd = socket(AF_INET, SOCK_STREAM, 0); memset(&address, '0', addrlen); address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_ANY); address.sin_port = htons(8080); bind(listenfd, (struct sockaddr*) &address, addrlen); if (listen(listenfd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } int epollfd; struct epoll_event ev; struct epoll_event events[MAX_EVENTS]; int nfds; epollfd = epoll_create1(0); if (epollfd == -1) { perror("epoll_create1"); exit(EXIT_FAILURE); } ev.events = EPOLLIN; ev.data.fd = listenfd; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) == -1) { perror("epoll_ctl: listen_sock"); exit(EXIT_FAILURE); } for (;;) { printf("epoll_wait n"); nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); if (nfds == -1) { perror("epoll_wait"); exit(EXIT_FAILURE); } printf("new msg n"); for (int n = 0; n < nfds; n++) { if (events[n].data.fd == listenfd) { conn_sock = accept(listenfd, (struct sockaddr*) &address, (socklen_t*) &addrlen); if (conn_sock < 0) { perror("accept"); exit(EXIT_FAILURE); } setnonblocking(conn_sock); ev.events = EPOLLIN | EPOLLET; ev.data.fd = conn_sock; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) { perror("epoll_ctl: conn_sock"); exit(EXIT_FAILURE); } } else { do_use_fd(events[n].data.fd, buffer); } } } shutdown(listenfd, SHUT_RDWR); return EXIT_SUCCESS; }
测试 : telnet 127.0.0.1 8080
可以打开2个终端来测试。
最后
以上就是爱听歌薯片最近收集整理的关于linux c 使用epoll处理socket连接以及客户端读写入门的全部内容,更多相关linux内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复