Hello everyone, it became necessary to accept packets from 4 devices using the UDP protocol. I use for this boost :: asio. The problem is this: when broadcasting, broadcast is used, when connecting to devices and receiving, I get the same (!) Data packets from everyone, although through wireshark it can be seen that different ones are sent. Here is an example of how I try to accept data:

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); 

The port and IP addresses of the devices are correct. When I try to connect to the correct IP address of the device, I don’t receive any packets at all. Those. bind to any (0.0.0.0) address works, but I get the same packages. And connect (192.168.2.101) for example - no. I hope explained clearly) What am I doing wrong?

    2 answers 2

    Here:

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

    Twice in a row there is a bandage of the same socket.

    receive_from second argument takes an endpoint , which will be set to the address / port of the sender of the received datagram. Those. their installation: ip::udp::endpoint ep1(ip::address::from_string("192.162.2.101"), port); does not make sense.

    Also receive_from blocks execution until it receives a datagram. Perhaps you need asynchronous functions instead (depending on the logic of your application).

    If the idea was to create two sockets, bind them to one port, and in each receive datagrams only from a specific client, then you need:

    • call the connect socket method passing the endpoint of the remote device as a parameter
    • call the socket's receive method (do not receive_from) to get information.

    In your situation, it may be better to use one socket, which will receive datagrams using receive_from from all clients, and determine who sent the datagram by the result.

    • It is a typographical error, in the program a bandage on different sockets. - Ivan
    • And the receive_from method for each socket is spun in a separate thread (using boost :: thread) - Ivan
    • When calling the connect method and the receive method - the data does not come, as I did initially - Ivan
    • However, you understood the idea correctly, only devices 4, and it is impossible to understand from which packages the data came from the contents of the packets. - Ivan
    • Remark: I get the packages different. But on the socket for which the receive_from method is called with an endpoint with the address 192.168.2.101, packets come from the address 192.168.2.102. I either connect incorrectly or accept the data incorrectly. Help please) - Ivan

    The issue is decided. First, the bind method of the socket is called, the parameter is the endpoint with any address (0.0.0.0). Then - the connect method of the same socket, the parameter - the end point with the desired address. It seems to work. Thanks for answers!

    • No, I did not decide ( - Ivan