打开使用Boost库短耳多个端口 [英] Opening multiple ports using Boost Asio libraries

查看:171
本文介绍了打开使用Boost库短耳多个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为短耳升压库的新手,我的要求是要建立一个服务器,它应该在600不同的端口异步(TCP通讯)听。有人建议我一个聪明的方式实现这一目标使用Boost短耳。我一直在使用升压短耳文档中提供的回声服务器的例子尝试,但未能真正了解多少
      提高:: ASIO :: io_service对象io_service对象;

I am a newbie for Boost Asio libraries, my requirement is to build a server, which should listen on 600 different ports asynchronously (TCP communication). Can someone suggest me a smart way to achieve this using Boost Asio. I have tried using echo server example provided at Boost Asio documentation but could not really understand much boost::asio::io_service io_service;

using namespace std; // For atoi.
for(long port=50000;port<=50600;port++)
{
    server s(io_service, port);
    io_service.run();
}

有人可以扔光这个?

Can someone throw light on this?

推荐答案

io_service对象负责处理分配给它的所有I / O;不需要为每个端口中单独的一个。对于你正在尝试做的,你需要创建单独的600服务器实例,然后调用 io_service.run()

The io_service is responsible for handling all I/O that is assigned to it; you don't need to create a separate one for each port. For what you are trying to do you will need to create 600 separate server instances then call io_service.run().

vector<shared_ptr<server> > servers;
for (uint16_t port = 50000; port != 50600; ++port)
{
    servers.push_back(shared_ptr<server>(new server(io_service, port)));
}
io_service.run();

这篇关于打开使用Boost库短耳多个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆