服务端文件websocket_server_async_ssl.cpp
客户端文件websocket_client_async_ssl.cpp
host:0.0.0.0 port:8080
段错误在服务端代码main函数里面 ioc.run();这一句
bt信息
#0 shared_count (r=..., this=0x13f83d8)
at /opt/translate_proxy/third-party/install/boost/include/boost/smart_ptr/detail/shared_count.hpp:438
#1 shared_ptr (r=..., this=0x13f83d0)
at /opt/translate_proxy/third-party/install/boost/include/boost/smart_ptr/shared_ptr.hpp:422
#2 http_session::http_session(boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>&&, boost::shared_ptr<shared_state> const&) (this=0x13f8380, socket=<optimized out>, state=...)
at /mnt/hgfs/work/translate_proxy/test_server/http_session.cpp:230
#3 0x0000000000872137 in boost::make_shared<http_session<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>, boost::shared_ptr<shared_state>&> > ()
at /opt/translate_proxy/third-party/install/boost/include/boost/smart_ptr/make_shared_object.hpp:256
#4 0x000000000087123f in listener::on_accept (this=0x13f99e8, ec=..., socket=...)
at /mnt/hgfs/work/translate_proxy/test_server/listener.cpp:93
#5 0x00000000007f324f in _M_call<std::shared_ptr<listener>, boost::system::error_code const&, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > > (
__ptr=<unknown type in /opt/translate_proxy/web_srv, CU 0x0, DIE 0x28f157>, this=<optimized out>)
at /usr/include/c++/4.8.2/functional:558
#6 operator()<std::shared_ptr<listener>, const boost::system::error_code&, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> >, void> (
__object=<unknown type in /opt/translate_proxy/web_srv, CU 0x0, DIE 0x1808f5>, this=<optimized out>)
at /usr/include/c++/4.8.2/functional:610
#7 invoke<0ul, boost::system::error_code const&, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > > (this=0x7fffffffdce0)
at /opt/translate_proxy/third-party/install/boost/include/boost/beast/core/detail/bind_handler.hpp:235
#8 operator()<const boost::system::error_code&, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > > (this=0x7fffffffdce0)
at /opt/translate_proxy/third-party/install/boost/include/boost/beast/core/detail/bind_handler.hpp:258
#9 boost::asio::detail::move_binder2<boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>), std::shared_ptr<listener> >, boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > >::operator() (this=this@entry=0x7fffffffdce0)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/bind_handler.hpp:666
#10 0x00000000007f33d6 in boost::asio::detail::executor_function<boost::asio::detail::move_binder2<boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>), std::shared_ptr<listener> >, boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > >, std::allocator<void> >::do_complete (
base=<optimized out>, call=<optimized out>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/executor_function.hpp:91
#11 0x00000000007cacc3 in complete (this=0x13f9480)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/executor_function.hpp:32
#12 operator() (this=<synthetic pointer>)
---Type <return> to continue, or q <return> to quit---
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/impl/executor.hpp:69
#13 asio_handler_invoke<boost::asio::executor::function> (function=<synthetic pointer>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/handler_invoke_hook.hpp:69
#14 invoke<boost::asio::executor::function, boost::asio::executor::function> (context=<synthetic pointer>,
function=<synthetic pointer>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/handler_invoke_helpers.hpp:37
#15 boost::asio::detail::executor_op<boost::asio::executor::function, std::allocator<void>, boost::asio::detail::scheduler_operation>::do_complete (owner=0x13fb1d0, base=<optimized out>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/executor_op.hpp:70
#16 0x000000000086fecd in complete (bytes_transferred=0, ec=..., owner=<optimized out>, this=<optimized out>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/scheduler_operation.hpp:40
#17 boost::asio::detail::strand_executor_service::invoker<boost::asio::io_context::executor_type const>::operator() (
this=this@entry=0x7fffffffde40)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/impl/strand_executor_service.hpp:90
#18 0x0000000000870091 in asio_handler_invoke<boost::asio::detail::strand_executor_service::invoker<boost::asio::io_context::executor_type const> > (function=...)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/handler_invoke_hook.hpp:69
#19 invoke<boost::asio::detail::strand_executor_service::invoker<boost::asio::io_context::executor_type const>, boost::asio::detail::strand_executor_service::invoker<boost::asio::io_context::executor_type const> > (context=..., function=...)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/handler_invoke_helpers.hpp:37
#20 boost::asio::io_context::executor_type::dispatch<boost::asio::detail::strand_executor_service::invoker<boost::asio::io_context::executor_type const>, std::allocator<void> >(boost::asio::detail::strand_executor_service::invoker<boost::asio::io_context::executor_type const>&&, std::allocator<void> const&) const (this=this@entry=0x13fb268,
f=f@entry=<unknown type in /opt/translate_proxy/web_srv, CU 0x0, DIE 0x46465c>, a=...)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/impl/io_context.hpp:265
#21 0x00000000008701f1 in boost::asio::detail::strand_executor_service::dispatch<boost::asio::io_context::executor_type const, boost::asio::executor::function, std::allocator<void> >(std::shared_ptr<boost::asio::detail::strand_executor_service::strand_impl> const&, boost::asio::io_context::executor_type const&, boost::asio::executor::function&&, std::allocator<void> const&) (impl=std::shared_ptr (count 2, weak 0) 0x13fb1d0, ex=...,
function=<unknown type in /opt/translate_proxy/web_srv, CU 0x0, DIE 0x464b58>, a=...)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/impl/strand_executor_service.hpp:128
#22 0x00000000007f34fe in boost::asio::executor::dispatch<boost::asio::detail::move_binder2<boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>), std::shared_ptr<listener> >, boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > >, std::allocator<void> >(boost::asio::detail::move_binder2<boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>), std::shared_ptr<listener> >, boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > >&&, std::allocator<void> const&) const (this=this@entry=0x7fffffffe040,
f=f@entry=<unknown type in /opt/translate_proxy/web_srv, CU 0x0, DIE 0x290500>, a=...)
---Type <return> to continue, or q <return> to quit---
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/impl/executor.hpp:351
#23 0x00000000007f382b in dispatch<boost::asio::detail::move_binder2<boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp>), std::shared_ptr<listener> >, boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > >, std::allocator<void> > (a=...,
f=<unknown type in /opt/translate_proxy/web_srv, CU 0x0, DIE 0x2906c5>, this=0x7fffffffe040)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/io_object_executor.hpp:121
#24 complete<boost::asio::detail::move_binder2<boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp>), std::shared_ptr<listener> >, boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type> > > > (handler=..., function=..., this=0x7fffffffe030)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/handler_work.hpp:72
#25 boost::asio::detail::reactive_socket_move_accept_op<boost::asio::ip::tcp, boost::asio::strand<boost::asio::io_context::executor_type>, boost::beast::detail::bind_front_wrapper<void (listener::*)(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>), std::shared_ptr<listener> >, boost::asio::detail::io_object_executor<boost::asio::executor> >::do_complete (owner=<optimized out>, base=<optimized out>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/reactive_socket_accept_op.hpp:211
#26 0x00000000007cd26f in complete (bytes_transferred=<optimized out>, ec=..., owner=0x13f6f30, this=<optimized out>)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/scheduler_operation.hpp:40
#27 boost::asio::detail::scheduler::do_run_one (this=this@entry=0x13f6f30, lock=..., this_thread=..., ec=...)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/impl/scheduler.ipp:447
#28 0x00000000007d7491 in boost::asio::detail::scheduler::run (this=0x13f6f30, ec=...)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/detail/impl/scheduler.ipp:200
#29 0x00000000007c052a in run (this=0x7fffffffe2c0)
at /opt/translate_proxy/third-party/install/boost/include/boost/asio/impl/io_context.ipp:63
#30 main (argc=<optimized out>, argv=<optimized out>) at /mnt/hgfs/work/translate_proxy/test_server/main.cpp:299
// -----------------------------客户端代码
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
//------------------------------------------------------------------------------
//
// Example: WebSocket SSL client, asynchronous
//
//------------------------------------------------------------------------------
#include "example/common/root_certificates.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/strand.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<
beast::ssl_stream<beast::tcp_stream>> ws_;
beast::flat_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_context
explicit
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(net::make_strand(ioc))
, ws_(net::make_strand(ioc), ctx)
{
}
// Start the asynchronous operation
void
run(
char const* host,
char const* port,
char const* text)
{
// Save these for later
host_ = host;
text_ = text;
// Look up the domain name
resolver_.async_resolve(
host,
port,
beast::bind_front_handler(
&session::on_resolve,
shared_from_this()));
}
void
on_resolve(
beast::error_code ec,
tcp::resolver::results_type results)
{
if(ec)
return fail(ec, "resolve");
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
beast::get_lowest_layer(ws_).async_connect(
results,
beast::bind_front_handler(
&session::on_connect,
shared_from_this()));
}
void
on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{
if(ec)
return fail(ec, "connect");
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake
ws_.next_layer().async_handshake(
ssl::stream_base::client,
beast::bind_front_handler(
&session::on_ssl_handshake,
shared_from_this()));
}
void
on_ssl_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "ssl_handshake");
// Turn off the timeout on the tcp_stream, because
// the websocket stream has its own timeout system.
beast::get_lowest_layer(ws_).expires_never();
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::timeout::suggested(
beast::role_type::client));
// Set a decorator to change the User-Agent of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-async-ssl");
}));
// Perform the websocket handshake
ws_.async_handshake(host_, "/",
beast::bind_front_handler(
&session::on_handshake,
shared_from_this()));
}
void
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Send the message
ws_.async_write(
net::buffer(text_),
beast::bind_front_handler(
&session::on_write,
shared_from_this()));
}
void
on_write(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "write");
// Read a message into our buffer
ws_.async_read(
buffer_,
beast::bind_front_handler(
&session::on_read,
shared_from_this()));
}
void
on_read(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws_.async_close(websocket::close_code::normal,
beast::bind_front_handler(
&session::on_close,
shared_from_this()));
}
void
on_close(beast::error_code ec)
{
if(ec)
return fail(ec, "close");
// If we get here then the connection is closed gracefully
// The make_printable() function helps print a ConstBufferSequence
std::cout << beast::make_printable(buffer_.data()) << std::endl;
}
};
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: websocket-client-async-ssl <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-async-ssl echo.websocket.org 443 \"Hello, world!\"\n";
return EXIT_FAILURE;
}
auto const host = argv[1];
auto const port = argv[2];
auto const text = argv[3];
// The io_context is required for all I/O
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::tlsv12_client};
// This holds the root certificate used for verification
load_root_certificates(ctx);
// Launch the asynchronous operation
std::make_shared<session>(ioc, ctx)->run(host, port, text);
// Run the I/O service. The call will return when
// the socket is closed.
ioc.run();
return EXIT_SUCCESS;
}
// -----------------------------客户端代码
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
//------------------------------------------------------------------------------
//
// Example: WebSocket SSL server, asynchronous
//
//------------------------------------------------------------------------------
#include "example/common/server_certificate.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/strand.hpp>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Echoes back all received WebSocket messages
class session : public std::enable_shared_from_this<session>
{
websocket::stream<
beast::ssl_stream<beast::tcp_stream>> ws_;
beast::flat_buffer buffer_;
public:
// Take ownership of the socket
session(tcp::socket&& socket, ssl::context& ctx)
: ws_(std::move(socket), ctx)
{
}
// Start the asynchronous operation
void
run()
{
// Set the timeout.
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake
ws_.next_layer().async_handshake(
ssl::stream_base::server,
beast::bind_front_handler(
&session::on_handshake,
shared_from_this()));
}
void
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Turn off the timeout on the tcp_stream, because
// the websocket stream has its own timeout system.
beast::get_lowest_layer(ws_).expires_never();
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::timeout::suggested(
beast::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-async-ssl");
}));
// Accept the websocket handshake
ws_.async_accept(
beast::bind_front_handler(
&session::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec)
{
if(ec)
return fail(ec, "accept");
// Read a message
do_read();
}
void
do_read()
{
// Read a message into our buffer
ws_.async_read(
buffer_,
beast::bind_front_handler(
&session::on_read,
shared_from_this()));
}
void
on_read(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This indicates that the session was closed
if(ec == websocket::error::closed)
return;
if(ec)
fail(ec, "read");
// Echo the message
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
beast::bind_front_handler(
&session::on_write,
shared_from_this()));
}
void
on_write(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "write");
// Clear the buffer
buffer_.consume(buffer_.size());
// Do another read
do_read();
}
};
//------------------------------------------------------------------------------
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
public:
listener(
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(net::make_strand(ioc))
{
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if(ec)
{
fail(ec, "open");
return;
}
// Allow address reuse
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
if(ec)
{
fail(ec, "set_option");
return;
}
// Bind to the server address
acceptor_.bind(endpoint, ec);
if(ec)
{
fail(ec, "bind");
return;
}
// Start listening for connections
acceptor_.listen(
net::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
return;
}
}
// Start accepting incoming connections
void
run()
{
do_accept();
}
private:
void
do_accept()
{
// The new connection gets its own strand
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
{
if(ec)
{
fail(ec, "accept");
}
else
{
// Create the session and run it
std::make_shared<session>(std::move(socket), ctx_)->run();
}
// Accept another connection
do_accept();
}
};
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
// Check command line arguments.
if (argc != 4)
{
std::cerr <<
"Usage: websocket-server-async-ssl <address> <port> <threads>\n" <<
"Example:\n" <<
" websocket-server-async-ssl 0.0.0.0 8080 1\n";
return EXIT_FAILURE;
}
auto const address = net::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_context is required for all I/O
net::io_context ioc{threads};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::tlsv12};
// This holds the self-signed certificate used by the server
load_server_certificate(ctx);
// Create and launch a listening port
std::make_shared<listener>(ioc, ctx, tcp::endpoint{address, port})->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ioc]
{
ioc.run();
});
ioc.run();
return EXIT_SUCCESS;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: