下面是使用 C 语言实现 epoll 的客户端和服务端代码示例。
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#define MAX_EVENTS 10
#define BUF_SIZE 256
int main(int argc, char* argv[])
{
int port = atoi(argv[1]); // 获取端口号
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket() failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind() failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 10) < 0) {
perror("listen() failed");
exit(EXIT_FAILURE);
}
printf("Listening on port %d...\n", port);
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1() failed");
exit(EXIT_FAILURE);
}
struct epoll_event event, events[MAX_EVENTS];
event.data.fd = server_fd;
event.events = EPOLLIN;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &event) < 0) {
perror("epoll_ctl() failed");
exit(EXIT_FAILURE);
}
char buf[BUF_SIZE];
while (1) {
int ready = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (ready < 0) {
perror("epoll_wait() failed");
exit(EXIT_FAILURE);
}
for (int i = 0; i < ready; i++) {
if (events[i].data.fd == server_fd) {
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept() failed");
exit(EXIT_FAILURE);
}
printf("Client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
event.data.fd = client_fd;
event.events = EPOLLIN;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &event) < 0) {
perror("epoll_ctl() failed");
exit(EXIT_FAILURE);
}
} else {
int client_fd = events[i].data.fd;
ssize_t count = recv(client_fd, buf, sizeof(buf), 0);
if (count < 0) {
perror("recv() failed");
exit(EXIT_FAILURE);
} else if (count == 0) {
printf("Client disconnected\n");
close(client_fd);
} else {
printf("Received %ld bytes from client: %.*s\n", count, (int)count, buf);
send(client_fd, buf, count, 0);
}
}
}
}
close(server_fd);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#define MAX_EVENTS 10
#define BUF_SIZE 256
int main(int argc, char* argv[])
{
const char* server_ip = argv[1];
int port = atoi(argv[2]);
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket() failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(server_ip);
server_addr.sin_port = htons(port);
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect() failed");
exit(EXIT_FAILURE);
}
printf("Connected to server: %s:%d\n", server_ip, port);
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1() failed");
exit(EXIT_FAILURE);
}
struct epoll_event event, events[MAX_EVENTS];
event.data.fd = client_fd;
event.events = EPOLLIN;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &event) < 0) {
perror("epoll_ctl() failed");
exit(EXIT_FAILURE);
}
char buf[BUF_SIZE];
while (1) {
int ready = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (ready < 0) {
perror("epoll_wait() failed");
exit(EXIT_FAILURE);
}
for (int i = 0; i < ready; i++) {
if (events[i].data.fd == client_fd) {
ssize_t count = recv(client_fd, buf, sizeof(buf), 0);
if (count < 0) {
perror("recv() failed");
exit(EXIT_FAILURE);
} else if (count == 0) {
printf("Server disconnected\n");
close(client_fd);
} else {
printf("Received %ld bytes from server: %.*s\n", count, (int)count, buf);
}
} else {
fgets(buf, sizeof(buf), stdin);
ssize_t count = send(client_fd, buf, strlen(buf), 0);
if (count < 0) {
perror("send() failed");
exit(EXIT_FAILURE);
}
}
}
}
close(client_fd);
return 0;
}
```
在这个示例中,我们使用了 epoll 来实现并发处理多个客户端的连接。服务器会等待客户端连接,一旦有客户端连接,就将其加入 epoll 等待队列中。同时,客户端也会连接服务器,并将其套接字加入 epoll 等待队列中。当客户端或服务器有消息到来时,都会触发 epoll_wait() 函数。然后,我们可以使用 recv() 和 send() 函数来接收和发送消息。
请注意,这只是一个示例代码,可能还需要进行更多的错误处理和安全检查。