3

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?

Etherealone
  • 3,488
  • 2
  • 37
  • 56
  • Your question isn't clear. You have an existing native descriptor that you want to assign to an already connected `boost::asio::ip::tcp::socket`? – Sam Miller Oct 23 '13 at 23:13
  • There's an [example](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp03/nonblocking/third_party_lib.cpp) showing reactor style operations included in the Asio documentation. There are also [similar](http://stackoverflow.com/questions/4686127/async-wait-on-file-descriptor-using-boost-asio/4686523#4686523) question on SO. Please include your code in your question so we can help. – Sam Miller Oct 23 '13 at 23:56
  • @SamMiller The third party library in that example also uses a ip::tcp::socket. I cannot modify the library to use ip::tcp::socket. The library does all the connecting and such using native methods and gives me an option to set blocking state and a function to get the fd. I don't know what else the library does other than connecting to the server when I call library_connect() method so I have to get the connected socket into asio loop somehow. – Etherealone Oct 24 '13 at 00:59
  • the example is just that, an example. It isn't intended to be all encompassing for every third party library. The null buffers concept was designed to solve your problem. Please edit your question to include code demonstrating your problem. – Sam Miller Oct 24 '13 at 03:19
  • @sammiller I have added an example. – Etherealone Oct 24 '13 at 17:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39936/discussion-between-sam-miller-and-tolga) – Sam Miller Oct 24 '13 at 17:28

2 Answers2

1

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.

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/basic_stream_socket/assign/overload1.html

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.

Chirag Desai
  • 1,249
  • 1
  • 10
  • 22
1

The problem was about dynamic linkage that I was using. As an answer to the question tcp::socket::assign works fine on connected sockets.

Etherealone
  • 3,488
  • 2
  • 37
  • 56