From ff01b716711b97c2fcaa709ea97f7650f106aa10 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 7 Oct 2016 14:13:19 +0200 Subject: Added ASIO networking library (nw) --- .../cpp03/echo/blocking_udp_echo_server.cpp | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp (limited to '3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp') diff --git a/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp b/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp new file mode 100644 index 00000000000..3cc364c20b5 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp @@ -0,0 +1,53 @@ +// +// 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 +#include +#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 \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; +} -- cgit v1.2.3-70-g09d2