简体   繁体   中英

Boost.asio, async_read and socket kind

I have a client/server application that is trying to read. When reading from the client, it is done asynchronously. It seems that async_read on the client side is failing.

I think that my client is trying to async_read before the server does the corresponding async_write (not completely sure). So my questions here are conceptual:

  1. can I async_read before the corresponding async_write to the socket on the server side has been done?
  2. I am not sure (cannot try right now), but seems that the kind of socket (blocking/non-blocking) changes the behavior of 1.
  3. In case I want to use non-blocking sockets, how can I async_read when I know there is data available? Is the bytes_readable or similar what I am looking for?

Thank you very much. I would appreciate confirmation on my 3 questions, since I have been trying many things for a while and I have little time to try further experiments.

  1. can I async_read before the corresponding async_write to the socket on the server side has been done?

Yes.

  1. I am not sure (cannot try right now), but seems that the kind of socket (blocking/non-blocking) changes the behavior of 1.

Quite possibly, but you shouldn't put the socket into non-blocking mode. Asynchronous I/O is a third I/O model, nothing to do with non-blocking mode.

  1. In case I want to use non-blocking sockets, how can I async_read when I know there is data available?

This is a contradiction in terms. Asynchronous I/O starts the operation and provides a rendezvous when it finishes. The operation itself remains blocking, but not to the async caller. If you want to use non-blocking sockets you should not be also attempting to use asynchronous I/O.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM