views:

84

answers:

2

Hi.

Is it possible to read a single byte via asio::read?

I'm getting a single byte response and it seems wasteful to use the current buffering code:

        //Read the 1 byte reply
        char buffer[1];
        size_t bytesRead = asio::read(s, asio::buffer(buffer, 1));
        if(bytesRead < 1) return false;

Thanks.

A: 

boost::asio is implemented on top of Windows' overlapped I/O TCP Winsock implementation. There's no way around buffers, etc. in such an implementation.

David Gladfelter
Linux is the target platform.
SyBer
+2  A: 

No, passing a buffer of a single byte is the only way.

Also it isn't wasteful. What is it that you're concerned about wasting?

Richard Wolf
Just thought allocating memory buffer takes more resources then defined variable, but I might not be familiar with Asio internals.
SyBer
The memory buffer in your sample above is allocated on the stack. This is exactly the same as if you had defined a single char variable.
Richard Wolf
I actually thought that buffer function creates some internal buffers as well, but now I see it actually just maps the pre-defined stuck buffer so it makes sense.
SyBer