I'm new to network programming in general, so please bear with me. I couldn't find anything about this in the bo开发者_JS百科ost documentation. When using asio::async_read, an end of file error is raised when there is no data to be read from the socket (at least, I think it does). Is there any way to implement some kind of check to prevent this behavior? What I'm trying to get at is: is there any way to wait for data to be present, and then read said data and call the handler? (If this is poorly worded, please say so. I will try to clarify.)
When using asio::async_read, an end of file error is raised when there is no data to be read from the socket
eof
is returned when the other side of the socket is closed, not when there is no data to read.
What I'm trying to get at is: is there any way to wait for data to be present, and then read said data and call the handler?
This is called polling, it's what the asio io_service
does for you. You tell the io_service
that you want to read data from the socket by initiating an async_read
. It performs the read on your behalf and invokes the completion handler when it is finished successfully or unsuccessfully.
It might be worth your time to study the differences between asynchronous and synchronous methods offered by Boost.Asio.
精彩评论