I'm using boost 1.75.0 and I'm trying to update my server so he can listen to two different ports at the same time
let's say IP 127.0.0.1 port 6500 and port 6600.
do I need to hold in the Server
two sockets?
this is my server
#include <boost/asio/io_service.hpp>
#include <boost/asio.hpp>
#include <queue>
#include "Session.h"
using boost::asio::ip::tcp;
class Server
{
public:
Server(boost::asio::io_service &io_service, short port)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
socket_(io_service)
{
do_accept();
}
private:
void do_accept(){
acceptor_.async_accept(socket_,
[this](boost::system::error_code ec) {
if (!ec) {
std::cout << "accept connection\n";
std::make_shared<Session>(std::move(socket_))->start();
}
do_accept();
});
}
tcp::acceptor acceptor_;
tcp::socket socket_;
};
this is my Session class
#include <boost/asio/io_service.hpp>
#include <boost/asio.hpp>
#include "message.h"
#include <fstream>
#include <boost/bind/bind.hpp>
namespace {
using boost::asio::ip::tcp;
auto constexpr log_active=true;
using boost::system::error_code;
using namespace std::chrono_literals;
using namespace boost::posix_time;
};
class Session
: public std::enable_shared_from_this<Session>
{
public:
Session(tcp::socket socket)
: socket_(std::move(socket)) ,
{
}
~Session()= default;
void start();
void do_write_alive();
private:
void do_read_header();
void do_read_body();
void do_write();
using Strand = boost::asio::strand<tcp::socket::executor_type>;
using Timer = boost::asio::steady_timer;
tcp::socket socket_{strand_};
Strand strand_{make_strand(socket_.get_executor())};
Timer recv_deadline_{strand_};
Timer send_deadline_{strand_};
enum { max_length = 1024 };
char data_[max_length];
};
I didn't include the implementation of the Session
class only the constructor because there not relevant.