4.29和4.30、epoll的API介绍和代码编写
- 1.epoll的IO多路复用使用流程图
- 2.epoll的相关函数的使用介绍
- 3.epoll代码编写
-
1.epoll的IO多路复用使用流程图
2.epoll的相关函数的使用介绍
#include
int epoll_create(int size);
- 参数:
size : 目前没有意义了。随便写一个数,必须大于0
- 返回值:
-1 : 失败
> 0 : 文件描述符,操作epoll实例的
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event {
uint32_t events;
epoll_data_t data;
};
常见的Epoll检测事件:
- EPOLLIN
- EPOLLOUT
- EPOLLERR
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
- 参数:
- epfd : epoll实例对应的文件描述符
- op : 要进行什么操作
EPOLL_CTL_ADD: 添加
EPOLL_CTL_MOD: 修改
EPOLL_CTL_DEL: 删除
- fd : 要检测的文件描述符
- event : 检测文件描述符什么事情
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
- 参数:
- epfd : epoll实例对应的文件描述符
- events : 传出参数,保存了发送了变化的文件描述符的信息
- maxevents : 第二个参数结构体数组的大小
- timeout : 阻塞时间
- 0 : 不阻塞
- -1 : 阻塞,直到检测到fd数据发生变化,解除阻塞
- > 0 : 阻塞的时长(毫秒)
- 返回值:
- 成功,返回发送变化的文件描述符的个数 > 0
- 失败 -1
3.epoll代码编写
①epoll服务端
#include
#include
#include
#include
#include
using namespace std;
int main() {
int server_listen_fd = socket(PF_INET, SOCK_STREAM, 0);
sockaddr_in server_addr;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(9999);
bind(server_listen_fd, (sockaddr *)&server_addr, sizeof(server_addr));
listen(server_listen_fd, 8);
int epoll_fd = epoll_create(2);
epoll_event event;
event.events = EPOLLIN;
event.data.fd = server_listen_fd;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_listen_fd, &event);
epoll_event events[1024];
int events_len = 1;
while (1) {
int change_len = epoll_wait(epoll_fd, events, events_len, -1);
if (change_len == -1) {
perror("epoll_wait");
exit(-1);
} else if (change_len == 0) {
continue;
} else if (change_len > 0) {
cout << "epoll wait ret: " << change_len << endl;
for (int i = 0; i < change_len; i ++ ) {
if (events[i].data.fd == server_listen_fd) {
sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(server_listen_fd, (sockaddr *)&client_addr, &client_addr_len);
event.events = EPOLLIN;
event.data.fd = client_fd;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &event);
events_len ++ ;
} else if ((events[i].events & EPOLLIN) == EPOLLIN) {
char buf[1024];
int recv_ret = recv(events[i].data.fd, buf, sizeof(buf), 0);
if (recv_ret == -1) {
perror("recv");
exit(-1);
} else if (recv_ret == 0) {
cout << "clinet closed..." << endl;
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
close(events[i].data.fd);
events_len -- ;
} else if (recv_ret > 0) {
printf("I am server, data: %s\n", buf);
send(events[i].data.fd, buf, strlen(buf) + 1, 0);
}
}
}
}
}
close(epoll_fd);
close(server_listen_fd);
return 0;
}
②客户端(没变,和之前一样)
#include
#include
#include
#include
using namespace std;
int main()
{
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd == -1) {
perror("socket");
exit(-1);
}
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
int pton_ret = inet_pton(AF_INET, "xxx.xxx.xxx.xxx", &server_addr.sin_addr.s_addr);
server_addr.sin_port = htons(9999);
int cone_ret = connect(client_fd, (sockaddr *)&server_addr, sizeof(server_addr));
if (cone_ret == -1) {
perror("connect");
exit(-1);
}
int num = 0;
char recvBuf[1024] = {0};
while (1) {
sprintf(recvBuf, "hello, I am client: %d\n", num ++ );
write(client_fd, recvBuf, strlen(recvBuf) + 1);
int read_ret = read(client_fd, recvBuf, sizeof(recvBuf));
if (read_ret == -1) {
perror("read");
exit(-1);
} else if (read_ret > 0) {
printf("recv client data is %s\n", recvBuf);
} else if (read_ret == 0){
cout << "server closed..." << endl;
break;
}
sleep(1);
}
close(client_fd);
return 0;
}