boost asio acceptor.accept invalid argument

大兔子大兔子 提交于 2020-01-16 18:01:32

问题


i am trying to code with boost asio in socket programming.

after setting boost in my ubuntu eclipse environment, i tried the sample code in boost web site.

but an error occur on acceptor.accept() function with invalid argument, like below

how can i fix this error ?

Invalid arguments '
Candidates are:
void accept(boost::asio::basic_socket<#10000,#10001> &, std::enable_if<&0[std::is_convertible<boost::asio::ip::tcp,#10000>::value],void>::type *)
boost::system::error_code accept(boost::asio::basic_socket<#10000,#10001> &, boost::system::error_code &, std::enable_if<&0[std::is_convertible<boost::asio::ip::tcp,#10000>::value],void>::type *)
void accept(boost::asio::basic_socket<boost::asio::ip::tcp,#10000> &, boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> &)
boost::system::error_code accept(boost::asio::basic_socket<boost::asio::ip::tcp,#10000> &, boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> &, boost::system::error_code &)
'   boostTest.cpp   /boostTest  line 41 Semantic Error

and this is the code i am trying

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

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

int main()
{
  try
  {
    boost::asio::io_service io_service;

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));

    for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);

      std::string message = make_daytime_string();

      boost::system::error_code ignored_error;
      boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

回答1:


i am one who questioned about invalid argument error.

it seems that i have found a remedy, i still don't understand why though.

when i fixed argument of accept function, the error disappear.

the error seemed to tell that the function needs additional argument std::enable_if or boost::asio::ip::basic_endpoint

so i just add boost::asio::ip::tcp::endpoint like below

after that, it worked

tcp::endpoint endpoint  = tcp::endpoint(tcp::v4(), port);
a.accept(sock, endpoint);


来源:https://stackoverflow.com/questions/23802920/boost-asio-acceptor-accept-invalid-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!