2 // blocking_udp_echo_server.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2014 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/asio.hpp>
15 using boost::asio::ip::udp;
17 enum { max_length = 1024 };
19 void server(boost::asio::io_service& io_service, unsigned short port)
21 udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
24 char data[max_length];
25 udp::endpoint sender_endpoint;
26 size_t length = sock.receive_from(
27 boost::asio::buffer(data, max_length), sender_endpoint);
28 sock.send_to(boost::asio::buffer(data, length), sender_endpoint);
32 int main(int argc, char* argv[])
38 std::cerr << "Usage: blocking_udp_echo_server <port>\n";
42 boost::asio::io_service io_service;
44 using namespace std; // For atoi.
45 server(io_service, atoi(argv[1]));
47 catch (std::exception& e)
49 std::cerr << "Exception: " << e.what() << "\n";