当前位置:网站首页>15. Bufferevent client test connection server
15. Bufferevent client test connection server
2022-04-22 04:38: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;
// error , Overtime ( The connection is disconnected and the meeting enters )
void event_cb(bufferevent* be,short events, void* arg)
{
cout << "[E]" << flush;
// After the read timeout event , Data stop reading
if (events & BEV_EVENT_TIMEOUT && events & BEV_EVENT_READING)
{
cout << "BEV_EVENT_READING BEV_EVENT_TIMEOUT" << endl;
//bufferevent_enable(be, EV_READ);
bufferevent_free(be);
}
else if (events & BEV_EVENT_ERROR)
{
bufferevent_free(be);
}
else
{
cout << "OTHERS" << endl;
}
}
void write_cb(bufferevent* be, void* arg)
{
cout << "[W]" << flush;
}
void read_cb(bufferevent* be, void* arg)
{
cout << "[R]" << flush;
char data[1024] = { 0 };
// Read input buffer data
int len = bufferevent_read(be, data, sizeof(data) - 1);
cout << "[" << data << "]" << endl;
if (len <= 0) return;
//if (strstr(data, "quit") != NULL)
//{
// cout << "quit";
// // Exit and close socket BEV_OPT_CLOSE_ON_FREE
// bufferevent_free(be);
//}
// send data , Write to output buffer
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;
// establish bufferevent Context BEV_OPT_CLOSE_ON_FREE, clear bufferevent Turn off socket
bufferevent *bev=bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE);
// Add monitoring events
bufferevent_enable(bev, EV_READ|EV_WRITE);
// Set the water level
// Read water level
bufferevent_setwatermark(bev, EV_READ,
5, // Low water level ,0 Is unlimited , The default is 0
10); // High water level ,0 Is unlimited , The default is 0
bufferevent_setwatermark(bev, EV_WRITE,
5, // Low water level ,0 Is unlimited , The default is 0, Buffered data is lower than 5 The write callback is called
0); // Invalid high water level
// The setting of timeout time
timeval t1 = { 3,0 };
bufferevent_set_timeouts(bev, &t1,0);
// Set the callback function
bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
}
void client_event_cb(bufferevent* be, short events, void* arg)
{
cout << "[Client E]" << flush;
// After the read timeout event , Data stop reading
if (events & BEV_EVENT_TIMEOUT && events & BEV_EVENT_READING)
{
cout << "BEV_EVENT_READING BEV_EVENT_TIMEOUT" << endl;
//bufferevent_enable(be, EV_READ);
bufferevent_free(be);
}
else if (events & BEV_EVENT_ERROR)
{
bufferevent_free(be);
}
else
{
cout << "OTHERS" << endl;
}
}
void client_write_cb(bufferevent* be, void* arg)
{
cout << "[Client W]" << flush;
}
void client_read_cb(bufferevent* be, void* arg)
{
cout << "[Client R]" << flush;
}
int main(int argc, char** argv)
{
#ifdef _WIN32
// initialization socket library
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
#else
// Ignore the pipe signal , Send data to the closed socket
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
event_base* base = event_base_new();
// Create a web server
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, // Callback function
base, // The parameters of the callback function arg
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE,
10, //listen back
(sockaddr*)&sin,
sizeof(sin)
);
{
// Call client code
//-1 Create inside socket
bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(6969);
evutil_inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr.S_un.S_addr);
// Set the callback function
bufferevent_setcb(bev, client_read_cb, client_write_cb, client_event_cb, 0);
bufferevent_enable(bev, EV_READ | EV_WRITE);
int re = bufferevent_socket_connect(bev, (sockaddr*)&sin, sizeof(sin));
if (re == 0)
{
cout << "connect" << endl;
}
}
event_base_dispatch(base);
evconnlistener_free(ev);
event_base_free(base);
return 0;
}
linux Next makefile
test:test.cpp
g++ $^ -o $@ -levent
./$@
clean:
rm -rf test
rm -rf *.o
版权声明
本文为[doru_ cpp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220432307493.html
边栏推荐
- Thesis reading (47): dtfd-mil: double tier feature interpretation multiple instance learning for histopathology
- 链表第四篇
- 毕设-SSM校园二手书籍销售系统+论文
- 论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
- 论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
- [concurrent programming 043] how to solve the problems of CAS and ABA?
- 链表(Linked List)第一篇
- 论文阅读 (49):Big Data Security and Privacy Protection (科普文)
- Knowledge points related to compliance type
- Cisco packet tracker experiment set
猜你喜欢

Solve the problem that the neo4j browser displays blank circles or non target attributes after importing nodes

How to use SQLite database file on SD card in Android studio

crypto-js加密算法库【安装教程、缓存加密】

软件测试的测试方法你知道多少?

goland汉化解决方法(下载插件失败的情况下)

Mui pop up menu

6_ Data analysis modeling

(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

Unity simple UI prefix tree red dot system

目标检测--轻量级网络(截至2022-04-21)
随机推荐
jsp hello world中文乱码
Financial retail map - transaction flow warning map
AT32 MCU Audio 24bit例程
[experience] Why does the IP address of HP printer start with 169.254
AT32 MCU F435/437 DFU DEMO
AT32 MCU F435/437 DFU DEMO
22.4.21学习感悟
Using random function to change the random range
15.bufferevent客户端测试连接服务器
想转行学测试,我劝你先了解一下这些内容
An article tells you what analysis as a service (AAAS) is
创意几何点线面三角形孟菲斯海报banneer广告设计ai矢量背景素材
MUI-弹出菜单
Oracle password expiration problem, set never expire
链表第四篇
更换vscode解释器
Provincial competition exercise 2 -- the 8th Fujian college student programming competition & supplementary questions
L1-054 blessing arrived (15 points)
2022年山东省安全员C证考试题及在线模拟考试
Will the expired data of redis be deleted immediately?