Is there a way that allows using an already connected socket in ASIO?
Trying to send one request; ip::tcp::socket::assign
gives randomly 2 different segfaults at 2 different places. One is before calling the callback (on op_queue_acess::next
) and other is after the callback (on boost::asio::detail::task_io_service_operation::complete
(func_ is equal to 0 and tries to be executed)). So I guess it does not work with connected sockets.
Edit:
The case is, I have a connected native descriptor (actually a socket underlying under another library) and I want to assign it to a new, empty ip::tcp::socket so that I can be notified when the socket is read
ready (using the socket::async_read_some
with null buffers) and use the library in a non-blocking way.
Sample code follows as:
class C
{
ip::tcp::socket socket_;
const char connection_info_[] = "...";
TPLibrary tp_;
void start()
{
.
.
tp_.connect(connection_info);
socket_.assign(ip::tcp::v4(), tp_.nativeSocket());
}
}
Then using async_read on this socket gives the said seg fault, sometimes its after calling callback, sometimes before.
EDIT: Now I have removed everything and only the tcp::socket and assigning to the socket left but still boost gives segfaults. Is it because io_service is on a dynamically loaded library and socket is in another dynamically loaded library with a reference to io_service on the main library?
You can assign already connected socket to boost::asio socket and enjoy boost features then onwards.
from below link you can find how to use assign API.
Let me know if you have any query.
Edit:
Now if your problem is about non-blocking read as async_read_some is non blocking then use the read_some as follows
boost::asio::async_read(*p_socket, boost::asio::buffer(&buffer[index], remaining_len2read), read_handler);
The above API is blocking and will not return until all the data is received.
The problem was about dynamic linkage that I was using. As an answer to the question tcp::socket::assign
works fine on connected sockets.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.