3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>close_socket</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="../../index.html" title="Chapter 1. Boost.Beast">
8 <link rel="up" href="../ref.html" title="This Page Intentionally Left Blank 2/2">
9 <link rel="prev" href="boost__beast__buffers_type.html" title="buffers_type">
10 <link rel="next" href="boost__beast__condition.html" title="condition">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="boost__beast__buffers_type.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost__beast__condition.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h4 class="title">
27 <a name="beast.ref.boost__beast__close_socket"></a><a class="link" href="boost__beast__close_socket.html" title="close_socket">close_socket</a>
28 </h4></div></div></div>
30 <a class="indexterm" name="idm45873511416704"></a>
31 Close a socket or socket-like object.
34 <a name="beast.ref.boost__beast__close_socket.h0"></a>
35 <span class="phrase"><a name="beast.ref.boost__beast__close_socket.synopsis"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.synopsis">Synopsis</a>
38 Defined in header <code class="literal"><<a href="../../../../../../boost/beast/core/stream_traits.hpp" target="_top">boost/beast/core/stream_traits.hpp</a>></code>
40 <pre class="programlisting"><span class="keyword">template</span><span class="special"><</span>
41 <span class="keyword">class</span> <span class="identifier">Socket</span><span class="special">></span>
42 <span class="keyword">void</span>
43 <span class="identifier">close_socket</span><span class="special">(</span>
44 <span class="identifier">Socket</span><span class="special">&</span> <span class="identifier">sock</span><span class="special">);</span>
47 <a name="beast.ref.boost__beast__close_socket.h1"></a>
48 <span class="phrase"><a name="beast.ref.boost__beast__close_socket.description"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.description">Description</a>
51 This function attempts to close an object representing a socket. In this
52 context, a socket is an object for which an unqualified call to the function
53 <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">beast_close_socket</span><span class="special">(</span><span class="identifier">Socket</span><span class="special">&)</span></code> is well-defined. The function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code> is a <span class="emphasis"><em>customization
54 point</em></span>, allowing user-defined types to provide an algorithm for
55 performing the close operation by overloading this function for the type
56 in question. Since the customization point is a function call, the normal
57 rules for finding the correct overload are applied including the rules for
58 argument-dependent lookup ("ADL"). This permits classes derived
59 from a type for which a customization is provided to inherit the customization
60 point. An overload for the networking class template <code class="computeroutput"><span class="identifier">net</span><span class="special">::</span><span class="identifier">basic_socket</span></code>
61 is provided, which implements the close algorithm for all socket-like objects
62 (hence the name of this customization point). When used in conjunction with
63 <a class="link" href="boost__beast__get_lowest_layer.html" title="get_lowest_layer"><code class="computeroutput"><span class="identifier">get_lowest_layer</span></code></a>,
64 a generic algorithm operating on a layered stream can perform a closure of
65 the underlying socket without knowing the exact list of concrete types.
68 <a name="beast.ref.boost__beast__close_socket.h2"></a>
69 <span class="phrase"><a name="beast.ref.boost__beast__close_socket.example_1"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.example_1">Example
73 The following generic function synchronously sends a message on the stream,
74 then closes the socket.
76 <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">WriteStream</span><span class="special">></span>
77 <span class="keyword">void</span> <span class="identifier">hello_and_close</span> <span class="special">(</span><span class="identifier">WriteStream</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">)</span>
78 <span class="special">{</span>
79 <span class="identifier">net</span><span class="special">::</span><span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">net</span><span class="special">::</span><span class="identifier">const_buffer</span><span class="special">(</span><span class="string">"Hello, world!"</span><span class="special">,</span> <span class="number">13</span><span class="special">));</span>
80 <span class="identifier">close_socket</span><span class="special">(</span><span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">stream</span><span class="special">));</span>
81 <span class="special">}</span>
84 To enable closure of user defined types, it is necessary to provide an overload
85 of the function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code>
89 <a name="beast.ref.boost__beast__close_socket.h3"></a>
90 <span class="phrase"><a name="beast.ref.boost__beast__close_socket.example_2"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.example_2">Example
94 The following code declares a user-defined type which contains a private
95 socket, and provides an overload of the customization point which closes
98 <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">my_socket</span>
99 <span class="special">{</span>
100 <span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">sock_</span><span class="special">;</span>
102 <span class="keyword">public</span><span class="special">:</span>
103 <span class="identifier">my_socket</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">io_context</span><span class="special">&</span> <span class="identifier">ioc</span><span class="special">)</span>
104 <span class="special">:</span> <span class="identifier">sock_</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">)</span>
105 <span class="special">{</span>
106 <span class="special">}</span>
108 <span class="keyword">friend</span> <span class="keyword">void</span> <span class="identifier">beast_close_socket</span><span class="special">(</span><span class="identifier">my_socket</span><span class="special">&</span> <span class="identifier">s</span><span class="special">)</span>
109 <span class="special">{</span>
110 <span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
111 <span class="identifier">s</span><span class="special">.</span><span class="identifier">sock_</span><span class="special">.</span><span class="identifier">close</span><span class="special">(</span><span class="identifier">ec</span><span class="special">);</span>
112 <span class="comment">// ignore the error</span>
113 <span class="special">}</span>
114 <span class="special">};</span>
117 <a name="beast.ref.boost__beast__close_socket.h4"></a>
118 <span class="phrase"><a name="beast.ref.boost__beast__close_socket.parameters"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.parameters">Parameters</a>
120 <div class="informaltable"><table class="table">
140 <code class="computeroutput"><span class="identifier">sock</span></code>
145 The socket to close. If the customization point is not defined
146 for the type of this object, or one of its base classes, then a
147 compiler error results.
153 <a name="beast.ref.boost__beast__close_socket.h5"></a>
154 <span class="phrase"><a name="beast.ref.boost__beast__close_socket.see_also"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.see_also">See
158 <a class="link" href="boost__beast__beast_close_socket.html" title="beast_close_socket"><code class="computeroutput"><span class="identifier">beast_close_socket</span></code></a>
161 Convenience header <code class="literal"><<a href="../../../../../../boost/beast/core.hpp" target="_top">boost/beast/core.hpp</a>></code>
164 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
165 <td align="left"></td>
166 <td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
168 Distributed under the Boost Software License, Version 1.0. (See accompanying
169 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>)
174 <div class="spirit-nav">
175 <a accesskey="p" href="boost__beast__buffers_type.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost__beast__condition.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>