Home | Libraries | People | FAQ | More |
Boost.Asio provides basic support UNIX domain sockets (also known as local sockets). The simplest use involves creating a pair of connected sockets. The following code:
local::stream_protocol::socket socket1(my_io_service); local::stream_protocol::socket socket2(my_io_service); local::connect_pair(socket1, socket2);
will create a pair of stream-oriented sockets. To do the same for datagram-oriented sockets, use:
local::datagram_protocol::socket socket1(my_io_service); local::datagram_protocol::socket socket2(my_io_service); local::connect_pair(socket1, socket2);
A UNIX domain socket server may be created by binding an acceptor to an endpoint, in much the same way as one does for a TCP server:
::unlink("/tmp/foobar"); // Remove previous binding. local::stream_protocol::endpoint ep("/tmp/foobar"); local::stream_protocol::acceptor acceptor(my_io_service, ep); local::stream_protocol::socket socket(my_io_service); acceptor.accept(socket);
A client that connects to this server might look like:
local::stream_protocol::endpoint ep("/tmp/foobar"); local::stream_protocol::socket socket(my_io_service); socket.connect(ep);
Transmission of file descriptors or credentials across UNIX domain sockets is not directly supported within Boost.Asio, but may be achieved by accessing the socket's underlying descriptor using the native() member function.
local::connect_pair, local::datagram_protocol, local::datagram_protocol::endpoint, local::datagram_protocol::socket, local::stream_protocol, local::stream_protocol::acceptor, local::stream_protocol::endpoint, local::stream_protocol::iostream, local::stream_protocol::socket, UNIX domain sockets examples.
UNIX domain sockets are only available at compile time if supported by
the target operating system. A program may test for the macro BOOST_ASIO_HAS_LOCAL_SOCKETS
to determine
whether they are supported.