[Boost-bugs] [Boost C++ Libraries] #9406: [asio] The asio handle shouldnot be called

Subject: [Boost-bugs] [Boost C++ Libraries] #9406: [asio] The asio handle shouldnot be called
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2013-11-18 09:32:18


#9406: [asio] The asio handle shouldnot be called
------------------------------+----------------------------
 Reporter: icerlion@… | Owner: chris_kohlhoff
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: asio
  Version: Boost 1.55.0 | Severity: Problem
 Keywords: |
------------------------------+----------------------------
 I want to write a UDP server, which can send and receive package. The
 following is the demo code:

 // Begin of code
 #include "stdafx.h"
 #include "boost/bind.hpp"
 #include "boost/asio.hpp"

 class UDPServer
 {
 public:
     UDPServer()
         :m_socket(m_io_service),
         m_timer(m_io_service)
     {
     }

     virtual ~UDPServer()
     {

     }

     void Initialize(const std::string& strIP, int nPort)
     {
         boost::system::error_code ec;
         boost::asio::ip::address ad =
 boost::asio::ip::address::from_string(strIP, ec);
         boost::asio::ip::udp::endpoint ep(ad, nPort);
         m_socket.open(ep.protocol());
         m_socket.bind(ep, ec);
         {
             // TEST CODE:
             boost::asio::ip::udp::endpoint
 ep(boost::asio::ip::address::from_string("172.18.8.111", ec), 8001);
             m_vServerGroup.push_back(ep);
         }
     }

     void Start()
     {
         StartAsyncReceiveFrom();
         NoticeSelfUp();
         while (true)
         {
             boost::system::error_code ec;
             m_io_service.poll(ec);
         }
     }

     void SendUDPMsg(const char* pMsg, int nLen,
 boost::asio::ip::udp::endpoint& dest_ep)
     {
         m_socket.async_send_to(
             boost::asio::buffer(pMsg, nLen),
             dest_ep,
             boost::bind(&UDPServer::HandleSendTo, this, _1, _2,
 boost::ref(dest_ep)));
     }

 protected:
     void NoticeSelfUp()
     {
         size_t count = m_vServerGroup.size();
         char* pBuff = "this is up";
         for (size_t i = 0; i < count; ++i)
         {
             SendUDPMsg(pBuff, strlen(pBuff), m_vServerGroup[i]);
         }
         m_timer.expires_from_now(boost::posix_time::seconds(5));
         m_timer.async_wait(boost::bind(&UDPServer::NoticeSelfUp, this));
     }

     void HandleReceiveFrom(const boost::system::error_code& ec, size_t
 bytes_recvd)
     {
         if (ec)
         {
             std::cout<<"HandleReceiveFrom error: \t"<<ec<<std::endl;
         }
         else
         {
             // TODO
         }
         StartAsyncReceiveFrom();
     }

     void HandleSendTo(const boost::system::error_code& ec, size_t
 bytes_sent, boost::asio::ip::udp::endpoint& ep)
     {
         if (ec)
         {
             std::cout<<"HandleReceiveFrom error:
 \t"<<ec<<"\t"<<ep<<std::endl;
         }
         else
         {
             // TODO
         }
     }

     void StartAsyncReceiveFrom()
     {
         m_socket.async_receive_from(boost::asio::buffer(m_buff,
 max_length),
             m_sender_point,
             boost::bind(&UDPServer::HandleReceiveFrom, this, _1, _2));
     }

 private:
     boost::asio::io_service m_io_service;
     boost::asio::ip::udp::socket m_socket;
     boost::asio::ip::udp::endpoint m_sender_point;
     boost::asio::deadline_timer m_timer;
     std::vector<boost::asio::ip::udp::endpoint> m_vServerGroup;
     enum { max_length = 1024 };
     char m_buff[max_length];
 };

 int _tmain(int argc, _TCHAR* argv[])
 {
     UDPServer server;
     server.Initialize("172.18.8.111", 8000);
     server.Start();
     return 0;
 }

 // End of code

 What's more, localmachine IP is 172.18.8.111, the port number 8001 is not
 opened. After I call SendUDPMsg, the handle UDPServer::HandleReceiveFrom
 is called, but there is no receive operation for the socket, I just called
 the send function. In my opinion, the HandleReceiveFrom should not be
 called!

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/9406>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:14 UTC