Asio includes classes added to permit synchronous and asynchronous read and write operations to be performed on POSIX file descriptors, such as pipes, standard input and output, and various devices.
These classes also provide limited support for regular files. This support
assumes that the underlying read and write operations provided by the operating
system never fail with EAGAIN
or EWOULDBLOCK
.
(This assumption normally holds for buffered file I/O.) Synchronous and
asynchronous read and write operations on file descriptors will succeed
but the I/O will always be performed immediately. Wait operations, and
operations involving asio::null_buffers
, are not portably
supported.
For example, to perform read and write operations on standard input and output, the following objects may be created:
posix::stream_descriptor in(my_io_context, ::dup(STDIN_FILENO)); posix::stream_descriptor out(my_io_context, ::dup(STDOUT_FILENO));
These are then used as synchronous or asynchronous read and write streams. This means the objects can be used with any of the read(), async_read(), write(), async_write(), read_until() or async_read_until() free functions.
posix::stream_descriptor, Chat example (C++03), Chat example (C++11).
POSIX stream descriptors are only available at compile time if supported
by the target operating system. A program may test for the macro ASIO_HAS_POSIX_STREAM_DESCRIPTOR
to determine whether they are supported.