• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Connecting</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="../using_websocket.html" title="WebSocket">
9<link rel="prev" href="../using_websocket.html" title="WebSocket">
10<link rel="next" href="handshaking.html" title="Handshaking">
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="../using_websocket.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.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="handshaking.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="beast.using_websocket.establishing_connections"></a><a class="link" href="establishing_connections.html" title="Connecting">Connecting</a>
28</h3></div></div></div>
29<p>
30        Before messages can be exchanged, a websocket stream first needs to be connected,
31        and then to have the websocket handshake performed. The stream delegates
32        the task of establishing the connection to the next layers. For example,
33        if the next layer is a connectible stream or socket object, it can be accessed
34        to call the necessary function for connecting. Here we make an outbound connection
35        as a client would do.
36      </p>
37<pre class="programlisting"><span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>
38<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">resolver</span> <span class="identifier">resolver</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>
39
40<span class="comment">// Connect the socket to the IP address returned from performing a name lookup</span>
41<span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">ws</span><span class="special">).</span><span class="identifier">connect</span><span class="special">(</span><span class="identifier">resolver</span><span class="special">.</span><span class="identifier">resolve</span><span class="special">(</span><span class="string">"example.com"</span><span class="special">,</span> <span class="string">"ws"</span><span class="special">));</span>
42</pre>
43<p>
44        To accept incoming connections, an acceptor is used. The websocket stream
45        may be constructed from the socket returned by the acceptor when an incoming
46        connection is established.
47      </p>
48<pre class="programlisting"><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">acceptor</span> <span class="identifier">acceptor</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>
49<span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">bind</span><span class="special">(</span><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">endpoint</span><span class="special">(</span><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">v4</span><span class="special">(),</span> <span class="number">0</span><span class="special">));</span>
50<span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">listen</span><span class="special">();</span>
51
52<span class="comment">// The socket returned by accept() will be forwarded to the tcp_stream,</span>
53<span class="comment">// which uses it to perform a move-construction from the net::ip::tcp::socket.</span>
54
55<span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">accept</span><span class="special">());</span>
56</pre>
57<p>
58        Alternatively, the incoming connection may be accepted directly into the
59        socket owned by the websocket stream, using this overload of the acceptor
60        member function.
61      </p>
62<pre class="programlisting"><span class="comment">// The stream will use the strand for invoking all completion handlers</span>
63<span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">make_strand</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">));</span>
64
65<span class="comment">// This overload of accept uses the socket provided for the new connection.</span>
66<span class="comment">// The function `tcp_stream::socket` provides access to the low-level socket</span>
67<span class="comment">// object contained in the tcp_stream.</span>
68
69<span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">accept</span><span class="special">(</span><span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">ws</span><span class="special">).</span><span class="identifier">socket</span><span class="special">());</span>
70</pre>
71</div>
72<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
73<td align="left"></td>
74<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
75      Falco<p>
76        Distributed under the Boost Software License, Version 1.0. (See accompanying
77        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>)
78      </p>
79</div></td>
80</tr></table>
81<hr>
82<div class="spirit-nav">
83<a accesskey="p" href="../using_websocket.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.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="handshaking.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
84</div>
85</body>
86</html>
87