| Home | Libraries | People | FAQ | More

# echo/blocking_udp_echo_server.cpp

    #include <cstdlib>
    #include <iostream>
    #include <boost/asio.hpp>

    using boost::asio::ip::udp;

    enum { max_length = 1024 };

    void server(boost::asio::io_service& io_service, short port)
    {
      udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
      for (;;)
      {
        char data[max_length];
        udp::endpoint sender_endpoint;
        size_t length = sock.receive_from(
            boost::asio::buffer(data, max_length), sender_endpoint);
        sock.send_to(boost::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;
        }

        boost::asio::io_service io_service;

        using namespace std; // For atoi.
        server(io_service, atoi(argv[1]));
      }
      catch (boost::asio::error& e)
      {
        std::cerr << e << "\n";
      }
      catch (std::exception& e)
      {
        std::cerr << "Exception: " << e.what() << "\n";
      }

      return 0;
    }

| Copyright © 2003 - 2006 Christopher M. Kohlhoff