Epoll相关的API:
int epoll_create(int size);
epoll在内核中维护一个事件表,将用户关心的文件描述符都放进去,因此无需像select和poll那样每次调用都要重复传入文件描述符或事件集,但epoll需要一个额外的文件描述符来唯一标识内核中的这个事件表。
int epoll_ctl(int epfd, int op, int fd, struct epoll_event* event);
op:
EPOLL_CTL_ADD:往事件表中注册fd上的事件
EPOLL_CTL_MOD:修改fd上注册的事件
EPOLL_CTL_DEL:删除fd上注册的事件
event:该参数指定要传入的文件描述符上关注的事件,它是epoll_event结构体指针类型,epoll_event定义如下
struct epoll_event{
__uint32_t events;
epoll_data_t data;
};
其中data用于存储用户数据,它是一个共用体,如下是data定义:
typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
}epoll_data_t;
int epoll_wait(int epfd, struct_event* events, int maxevents, int timeout);
- epfd:内核事件表
- events:用户指定的写入就绪事件的结构体数组,由内核来修改
- maxevents:最大监听事件个数
- timeout:阻塞时间,含义与poll中相同
- 返回值:就绪文件描述符个数
如下是用epoll实现的服务端代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/epoll.h>
void err_exit(char *msg)
{
perror(msg);
exit(1);
}
int create_socket(const char *ip, int port)
{
struct sockaddr_in server_addr;
int sockfd, reuse = 1;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if (inet_pton(PF_INET, ip, &server_addr.sin_addr) == -1)
err_exit("inet_pton() error");
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
err_exit("socket() error");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
err_exit("setsockopt() error");
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
err_exit("bind() error");
if (listen(sockfd, 5) == -1)
err_exit("listen() error");
return sockfd;
}
int main()
{
int listenFd = create_socket("127.0.0.1", 9977);
printf("create listen fd:%d\n", listenFd);
int epfd = epoll_create(10);
printf("create epoll fd:%d\n", epfd);
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = listenFd;
epoll_ctl(epfd, EPOLL_CTL_ADD, listenFd, &event);
while(1)
{
struct epoll_event events[100];
int num = epoll_wait(epfd, events, 100, -1);
printf("epoll wait fd num:%d\n", num);
int i = 0;
for(i = 0; i < num; i++)
{
if(events[i].events & EPOLLRDHUP)
{
int fd = events[i].data.fd;
printf("%d closed\n", fd);
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
}
else if(events[i].events & EPOLLIN)
{
int fd = events[i].data.fd;
if(listenFd == events[i].data.fd)
{
struct sockaddr_in cli;
socklen_t len = sizeof(cli);
int clientFd = accept(listenFd, (struct sockaddr*)&cli, &len);
char ip[16];
memset(ip,0,sizeof(ip));
inet_ntop(AF_INET,&cli.sin_addr.s_addr,ip,sizeof(ip));
printf("client[%s:%d] has connected to server!\n", ip, ntohs(cli.sin_port));
struct epoll_event event;
event.events = EPOLLIN|EPOLLRDHUP;
event.data.fd = clientFd;
epoll_ctl(epfd, EPOLL_CTL_ADD, clientFd, &event);
}else
{
char buff[128] = {0};
int ret = recv(fd, buff, 127, 0);
printf("%d: %s\n", fd, buff);
send(fd, "OK", 2, 0);
}
}
}
}
return 0;
}
tcp实现的客户端:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/epoll.h>
int main()
{
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
perror("create socket failed\n");
exit(-1);
}
printf("tcp client fd:%d\n", sockfd);
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(9977);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sockfd, (struct sockaddr*)&ser, sizeof(ser)) < 0)
{
perror("connect error");
exit(-1);
}
while(1)
{
char buf[128] = {0x00};
int i = 0;
sprintf(buf, "tcp client send msg count:%d", ++i);
if(send(sockfd, buf, strlen(buf), 0) < 0)
printf("tcp client send failed.\n");
memset(buf, 0, sizeof(buf));
ssize_t recvLen = recv(sockfd, buf, sizeof(buf)-1, 0);
if(recvLen < 0)
printf("tcp client recv failed, ret value:%d\n",recvLen);
printf("tcp client recv data:[%s]\n", buf);
sleep(5);
}
return 0;
}
|