Discussion:
Boost.Asio, An invalid argument was supplied.
stefys
2008-11-23 10:46:11 UTC
Permalink
Hello. I am trying to write a simple server, and I am using an
asynchronous acceptor to get clients:
acceptor.async_accept(crtclient->getSocket(),
boost::bind(&Server::gotClient, this, boost::asio::placeholders::error));
As soon as this is executed, my Server::gotClient gets called with
ec.message()="An invalid argument was supplied."
Apparently the problem is that I am supplying an invalid socket with
crtClient->getSocket(), but I can't understand how can this be.
crtClient is a boost::shared_ptr<Client>.
class Client {
...
public:
Client (boost::asio::io_service &ioserv, Server &server) :
socket_(ioserv), ... {}
...
boost::asio::ip::tcp::socket &getSocket () { return socket_; }
...
private: boost::asio::ip::tcp::socket socket_;
...
};

What could be wrong? Am I constructing socket incorrectly with
socket_(ioserv)? Because if I construct it with socket_(ioserv,
boost::asio::ip::tcp::v4()), I get the error "Already open" in
ec.message(), rather than "An invalid argument was supplied".
Igor R
2008-11-23 19:49:24 UTC
Permalink
Hi,

The lines of code you posted seem to be ok, so the problem is somewhere in
the code you haven't posted here. Ensure that your io_service is initialized
*before* you pass it to the other asio objects, and that the socket is
initialized before you pass it to the acceptor.
Here is an example of how you should use the acceptor:
http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/reference/basic_socket_acceptor/async_accept/overload1.html
Post by stefys
Hello. I am trying to write a simple server, and I am using an
acceptor.async_accept(crtclient->getSocket(),
boost::bind(&Server::gotClient, this, boost::asio::placeholders::error));
As soon as this is executed, my Server::gotClient gets called with
ec.message()="An invalid argument was supplied."
<...>
What could be wrong? Am I constructing socket incorrectly with
socket_(ioserv)? Because if I construct it with socket_(ioserv,
boost::asio::ip::tcp::v4()), I get the error "Already open" in
ec.message(), rather than "An invalid argument was supplied".
stefys
2008-11-24 14:12:01 UTC
Permalink
You were right, my apologies. Apparently, the problem was lying in
preparing the acceptor to wait for clients. I was doing .bind() on it,
but I was forgetting to do .listen().

Best regards,
Steven
Igor R
2008-11-24 14:54:26 UTC
Permalink
Ok, so your acceptor was "unopened". Note that you can use the constructor
that creates & opens the acceptor (i.e. puts it into listening mode):
http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/reference/basic_socket_acceptor/basic_socket_acceptor/overload3.html
Post by stefys
You were right, my apologies. Apparently, the problem was lying in
preparing the acceptor to wait for clients. I was doing .bind() on it, but I
was forgetting to do .listen().
Best regards,
Steven
Loading...