]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/echo/blocking_tcp_echo_server.cpp
2 // blocking_tcp_echo_server.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 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
;
19 const int max_length
= 1024;
21 void session(tcp::socket sock
)
27 char data
[max_length
];
29 boost::system::error_code error
;
30 size_t length
= sock
.read_some(boost::asio::buffer(data
), error
);
31 if (error
== boost::asio::error::eof
)
32 break; // Connection closed cleanly by peer.
34 throw boost::system::system_error(error
); // Some other error.
36 boost::asio::write(sock
, boost::asio::buffer(data
, length
));
39 catch (std::exception
& e
)
41 std::cerr
<< "Exception in thread: " << e
.what() << "\n";
45 void server(boost::asio::io_context
& io_context
, unsigned short port
)
47 tcp::acceptor
a(io_context
, tcp::endpoint(tcp::v4(), port
));
50 std::thread(session
, a
.accept()).detach();
54 int main(int argc
, char* argv
[])
60 std::cerr
<< "Usage: blocking_tcp_echo_server <port>\n";
64 boost::asio::io_context io_context
;
66 server(io_context
, std::atoi(argv
[1]));
68 catch (std::exception
& e
)
70 std::cerr
<< "Exception: " << e
.what() << "\n";