以下是一个简单的C语言事件驱动案例,使用了Linux下的epoll机制:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAX_EVENTS 10
int main(int argc, char *argv[]) {
int server_fd, client_fd, epoll_fd, n, i;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
struct epoll_event event, events[MAX_EVENTS];
char buffer[1024];
// 创建socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 绑定地址和端口
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// 监听socket
if (listen(server_fd, 5) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
// 创建epoll
if ((epoll_fd = epoll_create1(0)) == -1) {
perror("epoll_create1");
exit(EXIT_FAILURE);
}
// 将server_fd添加到epoll
event.data.fd = server_fd;
event.events = EPOLLIN;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &event) == -1) {
perror("epoll_ctl: server_fd");
exit(EXIT_FAILURE);
}
while (1) {
// 等待事件
n = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (n == -1) {
perror("epoll_wait");
exit(EXIT_FAILURE);
}
// 处理事件
for (i = 0; i < n; i++) {
if (events[i].data.fd == server_fd) { // 新连接
client_addr_len = sizeof(client_addr);
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len)) == -1) {
perror("accept");
} else {
printf("New connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// 将client_fd添加到epoll
event.data.fd = client_fd;
event.events = EPOLLIN;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &event) == -1) {
perror("epoll_ctl: client_fd");
exit(EXIT_FAILURE);
}
}
} else { // 读取数据
if ((n = read(events[i].data.fd, buffer, sizeof(buffer))) == -1) {
perror("read");
} else if (n == 0) {
printf("Connection closed by client\n");
// 关闭连接并从epoll中删除
close(events[i].data.fd);
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
} else {
printf("Received %d bytes from client: %.*s\n", n, n, buffer);
// 回复数据
if (write(events[i].data.fd, buffer, n) == -1) {
perror("write");
}
}
}
}
}
return 0;
}
这个程序会监听指定的端口,当有客户端连接时,将客户端的socket添加到epoll中,并等待事件。当有数据可读时,读取数据并回复相同的数据。当客户端关闭连接时,关闭socket并从epoll中删除。这个程序使用了非阻塞IO和事件驱动的编程模式,可以处理多个连接和并发读写。