1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 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__beast_close_socket.html" title="beast_close_socket"> 10<link rel="next" href="boost__beast__iequals.html" title="iequals"> 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="boost__beast__beast_close_socket.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__iequals.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="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> 29<p> 30 <a class="indexterm" name="idm46057510880176"></a> 31 </p> 32<p> 33 Close a socket or socket-like object. 34 </p> 35<h5> 36<a name="beast.ref.boost__beast__close_socket.h0"></a> 37 <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 </h5> 39<p> 40 Defined in header <code class="literal"><<a href="../../../../../../boost/beast/core/stream_traits.hpp" target="_top">boost/beast/core/stream_traits.hpp</a>></code> 41 </p> 42<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span> 43 <span class="keyword">class</span> <span class="identifier">Socket</span><span class="special">></span> 44<span class="keyword">void</span> 45<span class="identifier">close_socket</span><span class="special">(</span> 46 <span class="identifier">Socket</span><span class="special">&</span> <span class="identifier">sock</span><span class="special">);</span> 47</pre> 48<h5> 49<a name="beast.ref.boost__beast__close_socket.h1"></a> 50 <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 </h5> 52<p> 53 This function attempts to close an object representing a socket. In this 54 context, a socket is an object for which an unqualified call to the function 55 <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 56 point</em></span>, allowing user-defined types to provide an algorithm for 57 performing the close operation by overloading this function for the type 58 in question. Since the customization point is a function call, the normal 59 rules for finding the correct overload are applied including the rules for 60 argument-dependent lookup ("ADL"). This permits classes derived 61 from a type for which a customization is provided to inherit the customization 62 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> 63 is provided, which implements the close algorithm for all socket-like objects 64 (hence the name of this customization point). When used in conjunction with 65 <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>, 66 a generic algorithm operating on a layered stream can perform a closure of 67 the underlying socket without knowing the exact list of concrete types. 68 </p> 69<h5> 70<a name="beast.ref.boost__beast__close_socket.h2"></a> 71 <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 72 1</a> 73 </h5> 74<p> 75 The following generic function synchronously sends a message on the stream, 76 then closes the socket. 77 </p> 78<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> 79<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> 80<span class="special">{</span> 81 <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> 82 <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> 83<span class="special">}</span> 84</pre> 85<p> 86 To enable closure of user defined types, it is necessary to provide an overload 87 of the function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code> 88 for the type. 89 </p> 90<h5> 91<a name="beast.ref.boost__beast__close_socket.h3"></a> 92 <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 93 2</a> 94 </h5> 95<p> 96 The following code declares a user-defined type which contains a private 97 socket, and provides an overload of the customization point which closes 98 the private socket. 99 </p> 100<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">my_socket</span> 101<span class="special">{</span> 102 <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> 103 104<span class="keyword">public</span><span class="special">:</span> 105 <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> 106 <span class="special">:</span> <span class="identifier">sock_</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">)</span> 107 <span class="special">{</span> 108 <span class="special">}</span> 109 110 <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> 111 <span class="special">{</span> 112 <span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span> 113 <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> 114 <span class="comment">// ignore the error</span> 115 <span class="special">}</span> 116<span class="special">};</span> 117</pre> 118<h5> 119<a name="beast.ref.boost__beast__close_socket.h4"></a> 120 <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> 121 </h5> 122<div class="informaltable"><table class="table"> 123<colgroup> 124<col> 125<col> 126</colgroup> 127<thead><tr> 128<th> 129 <p> 130 Name 131 </p> 132 </th> 133<th> 134 <p> 135 Description 136 </p> 137 </th> 138</tr></thead> 139<tbody><tr> 140<td> 141 <p> 142 <code class="computeroutput"><span class="identifier">sock</span></code> 143 </p> 144 </td> 145<td> 146 <p> 147 The socket to close. If the customization point is not defined 148 for the type of this object, or one of its base classes, then a 149 compiler error results. 150 </p> 151 </td> 152</tr></tbody> 153</table></div> 154<h5> 155<a name="beast.ref.boost__beast__close_socket.h5"></a> 156 <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 157 Also</a> 158 </h5> 159<p> 160 <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 </p> 162</div> 163<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 164<td align="left"></td> 165<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie 166 Falco<p> 167 Distributed under the Boost Software License, Version 1.0. (See accompanying 168 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>) 169 </p> 170</div></td> 171</tr></table> 172<hr> 173<div class="spirit-nav"> 174<a accesskey="p" href="boost__beast__beast_close_socket.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__iequals.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 175</div> 176</body> 177</html> 178