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 ...
随机推荐
- Nginx 配置实例-动静分离
1.什么是动静分离 通过 location 指定不同的后缀名实现不同的请求转发.通过 expires 参数设置,可以使浏 览器缓存过期时间,减少与服务器之前的请求和流量.具体 Expires 定义: ...
- php实现图片下载
download.php下载代码 <?php //获取要下载的文件名 $filename = $_GET['filename']; //设置头信息 header('Content-Disposi ...
- 21.Java基础_String类
String类构造方法 package pack1; //推荐使用直接赋值的方式得到字符串 public class test { public static void main(String[] a ...
- CountDownLatch/CyclicBarrier/Semaphore 使用过吗?
CountDownLatch/CyclicBarrier/Semaphore 使用过吗?下面详细介绍用法: 一,(等待多线程完成的)CountDownLatch 背景; countDownLatch ...
- Vue props中Object和Array设置默认值
Vue中,在props中设置Object和Array的默认值 seller: { type: Object, default() { return {} } } seller: { type: Obj ...
- python多版本共存pip指向问题
这两天一致被一个问题困扰,电脑里装了anaconda和python3.7,在命令行里输入python,想要python3.7,出现的确实python3.6,或使用pip安装包时,不知道是装在里pyth ...
- django框架创建app及使用、
App 创建一个app : python manage.py startapp app01 admin: from django.contrib import admin # Register you ...
- vs code 中配置git go
{ "window.zoomLevel": 1, "editor.fontSize": 15, //"files.autoSave": &q ...
- [转]numpy的getA()/getA1()/getH()/getI()函数
转自https://blog.csdn.net/weixin_42906066/article/details/82625779 1.mat.getA() 将自身矩阵变量转化为ndarray类型的变量 ...
- python实现语音信号处理常用度量方法
信噪比(SNR) 有用信号功率与噪声功率的比(此处功率为平均功率),也等于幅度比的平方 $$SNR(dB)=10\log_{10}\frac{\sum_{n=0}^{N-1}s^2(n)}{\sum_ ...