diff options
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/echo')
7 files changed, 446 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/echo/.gitignore b/3rdparty/asio/src/examples/cpp11/echo/.gitignore new file mode 100644 index 00000000000..0882fa6ced3 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/.gitignore @@ -0,0 +1,11 @@ +.deps +.dirstamp +*.o +*.obj +*.exe +*_server +*_client +*.ilk +*.manifest +*.pdb +*.tds diff --git a/3rdparty/asio/src/examples/cpp11/echo/async_tcp_echo_server.cpp b/3rdparty/asio/src/examples/cpp11/echo/async_tcp_echo_server.cpp new file mode 100644 index 00000000000..d76ebe94aae --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/async_tcp_echo_server.cpp @@ -0,0 +1,114 @@ +// +// async_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 <memory> +#include <utility> +#include "asio.hpp" + +using asio::ip::tcp; + +class session + : public std::enable_shared_from_this<session> +{ +public: + session(tcp::socket socket) + : socket_(std::move(socket)) + { + } + + void start() + { + do_read(); + } + +private: + void do_read() + { + auto self(shared_from_this()); + socket_.async_read_some(asio::buffer(data_, max_length), + [this, self](std::error_code ec, std::size_t length) + { + if (!ec) + { + do_write(length); + } + }); + } + + void do_write(std::size_t length) + { + auto self(shared_from_this()); + asio::async_write(socket_, asio::buffer(data_, length), + [this, self](std::error_code ec, std::size_t /*length*/) + { + if (!ec) + { + do_read(); + } + }); + } + + tcp::socket socket_; + enum { max_length = 1024 }; + char data_[max_length]; +}; + +class server +{ +public: + server(asio::io_context& io_context, short port) + : acceptor_(io_context, tcp::endpoint(tcp::v4(), port)) + { + do_accept(); + } + +private: + void do_accept() + { + acceptor_.async_accept( + [this](std::error_code ec, tcp::socket socket) + { + if (!ec) + { + std::make_shared<session>(std::move(socket))->start(); + } + + do_accept(); + }); + } + + tcp::acceptor acceptor_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: async_tcp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + server s(io_context, std::atoi(argv[1])); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp11/echo/async_udp_echo_server.cpp b/3rdparty/asio/src/examples/cpp11/echo/async_udp_echo_server.cpp new file mode 100644 index 00000000000..8eb1b37f8b6 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/async_udp_echo_server.cpp @@ -0,0 +1,82 @@ +// +// async_udp_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 "asio.hpp" + +using asio::ip::udp; + +class server +{ +public: + server(asio::io_context& io_context, short port) + : socket_(io_context, udp::endpoint(udp::v4(), port)) + { + do_receive(); + } + + void do_receive() + { + socket_.async_receive_from( + asio::buffer(data_, max_length), sender_endpoint_, + [this](std::error_code ec, std::size_t bytes_recvd) + { + if (!ec && bytes_recvd > 0) + { + do_send(bytes_recvd); + } + else + { + do_receive(); + } + }); + } + + void do_send(std::size_t length) + { + socket_.async_send_to( + asio::buffer(data_, length), sender_endpoint_, + [this](std::error_code /*ec*/, std::size_t /*bytes_sent*/) + { + do_receive(); + }); + } + +private: + udp::socket socket_; + udp::endpoint sender_endpoint_; + enum { max_length = 1024 }; + char data_[max_length]; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: async_udp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + server s(io_context, std::atoi(argv[1])); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp11/echo/blocking_tcp_echo_client.cpp b/3rdparty/asio/src/examples/cpp11/echo/blocking_tcp_echo_client.cpp new file mode 100644 index 00000000000..c7c648df97f --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/blocking_tcp_echo_client.cpp @@ -0,0 +1,55 @@ +// +// blocking_tcp_echo_client.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 <cstring> +#include <iostream> +#include "asio.hpp" + +using asio::ip::tcp; + +enum { max_length = 1024 }; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n"; + return 1; + } + + asio::io_context io_context; + + tcp::socket s(io_context); + tcp::resolver resolver(io_context); + asio::connect(s, resolver.resolve(argv[1], argv[2])); + + std::cout << "Enter message: "; + char request[max_length]; + std::cin.getline(request, max_length); + size_t request_length = std::strlen(request); + asio::write(s, asio::buffer(request, request_length)); + + char reply[max_length]; + size_t reply_length = asio::read(s, + asio::buffer(reply, request_length)); + std::cout << "Reply is: "; + std::cout.write(reply, reply_length); + std::cout << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp11/echo/blocking_tcp_echo_server.cpp b/3rdparty/asio/src/examples/cpp11/echo/blocking_tcp_echo_server.cpp new file mode 100644 index 00000000000..84a72d44453 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/blocking_tcp_echo_server.cpp @@ -0,0 +1,74 @@ +// +// 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 <thread> +#include <utility> +#include "asio.hpp" + +using asio::ip::tcp; + +const int max_length = 1024; + +void session(tcp::socket 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 (;;) + { + std::thread(session, a.accept()).detach(); + } +} + +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; + + server(io_context, std::atoi(argv[1])); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp11/echo/blocking_udp_echo_client.cpp b/3rdparty/asio/src/examples/cpp11/echo/blocking_udp_echo_client.cpp new file mode 100644 index 00000000000..1d15780c429 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/blocking_udp_echo_client.cpp @@ -0,0 +1,58 @@ +// +// blocking_udp_echo_client.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 <cstring> +#include <iostream> +#include "asio.hpp" + +using asio::ip::udp; + +enum { max_length = 1024 }; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: blocking_udp_echo_client <host> <port>\n"; + return 1; + } + + asio::io_context io_context; + + udp::socket s(io_context, udp::endpoint(udp::v4(), 0)); + + udp::resolver resolver(io_context); + udp::resolver::results_type endpoints = + resolver.resolve(udp::v4(), argv[1], argv[2]); + + std::cout << "Enter message: "; + char request[max_length]; + std::cin.getline(request, max_length); + size_t request_length = std::strlen(request); + s.send_to(asio::buffer(request, request_length), *endpoints.begin()); + + char reply[max_length]; + udp::endpoint sender_endpoint; + size_t reply_length = s.receive_from( + asio::buffer(reply, max_length), sender_endpoint); + std::cout << "Reply is: "; + std::cout.write(reply, reply_length); + std::cout << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp11/echo/blocking_udp_echo_server.cpp b/3rdparty/asio/src/examples/cpp11/echo/blocking_udp_echo_server.cpp new file mode 100644 index 00000000000..8bb51735f26 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp11/echo/blocking_udp_echo_server.cpp @@ -0,0 +1,52 @@ +// +// blocking_udp_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 "asio.hpp" + +using asio::ip::udp; + +enum { max_length = 1024 }; + +void server(asio::io_context& io_context, unsigned short port) +{ + udp::socket sock(io_context, udp::endpoint(udp::v4(), port)); + for (;;) + { + char data[max_length]; + udp::endpoint sender_endpoint; + size_t length = sock.receive_from( + asio::buffer(data, max_length), sender_endpoint); + sock.send_to(asio::buffer(data, length), sender_endpoint); + } +} + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: blocking_udp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + server(io_context, std::atoi(argv[1])); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} |