diff options
author | 2016-10-07 14:13:19 +0200 | |
---|---|---|
committer | 2016-10-07 14:13:19 +0200 | |
commit | ff01b716711b97c2fcaa709ea97f7650f106aa10 (patch) | |
tree | 50dd4d687f38f50c4e136af030c02c267c769f3a /3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp | |
parent | 2a138159c30457aecd9e9679be5159704db0f954 (diff) |
Added ASIO networking library (nw)
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp b/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp new file mode 100644 index 00000000000..163dc2a392c --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp @@ -0,0 +1,79 @@ +// +// blocking_tcp_echo_server.cpp +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff 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) +// + +#include <cstdlib> +#include <iostream> +#include <boost/bind.hpp> +#include <boost/smart_ptr.hpp> +#include "asio.hpp" + +using asio::ip::tcp; + +const int max_length = 1024; + +typedef boost::shared_ptr<tcp::socket> socket_ptr; + +void session(socket_ptr sock) +{ + try + { + for (;;) + { + char data[max_length]; + + asio::error_code error; + size_t length = sock->read_some(asio::buffer(data), error); + if (error == asio::error::eof) + break; // Connection closed cleanly by peer. + else if (error) + throw asio::system_error(error); // Some other error. + + asio::write(*sock, asio::buffer(data, length)); + } + } + catch (std::exception& e) + { + std::cerr << "Exception in thread: " << e.what() << "\n"; + } +} + +void server(asio::io_context& io_context, unsigned short port) +{ + tcp::acceptor a(io_context, tcp::endpoint(tcp::v4(), port)); + for (;;) + { + socket_ptr sock(new tcp::socket(io_context)); + a.accept(*sock); + asio::thread t(boost::bind(session, sock)); + } +} + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: blocking_tcp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + using namespace std; // For atoi. + server(io_context, atoi(argv[1])); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} |