]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/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)
13 #include <boost/bind.hpp>
14 #include <boost/asio.hpp>
16 using boost::asio::ip::tcp
;
21 session(boost::asio::io_context
& io_context
)
33 socket_
.async_read_some(boost::asio::buffer(data_
, max_length
),
34 boost::bind(&session::handle_read
, this,
35 boost::asio::placeholders::error
,
36 boost::asio::placeholders::bytes_transferred
));
40 void handle_read(const boost::system::error_code
& error
,
41 size_t bytes_transferred
)
45 boost::asio::async_write(socket_
,
46 boost::asio::buffer(data_
, bytes_transferred
),
47 boost::bind(&session::handle_write
, this,
48 boost::asio::placeholders::error
));
56 void handle_write(const boost::system::error_code
& error
)
60 socket_
.async_read_some(boost::asio::buffer(data_
, max_length
),
61 boost::bind(&session::handle_read
, this,
62 boost::asio::placeholders::error
,
63 boost::asio::placeholders::bytes_transferred
));
72 enum { max_length
= 1024 };
73 char data_
[max_length
];
79 server(boost::asio::io_context
& io_context
, short port
)
80 : io_context_(io_context
),
81 acceptor_(io_context
, tcp::endpoint(tcp::v4(), port
))
89 session
* new_session
= new session(io_context_
);
90 acceptor_
.async_accept(new_session
->socket(),
91 boost::bind(&server::handle_accept
, this, new_session
,
92 boost::asio::placeholders::error
));
95 void handle_accept(session
* new_session
,
96 const boost::system::error_code
& error
)
100 new_session
->start();
110 boost::asio::io_context
& io_context_
;
111 tcp::acceptor acceptor_
;
114 int main(int argc
, char* argv
[])
120 std::cerr
<< "Usage: async_tcp_echo_server <port>\n";
124 boost::asio::io_context io_context
;
126 using namespace std
; // For atoi.
127 server
s(io_context
, atoi(argv
[1]));
131 catch (std::exception
& e
)
133 std::cerr
<< "Exception: " << e
.what() << "\n";