当前位置:网站首页>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))
边栏推荐
- Biscuit distribution
- Automatic correlation between QT signal and slot
- Judging the number of leap years from 1 to N years
- Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
- 弹性布局(display:flex;)属性详解
- 分饼干问题
- ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析
- NBD Network Block Device
- Learning notes on February 8, 2022 (C language)
- Remove interval (greedy)
猜你喜欢
![[try to hack] vulhub shooting range construction](/img/fc/6057b6dec9b51894140453e5422176.png)
[try to hack] vulhub shooting range construction

2022年广东高考分数线出炉,一个几家欢喜几家愁

JS to add elements to the header, or tail of an array

有哪个瞬间让你觉得这个世界出bug了?

Judging the number of leap years from 1 to N years

90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟

JGG | overview of duhuilong group of Hebei University Research on plant pan genomics

【Try to Hack】vulnhub DC1
![[Ocean University of China] Data Sharing for retest of initial Examination](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Ocean University of China] Data Sharing for retest of initial Examination

Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
随机推荐
Why should the coroutine be set to non blocking IO
3. Sequential structure multiple choice questions
dev/mapper的解释
QT pop up open file dialog box QFileDialog
网上办理股票开户安全吗?
移除区间(贪心)
Async await to achieve sleep waiting effect
Heavyweight! The domestic IDE is released and developed by Alibaba. It is completely open source! (high performance + high customization)
Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
有哪个瞬间让你觉得这个世界出bug了?
15 -- 最接近原点的 K 个点
[deep learning] multi task learning of multiple datasets data sets missing labels
Usage of pure virtual functions
One question per day, punch in
How to combine multiple motion graphs into a GIF? Generate GIF animation pictures in three steps
【深度学习】多标签学习
【深度学习】多任务学习 多个数据集 数据集漏标
[Ocean University of China] Data Sharing for retest of initial Examination
QT database connection
Automatic correlation between QT signal and slot