问题
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