3

[Update:] The answer for whoever is interested, is simply wrapping the io_service member var in boost::ref (boost::ref(io_service_))

I am experimenting with the udp server example from boost asio libraries, to see if I can initialize the socket somewhere other than in the constructor. The constructor proposed in the example is the following:

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      socket_(io_service, udp::endpoint(udp::v4(), port))
  {
    socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&server::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

I made the socket member a pointer, and also a shared pointer so that I don't have to be worried about whether it gets out of scope. "io_service_" is also a member variable and I use it to create the socket. My full code is the following:

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

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

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service)
      //socket_(io_service, udp::endpoint(udp::v4(), port))
  {
      initialize_socket(port);
  }

  void initialize_socket(short port)
  {

      socket_p = boost::make_shared<udp::socket>(io_service_, udp::endpoint(udp::v4(), port));
      // (...)
      socket_p->async_receive_from(
              boost::asio::buffer(data_, max_length), sender_endpoint_,
              boost::bind(&server::handle_receive_from, this,
                      boost::asio::placeholders::error,
                      boost::asio::placeholders::bytes_transferred));
  }

  void handle_receive_from(const boost::system::error_code& error,
      size_t bytes_recvd)
  {
    if (!error && bytes_recvd > 0)
    {
      std::cout << "\nReceived: ";
      std::cout.write(data_, bytes_recvd);
      std::cout << "\nSending same string back" << std::endl;
      socket_p->async_send_to(
          boost::asio::buffer(data_, bytes_recvd), sender_endpoint_,
          boost::bind(&server::handle_send_to, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
    else
    {
      socket_p->async_receive_from(
          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
  }

  void handle_send_to(const boost::system::error_code& /*error*/,
      size_t /*bytes_sent*/)
  {
    socket_p->async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&server::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

private:
  boost::asio::io_service& io_service_;
  boost::shared_ptr<udp::socket> socket_p;
  udp::endpoint sender_endpoint_;
  enum { max_length = 1024 };
  char data_[max_length];
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: async_udp_echo_server <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server s(io_service, atoi(argv[1]));

    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

When trying to compile, I get a rather long error message from where I think the important parts are:

required from here
    /usr/local/include/boost/smart_ptr/make_shared_object.hpp:747:5: error: no matching function for call to ‘boost::asio::basic_datagram_socket<boost::asio
    ::ip::udp>::basic_datagram_socket(const boost::asio::io_service&, const boost::asio::ip::basic_endpoint<boost::asio::ip::udp>&)’
         ::new( pv ) T( a1, a2 );

(...)

   basic_datagram_socket(boost::asio::io_service& io_service,
   ^
/usr/local/include/boost/asio/basic_datagram_socket.hpp:108:3: note:   no known conversion for argument 1 from ‘const boost::asio::io_service’ to ‘boost
::asio::io_service&’

Anybody knows what was wrong with this implementation?

6
  • make_shared<> copies its arguments (like bind or thread c'tor). You have to wrap the io_service_ in boost::ref: make_shared<udp::socket>(boost::ref(io_service_), .... Jul 13, 2015 at 10:25
  • Awesome!That did it. how can I mark this one as the answer? :)
    – BHZ
    Jul 14, 2015 at 3:41
  • I made it an answer. Jul 14, 2015 at 6:45
  • @StefanNäwe, I am trying to do something similar. However, I do not have an boost::asio::io_service copy like io_service_, but use the io_service directly passed from the outside. My code does not work. Is that the reason why?
    – CaTx
    Mar 13, 2018 at 7:03
  • I switched to using the copy (io_service_) as the OP, but it still wouldn't work. At my wit's end now.
    – CaTx
    Mar 13, 2018 at 7:07

2 Answers 2

4

make_shared<> copies its arguments (like bind or thread's c'tor). You have to wrap the io_service_ in boost::ref when passed to make_shared to ensure it gets passed as a reference:

socket_p = boost::make_shared(boost::ref(io_service_), udp::endpoint(udp::v4(), port));

0

This code compiles fine on Code::Blocks IDE on windows (though I changed boost::shared_ptr to c++11 std::shared_ptr).

Probably the problem lies either within your installation of boost libraries or compiler / linker settings.

EDIT:

Compiler used is mingw_64. Boost version is 1_58_0.

2
  • Code::Blocks is just an IDE that can use one of many compilers. Which compiler did you use?
    – xlem
    Jul 12, 2015 at 19:28
  • @dcook Thanks for notice, appended my answer.
    – Nick
    Jul 12, 2015 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.