Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening multiple ports using Boost Asio libraries

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?

like image 945
SKR Avatar asked Sep 17 '25 09:09

SKR


1 Answers

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();
like image 139
spencercw Avatar answered Sep 20 '25 00:09

spencercw