Advertisement
Guest User

asio UDP sample

a guest
Oct 22nd, 2010
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <boost/bind.hpp>
  4. #include <boost/asio.hpp>
  5. #include <boost/thread.hpp>
  6. #include <boost/date_time.hpp>
  7. #include <boost/foreach.hpp>
  8.  
  9. using boost::asio::ip::udp;
  10.  
  11. class server
  12. {
  13. public:
  14.   server(boost::asio::io_service& io_service, short port)
  15.     : io_service_(io_service),
  16.       socket_(io_service, udp::endpoint(udp::v4(), port)),
  17.       messages_sent_(0u)
  18.   {
  19.     socket_.async_receive_from(
  20.         boost::asio::buffer(data_, max_length), sender_endpoint_,
  21.         boost::bind(&server::handle_receive_from, this,
  22.           boost::asio::placeholders::error,
  23.           boost::asio::placeholders::bytes_transferred));
  24.   }
  25.  
  26.   void handle_receive_from(const boost::system::error_code& error,
  27.       size_t bytes_recvd)
  28.   {
  29.     if (error) {
  30.         std::cout << "receive error!" << std::endl;
  31.         return;
  32.     }
  33.  
  34.         std::string  message(data_, bytes_recvd);
  35.  
  36.       socket_.async_receive_from(
  37.           boost::asio::buffer(data_, max_length), sender_endpoint_,
  38.           boost::bind(&server::handle_receive_from, this,
  39.             boost::asio::placeholders::error,
  40.             boost::asio::placeholders::bytes_transferred));
  41.  
  42.       boost::lock_guard<boost::recursive_mutex>    lock(mutex_);
  43.  
  44.         messages_.push_back(message);
  45.   }
  46.  
  47.   void handle_send_to(const boost::system::error_code& error,
  48.       size_t /*bytes_sent*/)
  49.   {
  50.     if (error) {
  51.         std::cout << "send error!" << std::endl;
  52.     }
  53.  
  54.     ++messages_sent_;
  55.  
  56.     socket_.async_receive_from(
  57.         boost::asio::buffer(data_, max_length), sender_endpoint_,
  58.         boost::bind(&server::handle_receive_from, this,
  59.           boost::asio::placeholders::error,
  60.           boost::asio::placeholders::bytes_transferred));
  61.   }
  62.  
  63.   void send(udp::endpoint other_end, const std::string &message) {
  64.       socket_.async_send_to(
  65.           boost::asio::buffer(message.c_str(), message.size()),
  66.           other_end,
  67.           boost::bind(&server::handle_send_to, this,
  68.             boost::asio::placeholders::error,
  69.             boost::asio::placeholders::bytes_transferred));
  70.   }
  71.  
  72.  
  73.   std::vector<std::string>  messages_;
  74.   unsigned int messages_sent_;
  75.  
  76. private:
  77.   boost::asio::io_service& io_service_;
  78.   udp::socket socket_;
  79.   udp::endpoint sender_endpoint_;
  80.   enum { max_length = 1024 };
  81.   char data_[max_length];
  82.   boost::recursive_mutex  mutex_;
  83. };
  84.  
  85. static boost::asio::io_service io_service;
  86. static bool                    should_run;
  87.  
  88. void service_thread(void) {
  89.     while (should_run) {
  90.         io_service.run();
  91.  
  92.         io_service.reset();
  93.     }
  94. }
  95.  
  96.  
  97. int main(int argc, char* argv[])
  98. {
  99.   try
  100.   {
  101.     server a(io_service, 4567);
  102.     server b(io_service, 4568);
  103.  
  104.     should_run = true;
  105.  
  106.     boost::thread   t(service_thread);
  107.  
  108.     // generate an endpoint for 'a'
  109.     udp::resolver           resolver(io_service);
  110.     udp::resolver::query    query(udp::v4(), "localhost", "4567");
  111.     udp::endpoint           endp_a = *resolver.resolve(query);
  112.  
  113.     for (unsigned int i = 0; i < 2000; ++i) {
  114.         std::stringstream   sstr;
  115.         sstr << i;
  116.         b.send(endp_a, sstr.str());
  117.  
  118.         boost::this_thread::yield();
  119.     }
  120.  
  121.     boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
  122.  
  123.     BOOST_FOREACH(const std::string &str, a.messages_) {
  124.         std::cout << str << std::endl;
  125.     }
  126.     std::cout << "b sent " << b.messages_sent_ << " messages"
  127.               << std::endl;
  128.     std::cout << "a received " << a.messages_.size() << " messages"
  129.               << std::endl;
  130.  
  131.     // stop the listening thread
  132.     should_run = false;
  133.     io_service.stop();
  134.     t.join();
  135.   }
  136.   catch (std::exception& e)
  137.   {
  138.     std::cerr << "Exception: " << e.what() << "\n";
  139.   }
  140.  
  141.   return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement