使用epoll写一个Linux网络编程的例子
时间: 2024-05-08 13:18:52 浏览: 107
好的,这是一个使用epoll实现的简单的Linux网络编程例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#define MAX_EVENTS 1024
#define PORT 12345
int setnonblocking(int sockfd) {
int flags = fcntl(sockfd, F_GETFL, 0);
if (flags == -1) {
return -1;
}
flags |= O_NONBLOCK;
if (fcntl(sockfd, F_SETFL, flags) == -1) {
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
int listenfd, connfd, nfds, epollfd;
ssize_t n;
char buf[1024];
struct sockaddr_in servaddr, cliaddr;
struct epoll_event ev, events[MAX_EVENTS];
// 创建监听socket,绑定地址和端口
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(listenfd, 5);
// 创建epoll
epollfd = epoll_create1(0);
if (epollfd == -1) {
perror("epoll_create1");
exit(EXIT_FAILURE);
}
// 设置监听socket为非阻塞
if (setnonblocking(listenfd) < 0) {
perror("setnonblocking");
exit(EXIT_FAILURE);
}
// 添加监听socket事件到epoll
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listenfd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) == -1) {
perror("epoll_ctl: listenfd");
exit(EXIT_FAILURE);
}
// 循环处理epoll事件
for (;;) {
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if (nfds == -1) {
perror("epoll_wait");
exit(EXIT_FAILURE);
}
for (int i = 0; i < nfds; i++) {
if (events[i].data.fd == listenfd) { // 新连接
socklen_t clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
if (connfd == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
// 设置连接socket为非阻塞
if (setnonblocking(connfd) < 0) {
perror("setnonblocking");
exit(EXIT_FAILURE);
}
// 添加连接socket事件到epoll
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.fd = connfd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &ev) == -1) {
perror("epoll_ctl: connfd");
exit(EXIT_FAILURE);
}
// 打印新连接的客户端地址
printf("Accepted connection from %s:%d\n",
inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
} else { // 已连接
if (events[i].events & EPOLLIN) { // 可读
while ((n = read(events[i].data.fd, buf, sizeof(buf))) > 0) {
// 处理读取到的数据
// ...
}
if (n == 0) { // 对方关闭连接
close(events[i].data.fd);
printf("Closed connection on descriptor %d\n",
events[i].data.fd);
} else if (errno != EAGAIN && errno != EINTR) { // 发生错误
perror("read error");
close(events[i].data.fd);
}
} else if (events[i].events & EPOLLOUT) { // 可写
// 发送数据到连接socket
// ...
} else { // 其他事件
printf("Unknown event: %d\n", events[i].events);
}
}
}
}
// 关闭监听socket和epoll
close(listenfd);
close(epollfd);
return 0;
}
```
这个例子使用epoll监听TCP连接事件,支持EPOLLET边缘触发模式和非阻塞IO。当有新连接到来时,将连接socket添加到epoll中,并打印客户端地址;当连接socket可读或可写时,分别处理读取和发送数据。
阅读全文