初识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网络库的更多相关文章

  1. libevent网络库

    1.概述 libevent是一个C语言编写的.轻量级开源高性能事件通知库.作为底层网络库,已经被广泛应用(如:memcached.Vomit.Nylon.Netchat等).主要有以下几个亮点: 事件 ...

  2. 以libevent网络库为引:网络通信和多线程

    1. windows下编译及使用libevent  http://www.cnblogs.com/luxiaoxun/p/3603399.html 2.  <<libevent学习资料&g ...

  3. libevent 网络库安装

    ./configure prefix=/tools/libevent make sudo make install

  4. [原]网络库libevent在Visual Studio中的使用方法

    libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名分布式缓存软件memcached也 ...

  5. 轻量级网络库libevent初探

    本文是关于libevent库第一篇博文,主要由例子来说明如何利用该库.后续博文再深入研究该库原理. libevent库简介 就如libevent官网上所写的“libevent - an event n ...

  6. 开源网络库ACE、Boost的ASIO、libevent、libev、ZeroMQ

    开源C/C++网络库:ACE          C++语言 跨平台Boost的ASIO  C++语言 跨平台libevent     C语言   主要支持linux,新版增加了对windows的IOC ...

  7. 网络库libevent、libev、libuv对比

    Libevent.libev.libuv三个网络库,都是c语言实现的异步事件库Asynchronousevent library). 异步事件库本质上是提供异步事件通知(Asynchronous Ev ...

  8. Windows下libevent C++封装类实现(为什么要使用封装好的网络库?)

    题记 windows平台下对于服务器高并发的网络模型选型中,使用libevent是个不错的选择. 本文的背景基于:国内博客对于libevent大多介绍linux实现,大多是c语言的实现,Windows ...

  9. 《Linux 多线程服务端编程:使用 muduo C++ 网络库》电子版上市

    <Linux 多线程服务端编程:使用 muduo C++ 网络库> 电子版已在京东和亚马逊上市销售. 京东购买地址:http://e.jd.com/30149978.html 亚马逊Kin ...

随机推荐

  1. python 连接数据库,查询结果写入数据到excel

    使用Python链接数据库查询数据,并将查询结果写入到Excel中,实现方法上主要有两步,第一,查数据,第二,写Excel. 一.导入需要的包 import time import xlwt from ...

  2. 面向对象程序设计(Java) 第7周学习指导及要求

    2019面向对象程序设计(Java)第7周学习指导及要求 (2019.10.11-2019.10.14) 学习目标 掌握四种访问权限修饰符的使用特点: 掌握Object类的用途及常用API: 掌握Ar ...

  3. ffmpeg下载m3u8流媒体

    安装 编译好的windows可用版本的下载地址(官网中可以连接到这个网站,和官方网站保持同步): http://ffmpeg.zeranoe.com/builds/ 该版本为FFMPEG的Static ...

  4. day69_10_14 drf接口框架。

    一.drf框架简介 drf全程是:django-rest framework. 其中涉及的知识点有. 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码 - 基于restful规范下 ...

  5. 【转】认识JWT

    1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...

  6. [C1W2] Neural Networks and Deep Learning - Basics of Neural Network programming

    第二周:神经网络的编程基础(Basics of Neural Network programming) 二分类(Binary Classification) 这周我们将学习神经网络的基础知识,其中需要 ...

  7. [C8] 聚类(Clustering)

    聚类(Clustering) 非监督学习:简介(Unsupervised Learning: Introduction) 本章节介绍聚类算法,这是我们学习的第一个非监督学习算法--学习无标签数据,而不 ...

  8. MongoDB概念认识(四)

    1. database 一个mongodb中可以建立多个数据库. MongoDB的默认数据库为"db",该数据库存储在data目录中. MongoDB的单个实例可以容纳多个独立的数 ...

  9. 剑指Offer-9.变态跳台阶(C++/Java)

    题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 分析: 假设我们要求跳上第3级的跳法,可以从第0级跳3级台阶到达,也可以从第1级 ...

  10. rss订阅

    其实,本质上和爬虫没区别,只不过这是人家主动给你数据,而且是编排好格式后的数据 按个人主页url更新内容 去重,按照redis去重的方式 按时间保存内容 mysql 保存为时间格式(可以根据时间比较大 ...