当前位置:网站首页>13.bufferevent接受和发送数据
13.bufferevent接受和发送数据
2022-04-22 04:32:00 【doru_cpp】
test.cpp
#include <iostream>
#include <event2/event.h>
#include <event2/thread.h>
#include<event2/listener.h>
#include <event2/bufferevent.h>
#include <errno.h>
#include <string.h>
#ifndef _WIN32
#include <signal.h>
#else
#endif
using namespace std;
void event_cb(bufferevent* be,short events, void* arg)
{
cout << "[E]" << flush;
}
void write_cb(bufferevent* be, void* arg)
{
cout << "[W]" << flush;
}
void read_cb(bufferevent* be, void* arg)
{
cout << "[R]" << flush;
char data[1024] = { 0 };
//读取输入缓冲数据
int len = bufferevent_read(be, data, sizeof(data) - 1);
cout << "[" << data << "]" << endl;
if (len <= 0) return;
if (strstr(data, "quit") != NULL)
{
cout << "quit";
//退出并关闭socket BEV_OPT_CLOSE_ON_FREE
bufferevent_free(be);
}
//发送数据,写入到输出缓冲
bufferevent_write(be, "ok", 3);
}
void listen_cb(evconnlistener* ev, evutil_socket_t s, sockaddr* sin, int slen, void* arg)
{
cout << "listen_cb" << endl;
event_base* base = (event_base*)arg;
//创建bufferevent上下文 BEV_OPT_CLOSE_ON_FREE,清理bufferevent时关闭socket
bufferevent *bev=bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE);
//添加监控事件
bufferevent_enable(bev, EV_READ|EV_WRITE);
//设置回调函数
bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
}
int main(int argc, char** argv)
{
#ifdef _WIN32
//初始化socket库
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
#else
//忽略管道信号,发送数据给已关闭的socket
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
event_base* base = event_base_new();
//创建网络服务器
sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(6969);
evconnlistener* ev = evconnlistener_new_bind(base,
listen_cb, //回调函数
base, //回调函数的参数arg
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE,
10, //listen back
(sockaddr*)&sin,
sizeof(sin)
);
event_base_dispatch(base);
evconnlistener_free(ev);
event_base_free(base);
return 0;
}
linux下的makefile
test:test.cpp
g++ $^ -o $@ -levent
./$@
clean:
rm -rf test
rm -rf *.o
版权声明
本文为[doru_cpp]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xiaoduan6/article/details/124307653
边栏推荐
- Jupiter notebook modifying the default opening path
- 7_ Data analysis - Evaluation
- AT32 MCU Audio 24bit例程
- AT32 MCU F435/437 DFU DEMO
- Torch file saving and loading -- [torch learning notes]
- Your mind will change your behavior, and your behavior will change your situation
- L3-022 subway one day tour (30 points) [Floyd + DFS]
- Job scheduling, intermediate scheduling, process scheduling
- 03-DataFrame & Column
- How did Jian Daoyun, who started a business in a small team of 10 people and now has an income of more than 100 million without financing, do it?
猜你喜欢

Rsync remote synchronization

01-Read&Write
![[experience] Why does the IP address of HP printer start with 169.254](/img/e7/5068a565b57f066377fcb5754bc4bd.png)
[experience] Why does the IP address of HP printer start with 169.254

Filebeat collects log data and transfers it to redis. Different es indexes are created according to log fields through logstash
![[concurrent programming 043] how to solve the problems of CAS and ABA?](/img/bd/8638aa75d7d3b237d792a201e68c0f.png)
[concurrent programming 043] how to solve the problems of CAS and ABA?

WebSocket学习

How did Jian Daoyun, who started a business in a small team of 10 people and now has an income of more than 100 million without financing, do it?

(sip-1-phone registration) analysis of the whole process of IP phone registration to PBX telephone exchange through SIP Protocol - how to see the message in Wireshark

What are the main aspects of mobile app testing?

Solve the problem that the neo4j browser displays blank circles or non target attributes after importing nodes
随机推荐
5_ Data analysis - Data Visualization
Pgpool II 4.3 Chinese Manual - introductory tutorial
C language learning record -- use and analysis of string function (1)
毕设-SSM校园二手书籍销售系统+论文
01-Read&Write
pipeline communication
[concurrent programming 044] CAS cycle time is too long. What's the problem?
二叉树的层序遍历
05-Aggregation
Training summary report
Peer interview sharing Lenovo WinForm direction 20220420
Interpretation of asterisk source code (I: basic introduction and chan_sip code analysis)
ES6 practical tips
2022G2电站锅炉司炉操作证考试题库及在线模拟考试
Opensca version upgrade | opensca v1.0 Release of version 0.4
Revit (3) - second opening - create column
03-DataFrame & Column
Introduction to Intel edge software center
6_ Data analysis modeling
[taro development] - Global custom navigation bar adaptation message notification box location and other problems (14)