Создание кода Асинхронизированное UDP -сервер, снятое с документации Boost :: ASIO, не работает из -за std :: bind. ПочеC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Создание кода Асинхронизированное UDP -сервер, снятое с документации Boost :: ASIO, не работает из -за std :: bind. Поче

Сообщение Anonymous »

Я попытался запустить код, создавая асинхронный UDP -сервер. Код взят непосредственно из примера документации Boost :: Asio , который я воспроизводил ниже:
#include
#include
#include
#include
#include
#include
#include

using boost::asio::ip::udp;

std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}

class udp_server
{
public:
udp_server(boost::asio::io_context& io_context)
: socket_(io_context, udp::endpoint(udp::v4(), 13))
{
start_receive();
}

private:
void start_receive()
{
socket_.async_receive_from(
boost::asio::buffer(recv_buffer_), remote_endpoint_,
std::bind(&udp_server::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void handle_receive(const boost::system::error_code& error,
std::size_t /*bytes_transferred*/)
{
if (!error)
{
std::shared_ptr message(
new std::string(make_daytime_string()));

socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
std::bind(&udp_server::handle_send, this, message,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));

start_receive();
}
}

void handle_send(std::shared_ptr /*message*/,
const boost::system::error_code& /*error*/,
std::size_t /*bytes_transferred*/)
{
}

udp::socket socket_;
udp::endpoint remote_endpoint_;
std::array recv_buffer_;
};

int main()
{
try
{
boost::asio::io_context io_context;
udp_server server(io_context);
io_context.run();
}
catch (std::exception& e)
{
std::cerr (Bind> std> std>. Пытался соответственно изменить приведенный выше код документации: < /p>
#include
// ...
socket_.async_receive_from(
boost::asio::buffer(recv_buffer_), remote_endpoint_,
boost::bind(&udp_server::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)
);
//... same with socket_async_send_to

И это работает отлично (ссылка на Godbolt).
Мое предыдущее впечатление было, что std :: bind было снято с Boost :: Bind во время обновления языка к C ++ 11.
, это не так, как в исследовании Cursory по Sackoverflow может показывать. Код UDP-сервер выше, найденный в документации Boost :: ASIO , неудачная из-за отсутствия интеграции std :: bind с помощью Boost :: Asio :: Placeholders , или это из-за некоторой более эзотерической проблемы?/home/C++code/Crash_Course_exercises/chapter_20/exercise_20_1.cpp: In member function ‘void udp_server::start_receive()’:
/home/C++code/Crash_Course_exercises/chapter_20/exercise_20_1.cpp:31:31: error: no matching function for call to ‘boost::asio::basic_datagram_socket::async_receive_from(boost::asio::mutable_buffers_1, boost::asio::ip::udp::endpoint&, std::_Bind_helper::type)’
31 | socket_.async_receive_from(
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^
32 | boost::asio::buffer(recv_buffer_), remote_endpoint_,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 | std::bind(&udp_server::handle_receive, this,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 | boost::asio::placeholders::error,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 | boost::asio::placeholders::bytes_transferred
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36 | )
| ~
37 | );
| ~
In file included from /usr/include/boost/asio.hpp:24,
from /home/C++code/Crash_Course_exercises/chapter_20/exercise_20_1.cpp:7:
/usr/include/boost/asio/basic_datagram_socket.hpp:1012:3: note: candidate: ‘template requires completion_token_for auto boost::asio::basic_datagram_socket::async_receive_from(const MutableBufferSequence&, boost::asio::basic_datagram_socket::endpoint_type&, ReadHandler&&) [with MutableBufferSequence = MutableBufferSequence; ReadHandler = ReadHandler; Protocol = boost::asio::ip::udp; Executor = boost::asio::execution::any_executor]’
1012 | async_receive_from(const MutableBufferSequence& buffers,
| ^~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:1012:3: note: template argument deduction/substitution failed:
/usr/include/boost/asio/basic_datagram_socket.hpp:1012:3: note: constraints not satisfied
In file included from /usr/include/boost/asio/detail/handler_type_requirements.hpp:53,
from /usr/include/boost/asio/impl/execution_context.hpp:18,
from /usr/include/boost/asio/execution_context.hpp:409,
from /usr/include/boost/asio/detail/scheduler.hpp:21,
from /usr/include/boost/asio/system_context.hpp:19,
from /usr/include/boost/asio/impl/system_executor.hpp:22,
from /usr/include/boost/asio/system_executor.hpp:662,
from /usr/include/boost/asio/associated_executor.hpp:22,
from /usr/include/boost/asio.hpp:21,
from /home/C++code/Crash_Course_exercises/chapter_20/exercise_20_1.cpp:7:
/usr/include/boost/asio/async_result.hpp: In substitution of ‘template requires completion_token_for auto boost::asio::basic_datagram_socket::async_receive_from(const MutableBufferSequence&, boost::asio::basic_datagram_socket::endpoint_type&, ReadHandler&&) [with MutableBufferSequence = boost::asio::ip::udp; ReadHandler = boost::asio::execution::any_executor]’:
/home/C++code/Crash_Course_exercises/chapter_20/exercise_20_1.cpp:31:31: required from here
/usr/include/boost/asio/async_result.hpp:493:20: required for the satisfaction of ‘completion_token_for’ [with ReadHandler = std::_Bind<
/usr/include/boost/asio/async_result.hpp:498: confused by earlier errors, bailing out
make[2]: *** [chapter_20/CMakeFiles/exercise_20_1.dir/build.make:79: chapter_20/CMakeFiles/exercise_20_1.dir/exercise_20_1.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:9150: chapter_20/CMakeFiles/exercise_20_1.dir/all] Error 2
make: *** [Makefile:146: all] Error 2


Подробнее здесь: https://stackoverflow.com/questions/795 ... n-not-work
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»