# 10.1 Hello_World服务器
```cpp
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#ifndef WIN32
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
# include <arpa/inet.h>
# endif
#include <sys/socket.h>
#endif
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>
static const char MESSAGE[] = "Hello, World!\n";
static const int PORT = 9995;
static void listener_cb(struct evconnlistener *, evutil_socket_t,
struct sockaddr *, int socklen, void *);
static void conn_writecb(struct bufferevent *, void *);
static void conn_eventcb(struct bufferevent *, short, void *);
static void signal_cb(evutil_socket_t, short, void *);
int
main(int argc, char **argv)
{
struct event_base *base;
struct evconnlistener *listener;
struct event *signal_event;
struct sockaddr_in sin;
#ifdef WIN32
WSADATA wsa_data;
WSAStartup(0x0201, &wsa_data);
#endif
base = event_base_new();
if (!base) {
fprintf(stderr, "Could not initialize libevent!\n");
return 1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
(struct sockaddr*)&sin,
sizeof(sin));
if (!listener) {
fprintf(stderr, "Could not create a listener!\n");
return 1;
}
signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
if (!signal_event || event_add(signal_event, NULL)<0) {
fprintf(stderr, "Could not create/add a signal event!\n");
return 1;
}
event_base_dispatch(base);
evconnlistener_free(listener);
event_free(signal_event);
event_base_free(base);
printf("done\n");
return 0;
}
static void
listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
struct sockaddr *sa, int socklen, void *user_data)
{
struct event_base *base = user_data;
struct bufferevent *bev;
bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
if (!bev) {
fprintf(stderr, "Error constructing bufferevent!");
event_base_loopbreak(base);
return;
}
bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
bufferevent_enable(bev, EV_WRITE);
bufferevent_disable(bev, EV_READ);
bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
}
static void
conn_writecb(struct bufferevent *bev, void *user_data)
{
struct evbuffer *output = bufferevent_get_output(bev);
if (evbuffer_get_length(output) == 0) {
printf("flushed answer\n");
bufferevent_free(bev);
}
}
static void
conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{
if (events & BEV_EVENT_EOF) {
printf("Connection closed.\n");
} else if (events & BEV_EVENT_ERROR) {
printf("Got an error on the connection: %s\n",
strerror(errno));/*XXX win32*/
}
/* None of the other events can happen here, since we haven't enabled
* timeouts */
bufferevent_free(bev);
}
static void
signal_cb(evutil_socket_t sig, short events, void *user_data)
{
struct event_base *base = user_data;
struct timeval delay = { 2, 0 };
printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
event_base_loopexit(base, &delay);
}
```
- 封面
- 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服务器