]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/echo/async_tcp_echo_server.cpp
2 // async_tcp_echo_server.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15 #include <boost/asio.hpp>
17 using boost::asio::ip::tcp
;
20 : public std::enable_shared_from_this
<session
>
23 session(tcp::socket socket
)
24 : socket_(std::move(socket
))
36 auto self(shared_from_this());
37 socket_
.async_read_some(boost::asio::buffer(data_
, max_length
),
38 [this, self
](boost::system::error_code ec
, std::size_t length
)
47 void do_write(std::size_t length
)
49 auto self(shared_from_this());
50 boost::asio::async_write(socket_
, boost::asio::buffer(data_
, length
),
51 [this, self
](boost::system::error_code ec
, std::size_t /*length*/)
61 enum { max_length
= 1024 };
62 char data_
[max_length
];
68 server(boost::asio::io_context
& io_context
, short port
)
69 : acceptor_(io_context
, tcp::endpoint(tcp::v4(), port
))
77 acceptor_
.async_accept(
78 [this](boost::system::error_code ec
, tcp::socket socket
)
82 std::make_shared
<session
>(std::move(socket
))->start();
89 tcp::acceptor acceptor_
;
92 int main(int argc
, char* argv
[])
98 std::cerr
<< "Usage: async_tcp_echo_server <port>\n";
102 boost::asio::io_context io_context
;
104 server
s(io_context
, std::atoi(argv
[1]));
108 catch (std::exception
& e
)
110 std::cerr
<< "Exception: " << e
.what() << "\n";