Boost ASIO. UDP.



  • Hey, everybody, there's a need to take the packages from the four devices on the UDP protocol. Used to boost:asio. The problem is, when the broadcast is sent, when I'm connected to the devices and the reception, I get the same data packages from all, although wireshark shows that it's different. That's the example I'm trying to take.

    io_service service;
    ip::udp::socket sock_mu(service); 
    ip::udp::socket sock_s1(service); 
    ip::udp::endpoint ep_m(ip::address::from_string("0.0.0.0"), port);  
    ip::udp::endpoint ep_s1(ip::address::from_string("0.0.0.0"), port); 
    sock_mu.open(boost::asio::ip::udp::v4()); 
    sock_s1.open(boost::asio::ip::udp::v4());
    sock_mu.set_option(boost::asio::socket_base::broadcast(true)); 
    sock_s1.set_option(boost::asio::socket_base::broadcast(true)); 
    sock_mu.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    sock_s1.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    sock_mu.bind(ep_m);
    sock_mu.bind(ep_s1);
    ip::udp::endpoint ep1(ip::address::from_string("192.162.2.101"), port);  
    ip::udp::endpoint ep2(ip::address::from_string("192.168.2.102"), port);
    sock_mu.receive_from(boost::asio::buffer(buf_m2, 16384), ep1);
    sock_s1.receive_from(boost::asio::buffer(buf_m2, 16384), ep2);
    

    Port and IP addresses of devices are correct. When trying to connect to the right IP address, the devices don't get the packages at all. I mean, bind to any (0.0.0.0) address works, but I get the same packages. A connect (192.168.2.101) e.g., no. I hope you understand. What am I doing wrong?



  • Here:

    sock_mu.bind(ep_m);
    sock_mu.bind(ep_s1);
    

    Twice in a row, the bin of the same straw.

    receive_from Second argument accepted endpointto be placed at the address/port of the consignor of the detagram received. That is, their installation: ip::udp::endpoint ep1(ip::address::from_string("192.162.2.101"), port); Doesn't make sense.

    Also receive_from blocks performance until it gets the detagram. Maybe you need asynchronous functions instead of her (depending on your application logic).

    If the idea was to create two sockets, squeeze them by one port, and in each case, to get the detagrams from a certain client, it would be necessary:

    • cause the method of skate http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/basic_datagram_socket/connect/overload1.html transmitted as a parameter the final point of the remote device
    • Call the method. http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/basic_datagram_socket/receive/overload1.html Soketa (not receive_from) to get information.

    In your situation, it might be better to use one socket to take the detagrams with help. receive_from From all the customers and the result is to determine who sent the detagram.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2