当前位置:网站首页>10. Libevent receives and processes server messages
10. Libevent receives and processes server messages
2022-04-22 04:38:00 【doru_ cpp】
test.cpp
#include <iostream>
#include <thread>
#include <event2/event.h>
#include <event2/event.h>
#include <event2/thread.h>
#include<event2/listener.h>
#include <errno.h>
#include <string.h>
#ifndef _WIN32
#include <signal.h>
#else
#endif
using namespace std;
#define SPORT 8989
// Normal disconnection and timeout will enter
void client_cb(evutil_socket_t s, short w, void* arg)
{
event* ev = (event*)arg;
// Timeout event judgment
if (w & EV_TIMEOUT)
{
cout << "time out" << endl;
event_free(ev);
evutil_closesocket(s);
return;
}
char buf[1024] = { 0 };
int len = recv(s, buf, sizeof(buf) - 1, 0);
if (len > 0)
{
cout << buf << endl;
send(s, "ok", 2, 0);
}
else
{
// It needs to be cleaned up event
cout << "." << flush;
event_free(ev);
evutil_closesocket(s);
}
}
void listen_cb(evutil_socket_t s, short w, void* arg)
{
cout << "listen_cb" << endl;
sockaddr_in sin;
socklen_t size = sizeof(sin);
// Read connection information
evutil_socket_t client = accept(s, (sockaddr*)&sin, &size);
char ip[16] = { 0 };
evutil_inet_ntop(AF_INET, &sin.sin_addr,ip,sizeof(ip)-1);
cout << "client ip :" << ip << endl;
event_base* base = (event_base*)arg;
// Client data read event
event* ev = event_new(base, client, EV_READ | EV_PERSIST, client_cb, event_self_cbarg());
timeval t = { 10,0 };
event_add(ev, &t);
}
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();
//event The server
cout << "event server ->" << endl;
// establish socket
evutil_socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock <= 0)
{
cout << "socket error: " << strerror(errno) << endl;
return -1;
}
// Set address multiplexing and non blocking
evutil_make_socket_nonblocking(sock);
evutil_make_listen_socket_reuseable(sock);
// Binding port and address
sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SPORT);
int re = ::bind(sock, (sockaddr*)&sin, sizeof(sin));
if (re != 0)
{
cerr << "bind error: " << strerror(errno) << endl;
return -1;
}
// Start listening
listen(sock, 10);
// Accept connection Events
event* ev = event_new(base, sock, EV_READ | EV_PERSIST, listen_cb, base);
event_add(ev, 0);
// Enter the event main loop
event_base_dispatch(base);
evutil_closesocket(sock);
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/202204220432307688.html
边栏推荐
猜你喜欢

Jupiter notebook modifying the default opening path

Peer interview sharing Lenovo WinForm direction 20220420

创意几何点线面三角形孟菲斯海报banneer广告设计ai矢量背景素材

树莓派4B编译paddlelite(详细步骤2022年)

安装opencv时遇到的报错

ObjectMapper,别再像个二货一样一直new了

(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

线程池状态+ThreadPoolExecutor

毕设-SSM校园二手书籍销售系统+论文

How do I test the shuttle application? Unit test
随机推荐
Unity 简单UI前缀树红点系统
12.libevent循环函数和退出测试
11.libevent对水平触发和边缘触发测试
【S32K3_STM&PIT_MCAL】
5_ Data analysis - Data Visualization
MUI-弹出菜单
goland汉化解决方法(下载插件失败的情况下)
C language string
10.libevent接收和处理服务器消息
Mapbox creates multiple draggable marker points
Spark installation and use of educoder
15.bufferevent客户端测试连接服务器
将矩阵转换为稀疏矩阵,再将稀疏矩阵转换为矩阵(第一篇)
Bi set - SSM campus second-hand book sales system + thesis
树莓派4B编译paddlelite(详细步骤2022年)
What are the main aspects of mobile app testing?
WinPcap get device list
POJ - 2456 Aggressive cows (二分 + 贪心)
Binary search for qualified values and local minimum values
Solution to Chinese translation of GoLand (in case of failure to download plug-ins)