当前位置:网站首页>Boost listening port server
Boost listening port server
2022-06-25 14:59:00 【qq_ twenty-three million nine hundred and fifty-three thousand 】
The first way , More comprehensive approach
1, establish acceptor io
boost::asio::io_service io_serv;
boost::asio::ip::tcp::acceotpr acceotpr(io_serv);
acceotpr.open(io_serv, ip::tcp::v4());
2, Set up port multiplexing
int on = 1;
setsockopt(acceotpr->native_handle(), SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
setsockopt(acceotpr->native_handle(), SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on));
// Or call acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
3, Set listening port
{
// If listening to all network card port numbers
boost::asio::ip::tcp::endpoint endpoint(ipc::tcp::v4(), port)
// If you monitor ip+ Port number
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address_v4::from_string(ip), port)
}
4, Binding port
acceotpr.bind(endpoint);
5, monitor
acceotpr.listen();
6, Receive data asynchronously
start_accept()
{
boost::asio::ip::tcp::socket *psock = new boost::asio::ip::tcp::socket(io_serv)
acceotpr.async_accept(sock, boost::bind(handle_accept, this, (void *)psock, _1)); // Bind receive function
}
7, Data processing
handle_accept(void *psock, const boost::system::error_code &error)
{
if(error)
return ;
std::ostringstream local = psock->local_endpoint().address();
std::string local_str = local.str();
std::ostringstream remote = psock->remote_endpoint().address();
std::string remote_str = remote.str();
start_accept();
}
8, Execute callback using
io_serv.run();
The second way , Simple way
boost::asio::io_service io_serv;
boost::asio::ip::tcp::acceotpr acceotpr(io_serv, tcp::endpoint(tcp::v4(), port))
边栏推荐
- New title of PTA
- QT loading third-party library basic operation
- 定位position(5种方式)
- QQ情话糖果情话内容获取并保存
- Learning notes on February 18, 2022 (C language)
- 移除区间(贪心)
- NBD Network Block Device
- About the problem of kicad stuck in win10 version, version 6 x
- Using Sphinx to automatically generate API documents from py source files
- Differences between member variables and local variables
猜你喜欢

Js- get the mouse coordinates and follow them

System Verilog — interface

C language escape character and its meaning

Uniapp icon configuration

【Try to Hack】vulnhub DC1

Source code analysis of zeromq lockless queue

Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)

basic_ String mind map

关于win10 版本kicad 卡死的问题, 版本6.x

Yolov3 spp Darknet version to caffemodel and then to OM model
随机推荐
【中国海洋大学】考研初试复试资料分享
‘make_ unique’ is not a member of ‘std’
C language LNK2019 unresolved external symbols_ Main error
Golang channel reading data
NBD Network Block Device
Go语言Zap库Logger的定制化和封装使用详解
Daily question, magic square simulation
【深度学习】多任务学习 多个数据集 数据集漏标
有哪个瞬间让你觉得这个世界出bug了?
Common classes in QT
QT opens the print dialog box in a text editor
Std:: vector minutes
Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?
User defined data type - structure
QT database connection deletion
Using Sphinx to automatically generate API documents from py source files
【Try to Hack】vulhub靶场搭建
Master XSS completely from 0 to 1
Jaspersoft studio installation
Customization and encapsulation of go language zap library logger