error: no matching function for call to ‘boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >::basic_stream_socket()’
Hi Why doesn't this work: _socket = tcp::socket(ioService); _socket is a private member of type tcp::socket. How can I store the socket as a class member (so I can access it when using read/write from other class methods)? Also, is there any way to define a timeout on a synchronous connect()? If not, what's the workaround? Can I do an async connect and then somehow return to synchronous mode of operation? Thanks, David
Why doesn't this work:
_socket = tcp::socket(ioService);
1) Socket has no default constructor. You have to pass ioService to its constructor. 2) socket it not copiable/assignable.
Also, is there any way to define a timeout on a synchronous connect()?
No.
If not, what's the workaround?
You can write your own sync. connect with timeout, using async_connect, like this: http://lists.boost.org/Archives/boost/2007/04/120339.php
Can I do an async connect and then somehow return to synchronous mode of operation?
Sure, you can mix sync and async approaches.
Why doesn't this work:
_socket = tcp::socket(ioService);
1) Socket has no default constructor. You have to pass ioService to its constructor. 2) socket it not copiable/assignable.
I tied _socket(ioService), still no luck....
Also, is there any way to define a timeout on a synchronous connect()?
No.
If not, what's the workaround?
You can write your own sync. connect with timeout, using async_connect, like this: http://lists.boost.org/Archives/boost/2007/04/120339.php
Can I do an async connect and then somehow return to synchronous mode of operation?
Sure, you can mix sync and async approaches.
Thanks - will try this out.
On Thu, Feb 18, 2010 at 8:49 AM, David Kaplan
I tied _socket(ioService), still no luck....
This is presumably a member variable, so you must do it in the constructor initializer list. I have written many classes that have an asio::socket member variable, so I know it works. Jon
On Thu, Feb 18, 2010 at 8:59 AM, Jonathan Franklin
On Thu, Feb 18, 2010 at 8:49 AM, David Kaplan
wrote: I tied _socket(ioService), still no luck....
This is presumably a member variable, so you must do it in the constructor initializer list. I have written many classes that have an asio::socket member variable, so I know it works.
Note that if you need to delay the creation of the socket, you might consider using a smart pointer to a socket. Jon
On 18 February 2010 18:00, Jonathan Franklin
Note that if you need to delay the creation of the socket, you might consider using a smart pointer to a socket.
This should solve the issue - thanks! Will try it out...
Note that if you need to delay the creation of the socket, you might consider using a smart pointer to a socket.
This should solve the issue - thanks! Will try it out...
Besides, you can define io_service as a member of you class, and pass it to asio objects: class my_class { my_class() : sock_(io_) {} private: // io_ must be declared *before* any other asio objects, which are going to be initialized with it! io_service io_; tcp::socket sock_; }; Note, however, that both io_ and sock_ are non-copyable.
On Fri, Feb 19, 2010 at 2:07 AM, David Kaplan
On 18 February 2010 18:00, Jonathan Franklin
wrote: Note that if you need to delay the creation of the socket, you might consider using a smart pointer to a socket.
This should solve the issue - thanks! Will try it out...
I wasn't recommending that you use this idiom to solve your io service initialization problem. For that, you should use the idiom that Igor suggested. But I frequently find myself not knowing how many sockets will be needed a priori, or otherwise needing to delay allocation. In which case, some form of smart pointer works great. Jon
I tied _socket(ioService), still no luck....
You have to initialize _socket this way in your object constructor initialization list: class YourClass { YourClass(io_service &io) : _socket(io) {} tcp::socket _socket; };
But that means that I need to pass io_service to the class and I can't
initialize my io service in the class itself??
On 18 February 2010 18:02, Igor R
I tied _socket(ioService), still no luck....
You have to initialize _socket this way in your object constructor initialization list:
class YourClass { YourClass(io_service &io) : _socket(io) {}
tcp::socket _socket; }; _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
David Kaplan
-
Igor R
-
Jonathan Franklin