在Windows和Linux上提升udp接收器

时间:2014-01-30 04:23:34

标签: sockets boost boost-asio

我有一个简单的udp应用程序使用boost:asio编写,其中udp单播接收器在Windows上运行,单播发送器在linux上运行。我在linux上的单播发送者成功发送了数据包,但是在Windows上的reciver没有任何数据包,但我可以看到发送者在发送到windows上的wireshark发送的数据包。下面是我的udp接收器代码(基于提供我的升级库的示例)。

 class unicast_Receiver
 {
 public:
   unicast_Receiver(boost::asio::io_service& io_service,    boost::asio::ip::udp::endpoint end1)
     : socket_(io_service)
   {


     socket_.open(boost::asio::ip::udp::v4());
     //socket_.open(end1.protocol());
     socket_.bind(end1);
     socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
     std::cout  << " >> on  " <<socket_.local_endpoint()<<" :: @"  << __LINE__ << __FUNCTION__   <<std::endl;
     start_receive();

   }

 private:
   void start_receive()
   {
      std::cout  << "\n !!!!Waiting in the Unicast Data Receive >> on  " << Unicastlisten_endpoint_ <<" :: @"  << __LINE__ << __FUNCTION__   <<std::endl;
     socket_.async_receive_from(
           boost::asio::buffer(data_, max_length), remote_endpoint_,
         boost::bind(&unicast_Receiver::handle_receive, this,
           boost::asio::placeholders::error,
           boost::asio::placeholders::bytes_transferred));
   }

   void handle_receive(const boost::system::error_code& error,
       std::size_t /*bytes_transferred*/)
   {
     if (!error || error == boost::asio::error::message_size)
     {

       std::cout  << "Unicast Data Received >> " <<"<< @"  << __LINE__ << __FUNCTION__   <<std::endl;
      std::cout << "\n" <<  data_  << "\n";
      memset(data_,0,max_length);
      start_receive();
     }
   }

   void handle_send(boost::shared_ptr<std::string> /*message*/,
       const boost::system::error_code& /*error*/,
       std::size_t /*bytes_transferred*/)
   {
   }


   udp::socket socket_;
   udp::endpoint remote_endpoint_;
   enum { max_length = 1024 };
   char data_[max_length];
   boost::array<char, 1> recv_buffer_;
 };


 int main(int argc, char* argv[])
 {
   boost::asio::io_service io_serviceSe;
   boost::asio::ip::udp::endpoint  Unicastlisten_endpoint_(boost::asio::ip::address::from_string(argv[1]),atoi(argv[2]));
   unicast_Receiver server(io_serviceSe,Unicastlisten_endpoint_);
 }

0 个答案:

没有答案