1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>UNIX Domain Sockets</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../posix.html" title="POSIX-Specific Functionality"> 9<link rel="prev" href="../posix.html" title="POSIX-Specific Functionality"> 10<link rel="next" href="stream_descriptor.html" title="Stream-Oriented File Descriptors"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../posix.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../posix.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stream_descriptor.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.overview.posix.local"></a><a class="link" href="local.html" title="UNIX Domain Sockets">UNIX Domain Sockets</a> 28</h4></div></div></div> 29<p> 30 Boost.Asio provides basic support for UNIX domain sockets (also known as 31 local sockets). The simplest use involves creating a pair of connected 32 sockets. The following code: 33 </p> 34<pre class="programlisting">local::stream_protocol::socket socket1(my_io_context); 35local::stream_protocol::socket socket2(my_io_context); 36local::connect_pair(socket1, socket2); 37</pre> 38<p> 39 will create a pair of stream-oriented sockets. To do the same for datagram-oriented 40 sockets, use: 41 </p> 42<pre class="programlisting">local::datagram_protocol::socket socket1(my_io_context); 43local::datagram_protocol::socket socket2(my_io_context); 44local::connect_pair(socket1, socket2); 45</pre> 46<p> 47 A UNIX domain socket server may be created by binding an acceptor to an 48 endpoint, in much the same way as one does for a TCP server: 49 </p> 50<pre class="programlisting">::unlink("/tmp/foobar"); // Remove previous binding. 51local::stream_protocol::endpoint ep("/tmp/foobar"); 52local::stream_protocol::acceptor acceptor(my_io_context, ep); 53local::stream_protocol::socket socket(my_io_context); 54acceptor.accept(socket); 55</pre> 56<p> 57 A client that connects to this server might look like: 58 </p> 59<pre class="programlisting">local::stream_protocol::endpoint ep("/tmp/foobar"); 60local::stream_protocol::socket socket(my_io_context); 61socket.connect(ep); 62</pre> 63<p> 64 Transmission of file descriptors or credentials across UNIX domain sockets 65 is not directly supported within Boost.Asio, but may be achieved by accessing 66 the socket's underlying descriptor using the <a class="link" href="../../reference/basic_socket/native_handle.html" title="basic_socket::native_handle">native_handle()</a> 67 member function. 68 </p> 69<h6> 70<a name="boost_asio.overview.posix.local.h0"></a> 71 <span class="phrase"><a name="boost_asio.overview.posix.local.see_also"></a></span><a class="link" href="local.html#boost_asio.overview.posix.local.see_also">See 72 Also</a> 73 </h6> 74<p> 75 <a class="link" href="../../reference/local__connect_pair.html" title="local::connect_pair">local::connect_pair</a>, 76 <a class="link" href="../../reference/local__datagram_protocol.html" title="local::datagram_protocol">local::datagram_protocol</a>, 77 <a class="link" href="../../reference/local__datagram_protocol/endpoint.html" title="local::datagram_protocol::endpoint">local::datagram_protocol::endpoint</a>, 78 <a class="link" href="../../reference/local__datagram_protocol/socket.html" title="local::datagram_protocol::socket">local::datagram_protocol::socket</a>, 79 <a class="link" href="../../reference/local__stream_protocol.html" title="local::stream_protocol">local::stream_protocol</a>, 80 <a class="link" href="../../reference/local__stream_protocol/acceptor.html" title="local::stream_protocol::acceptor">local::stream_protocol::acceptor</a>, 81 <a class="link" href="../../reference/local__stream_protocol/endpoint.html" title="local::stream_protocol::endpoint">local::stream_protocol::endpoint</a>, 82 <a class="link" href="../../reference/local__stream_protocol/iostream.html" title="local::stream_protocol::iostream">local::stream_protocol::iostream</a>, 83 <a class="link" href="../../reference/local__stream_protocol/socket.html" title="local::stream_protocol::socket">local::stream_protocol::socket</a>, 84 <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.unix_domain_sockets">UNIX 85 domain sockets examples</a>. 86 </p> 87<h6> 88<a name="boost_asio.overview.posix.local.h1"></a> 89 <span class="phrase"><a name="boost_asio.overview.posix.local.notes"></a></span><a class="link" href="local.html#boost_asio.overview.posix.local.notes">Notes</a> 90 </h6> 91<p> 92 UNIX domain sockets are only available at compile time if supported by 93 the target operating system. A program may test for the macro <code class="computeroutput">BOOST_ASIO_HAS_LOCAL_SOCKETS</code> 94 to determine whether they are supported. 95 </p> 96</div> 97<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 98<td align="left"></td> 99<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 100 Kohlhoff<p> 101 Distributed under the Boost Software License, Version 1.0. (See accompanying 102 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 103 </p> 104</div></td> 105</tr></table> 106<hr> 107<div class="spirit-nav"> 108<a accesskey="p" href="../posix.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../posix.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stream_descriptor.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 109</div> 110</body> 111</html> 112