##2.6 epoll服务器
###2.6.1 服务端
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#define SERVER_PORT (7778)
#define EPOLL_MAX_NUM (2048)
#define BUFFER_MAX_LEN (4096)
char buffer[BUFFER_MAX_LEN];
void str_toupper(char *str)
{
int i;
for (i = 0; i < strlen(str); i ++) {
str[i] = toupper(str[i]);
}
}
int main(int argc, char **argv)
{
int listen_fd = 0;
int client_fd = 0;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
socklen_t client_len;
int epfd = 0;
struct epoll_event event, *my_events;
// socket
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
// bind
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
bind(listen_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
// listen
listen(listen_fd, 10);
// epoll create
epfd = epoll_create(EPOLL_MAX_NUM);
if (epfd < 0) {
perror("epoll create");
goto END;
}
// listen_fd -> epoll
event.events = EPOLLIN;
event.data.fd = listen_fd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &event) < 0) {
perror("epoll ctl add listen_fd ");
goto END;
}
my_events = malloc(sizeof(struct epoll_event) * EPOLL_MAX_NUM);
while (1) {
// epoll wait
int active_fds_cnt = epoll_wait(epfd, my_events, EPOLL_MAX_NUM, -1);
int i = 0;
for (i = 0; i < active_fds_cnt; i++) {
// if fd == listen_fd
if (my_events[i].data.fd == listen_fd) {
//accept
client_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept");
continue;
}
char ip[20];
printf("new connection[%s:%d]\n", inet_ntop(AF_INET, &client_addr.sin_addr, ip, sizeof(ip)), ntohs(client_addr.sin_port));
event.events = EPOLLIN | EPOLLET;
event.data.fd = client_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &event);
}
else if (my_events[i].events & EPOLLIN) {
printf("EPOLLIN\n");
client_fd = my_events[i].data.fd;
// do read
buffer[0] = '\0';
int n = read(client_fd, buffer, 5);
if (n < 0) {
perror("read");
continue;
}
else if (n == 0) {
epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &event);
close(client_fd);
}
else {
printf("[read]: %s\n", buffer);
buffer[n] = '\0';
#if 1
str_toupper(buffer);
write(client_fd, buffer, strlen(buffer));
printf("[write]: %s\n", buffer);
memset(buffer, 0, BUFFER_MAX_LEN);
#endif
/*
event.events = EPOLLOUT;
event.data.fd = client_fd;
epoll_ctl(epfd, EPOLL_CTL_MOD, client_fd, &event);
*/
}
}
else if (my_events[i].events & EPOLLOUT) {
printf("EPOLLOUT\n");
/*
client_fd = my_events[i].data.fd;
str_toupper(buffer);
write(client_fd, buffer, strlen(buffer));
printf("[write]: %s\n", buffer);
memset(buffer, 0, BUFFER_MAX_LEN);
event.events = EPOLLIN;
event.data.fd = client_fd;
epoll_ctl(epfd, EPOLL_CTL_MOD, client_fd, &event);
*/
}
}
}
END:
close(epfd);
close(listen_fd);
return 0;
}
```
###2.6.2 客户端
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_LINE (1024)
#define SERVER_PORT (7778)
void setnoblocking(int fd)
{
int opts = 0;
opts = fcntl(fd, F_GETFL);
opts = opts | O_NONBLOCK;
fcntl(fd, F_SETFL);
}
int main(int argc, char **argv)
{
int sockfd;
char recvline[MAX_LINE + 1] = {0};
struct sockaddr_in server_addr;
if (argc != 2) {
fprintf(stderr, "usage ./client <SERVER_IP>\n");
exit(0);
}
// 创建socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "socket error");
exit(0);
}
// server addr 赋值
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, argv[1], &server_addr.sin_addr) <= 0) {
fprintf(stderr, "inet_pton error for %s", argv[1]);
exit(0);
}
// 链接服务端
if (connect(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
perror("connect");
fprintf(stderr, "connect error\n");
exit(0);
}
setnoblocking(sockfd);
char input[100];
int n = 0;
int count = 0;
// 不断的从标准输入字符串
while (fgets(input, 100, stdin) != NULL)
{
printf("[send] %s\n", input);
n = 0;
// 把输入的字符串发送 到 服务器中去
n = send(sockfd, input, strlen(input), 0);
if (n < 0) {
perror("send");
}
n = 0;
count = 0;
// 读取 服务器返回的数据
while (1)
{
n = read(sockfd, recvline + count, MAX_LINE);
if (n == MAX_LINE)
{
count += n;
continue;
}
else if (n < 0){
perror("recv");
break;
}
else {
count += n;
recvline[count] = '\0';
printf("[recv] %s\n", recvline);
break;
}
}
}
return 0;
}
```
- 封面
- 1 Libevent官方
- 2 epoll
- 2.1 流-IO操作-阻塞
- 2.2 解决阻塞死等待的办法
- 2.3 什么是epoll
- 2.4 epollAPI
- 2.5 触发模式
- 2.6 简单的epoll服务器
- 3 epoll和reactor
- 3.1 reactor反应堆模式
- 3.2 epoll的反应堆模式实现
- 4 event_base
- 4.1 创建event_base
- 4.2 检查event_base后端
- 4.3 释放event_base
- 4.4 event_base优先级
- 4.5 event_base和fork
- 5 事件循环event_loop
- 5.1 运行循环
- 5.2 停止循环
- 5.3 转储event_base的状态
- 6 事件event
- 6.1 创建事件
- 6.2 事件的未决和非未决
- 6.3 事件的优先级
- 6.4 检查事件状态
- 6.5 一次触发事件
- 6.6 手动激活事件
- 6.7 事件状态之间的转换
- 7 数据缓冲Bufferevent
- 7.1 回调和水位
- 7.2 延迟回调
- 7.3 bufferevent 选项标志
- 7.4 使用bufferevent
- 7.5 通用bufferevent操作
- 7.5.1 释放bufferevent操作
- 7.5.2 操作回调、水位和启用/禁用
- 7.5.3 操作bufferevent中的数据
- 7.5.4 bufferevent的清空操作
- 8 数据封装evBuffer
- 8.1 创建和释放evbuffer
- 8.2 evbuffer与线程安全
- 8.3 检查evbuffer
- 8.4 向evbuffer添加数据
- 8.5 evbuffer数据移动
- 8.6 添加数据到evbuffer前
- 8 链接监听器evconnlistener
- 8.1 创建和释放 evconnlistener
- 8.2 启用和禁用 evconnlistener
- 8.3 调整 evconnlistener 的回调函数
- 8.4 检测 evconnlistener
- 8.5 侦测错误
- 9 libevent常用设置
- 9.1 日志消息回调设置
- 9.2 致命错误回调设置
- 9.3 内存管理回调设置
- 9.4 锁和线程的设置
- 9.5 调试事件的使用
- 10 基于libevent服务器
- 10.1 Hello_World服务器(基于信号)
- 10.2 基于事件服务器
- 10.3 回显服务器
- 10.3 libevent实现http服务器