I'm trying to write a server using boost/asio, I have "class Server", when I try to pass in the parameters of this class io_service, An error occurs that this io_service not available in boost/asio.
Server.h
#pragma once
#define _WIN32_WINNT
#include <iostream>
#include <string>
#include <boost/asio.hpp>
class Server
{
std::string server_address;
std::string server_port;
boost::asio::io_service server_io_service;
boost::asio::ip::tcp::endpoint server_ep;
boost::asio::ip::tcp::acceptor server_acceptor;
boost::asio::ip::tcp::socket server_socket;
public:
explicit Server(std::string address, std::string port, boost::asio::io_service& io_service);
explicit Server();
~Server();
void start_listening() noexcept;
};
Server.cpp
#include "../headers/server.h"
Server::Server(std::string address, std::string port, boost::asio::io_service& io_service) :
server_address(address),
server_port(port),
server_io_service(io_service), // error
server_ep(boost::asio::ip::tcp::v4(), std::stoi(port)),
server_acceptor(io_service, server_ep),
server_socket(io_service)
{}
output [1/2] Building CXX object CMakeFiles\Server.dir\source\Server.cpp.obj FAILED: CMakeFiles/Server.dir/source/Server.cpp.obj C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1438~1.331\bin\Hostx64\x64\cl.exe /nologo /TP -ID:\boost_1_84_0\boost_1_84_0 /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Ob0 /Od /RTC1 -std:c++20 -ZI /showIncludes /FoCMakeFiles\Server.dir\source\Server.cpp.obj /FdCMakeFiles\Server.dir\ /FS -c D:\C++\Server\source\Server.cpp D:\boost_1_84_0\boost_1_84_0\boost\asio\detail\config.hpp(667): fatal error C1017: недопустимое константное выражение целого типа ninja: build stopped: subcommand failed.
(https://i.stack.imgur.com/rYYeN.png)
I tried to find information about this problem in the documentation "Boost/asio", but I couldn't find anything about it.
io_serviceis not copyable. It's also deprecated.io_contextreplaces it. It is also not copyable.So, either store a reference to it:
Or (better yet) store an executor to it
Now it's weird to accept the
server_addresswhich is unused. Just acceptuint16_t portinstead, or the fullendpoint.Full Demo
Live On Coliru