Linux 初识Libevent网络库
初识Libevent
libevent是用c写的高并发网络io库,只要有文件描述符,就都可使用libevent。
libevent使用回调函数(callback) 。
有了libevent,网络编程我有
1, FIFO的进程间通信。
利用FIFO的进程间通信read端:
#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MYFIFO "myfifo"
//call back
void readcb(evutil_socket_t fd, short what, void* arg){
//read fifo
char buf[24] = {0};
int len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
printf("data len = %d, buf = %s\n", len, buf);
printf("read event:%s\n", what & EV_READ ? "Yes" : "No");
}
int main(){
unlink(MYFIFO);
mkfifo(MYFIFO, 0664);
int fd = open(MYFIFO, O_RDONLY | O_NONBLOCK);
//int fd = open(MYFIFO, O_RDONLY);
struct event_base* base;
base = event_base_new();
//create event
struct event* ev = NULL;
ev = event_new(base, fd, EV_READ | EV_PERSIST | EV_ET, readcb, NULL);
//add event
event_add(ev, NULL);
//start
event_base_dispatch(base);
//free event
event_free(ev);
event_base_free(base);
close(fd);
return 0;
}
利用FIFO的进程间通信write端:
#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MYFIFO "myfifo"
//call back
void writecb(evutil_socket_t fd, short what, void* arg){
//write fifo
char buf[24] = {0};
static int num = 0;
sprintf(buf, "num = %d", num++);
write(fd, buf, strlen(buf) + 1);
}
int main(){
int fd = open(MYFIFO, O_WRONLY | O_NONBLOCK);
struct event_base* base;
base = event_base_new();
//create event
struct event* ev = NULL;
ev = event_new(base, fd, EV_WRITE | EV_PERSIST | EV_ET, writecb, NULL);
//add event
event_add(ev, NULL);
//start
event_base_dispatch(base);
//free event
event_free(ev);
event_base_free(base);
close(fd);
return 0;
}
2, socket通信。
server端:
#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <event2/bufferevent.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <event2/listener.h>
//write callback
void write_cb(struct bufferevent* bev, void* ctx){
printf("all is sent\n");
}
//read callback
void read_cb(struct bufferevent *bev, void *ctx){
char buf[64];
size_t ret = bufferevent_read(bev, buf, sizeof(buf));
buf[ret] = '\0';
printf("server recf:%s\n", buf);
bufferevent_write(bev, "hahaha", 6);
}
//event callback
void event_cb(struct bufferevent *bev, short what, void *ctx){
if(what & BEV_EVENT_EOF){
printf("EOF\n");
}
if(what & BEV_EVENT_CONNECTED){
printf("connected\n");
}
}
//listener call back
void listencb(struct evconnlistener* listener, evutil_socket_t fd,
struct sockaddr* cli, int len, void* ptr){
struct event_base* base = evconnlistener_get_base(listener);
struct bufferevent* bev =
bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
//set callback function
bufferevent_setcb(bev, read_cb, write_cb, event_cb, NULL);
bufferevent_enable(bev, EV_READ | EV_WRITE);
//set water
bufferevent_setwatermark(bev, EV_READ, 10, 0);
bufferevent_setwatermark(bev, EV_WRITE, 1, 2);
}
int main(int argc, char** argv){
int port = atoi(argv[1]);
struct event_base* base;
base = event_base_new();
if(!base){
perror("event_base_new");
exit(1);
}
struct sockaddr_in s;
s.sin_family = AF_INET;
s.sin_port = htons(port);
s.sin_addr.s_addr = htonl(INADDR_ANY);
struct evconnlistener* listener =
evconnlistener_new_bind(base, listencb, NULL, LEV_OPT_CLOSE_ON_FREE,
-1, (struct sockaddr*)&s, sizeof(s));
if(!listener){
perror("bind");
exit(1);
}
event_base_dispatch(base);
event_base_free(base);
}
client端:
#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <event2/bufferevent.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <event2/listener.h>
#include <event2/dns.h>
#include <stdlib.h>
#include <event2/util.h>
//read write callback
void readcb(struct bufferevent *bev, void *ctx){
char buf[64];
size_t ret = bufferevent_read(bev, buf, sizeof(buf));
buf[ret] = '\0';
printf("client recf:%s\n", buf);
}
//event callback
void eventcb(struct bufferevent *bev, short what, void *ctx){
if(what & BEV_EVENT_CONNECTED){
printf("connect okay\n");
}
else if(what & BEV_EVENT_ERROR){
struct event_base* base = ctx;
int err = bufferevent_socket_get_dns_error(bev);
if(err){
printf("DNS error:%s\n", evutil_gai_strerror(err));
}
printf("closing\n");
bufferevent_free(bev);
event_base_loopexit(base, NULL);
}
}
//terminal read callback
void termicb(evutil_socket_t fd, short what, void* ptr){
char buf[64] = {0};
int len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
printf("in termicb\n");
struct bufferevent* bev = ptr;
bufferevent_write(bev, buf, len);
}
int main(int artc, char** argv){
struct event_base* base;
base = event_base_new();
struct evdns_base* dns_base;
dns_base = evdns_base_new(base, 1);
struct bufferevent* bev;
bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
int port = atoi(argv[2]);
bufferevent_setcb(bev, readcb, NULL, eventcb, base);
bufferevent_enable(bev, EV_READ);
bufferevent_socket_connect_hostname(bev, dns_base, AF_INET, argv[1], port);
struct event* ev = event_new(base, STDIN_FILENO, EV_READ | EV_PERSIST, termicb, bev);
event_add(ev, NULL);
event_base_dispatch(base);
event_base_free(base);
}
c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854
Linux 初识Libevent网络库的更多相关文章
- libevent网络库
1.概述 libevent是一个C语言编写的.轻量级开源高性能事件通知库.作为底层网络库,已经被广泛应用(如:memcached.Vomit.Nylon.Netchat等).主要有以下几个亮点: 事件 ...
- 以libevent网络库为引:网络通信和多线程
1. windows下编译及使用libevent http://www.cnblogs.com/luxiaoxun/p/3603399.html 2. <<libevent学习资料&g ...
- libevent 网络库安装
./configure prefix=/tools/libevent make sudo make install
- [原]网络库libevent在Visual Studio中的使用方法
libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名分布式缓存软件memcached也 ...
- 轻量级网络库libevent初探
本文是关于libevent库第一篇博文,主要由例子来说明如何利用该库.后续博文再深入研究该库原理. libevent库简介 就如libevent官网上所写的“libevent - an event n ...
- 开源网络库ACE、Boost的ASIO、libevent、libev、ZeroMQ
开源C/C++网络库:ACE C++语言 跨平台Boost的ASIO C++语言 跨平台libevent C语言 主要支持linux,新版增加了对windows的IOC ...
- 网络库libevent、libev、libuv对比
Libevent.libev.libuv三个网络库,都是c语言实现的异步事件库Asynchronousevent library). 异步事件库本质上是提供异步事件通知(Asynchronous Ev ...
- Windows下libevent C++封装类实现(为什么要使用封装好的网络库?)
题记 windows平台下对于服务器高并发的网络模型选型中,使用libevent是个不错的选择. 本文的背景基于:国内博客对于libevent大多介绍linux实现,大多是c语言的实现,Windows ...
- 《Linux 多线程服务端编程:使用 muduo C++ 网络库》电子版上市
<Linux 多线程服务端编程:使用 muduo C++ 网络库> 电子版已在京东和亚马逊上市销售. 京东购买地址:http://e.jd.com/30149978.html 亚马逊Kin ...
随机推荐
- mysql清空带外键的表
set FOREIGN_KEY_CHECKS =0;TRUNCATE memo;TRUNCATE customer;set FOREIGN_KEY_CHECKS =1;
- GitHub密钥生成
前提电脑上需装有Git软件 这里提供百度云下载地址:https://pan.baidu.com/s/1r0y4XRyQCz7ZJBnZJhAtqw 提取码:88qf 1.登录GitHub账号 2.点 ...
- post 登录禅道,不成功,无解中
]}s.get("http://localhost/zentaopms116/www/misc-checkUpdate-16dd7448451f46bb496a2099b6a9af8c.ht ...
- Centos 7+KVM(Windows Server 2008 r2 )
KVM虚拟机 Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中.它使用Linux自身的调度 ...
- input 控件常用属性
- 2019.6.13_SQL语句中----删除表数据drop、truncate和delete的用法
一.SQL中的语法 1.drop table 表名称 eg: drop table dbo.Sys_Test 2.truncate table 表 ...
- SCOI 2005 互不侵犯
洛谷 P1896 [SCOI2005]互不侵犯 洛谷传送门 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一 ...
- java虚拟机规范学习笔记之数据类型
1.1 class文件格式 编译后被Java虚拟机所执行的代码使用了一种平台中立的二进制格式来表示,并且经常以文件的形式来存储,这种格式称为class文件格式.class文件格式中精确的定义了类与接口 ...
- FineUIPro v6.0.0 大版本更新,快来围观!
本月末(2019-09-20),我们会发布激动人心的 FineUI v6.0.0 版本,这个版本将带来一系列的重要更新! 在列举新版本特性之前,我们先来回顾下每次发布大版本的关键时间点: v1.0.0 ...
- vue怎么给自定义组件绑定原生事件
下面主要以4个示例Demo演示(示例代码JS引用的Vue CDN),建议小伙伴直接复制示例代码运行查看, 赶时间的小伙伴可直接往下拉,看示例demo4 注:全局或局部注册的组件称为子组件,其中声明的 ...