Bind socket to specific network interface using Boost.Asio


6 days ago


10 time


My PC has several network cards and I'm trying to receive UDP data from several broadcast devices. Each device is isolated on a dedicated network and I'm trying to read UDP data from multiple devices at the same time. I'm using Boost version 1.67. Let's pretend in this post that I want to get data from one only specific device, so I want to bind on a local network interface.

On Windows the following code works, but on my Ubuntu 16.04 64bits machine it does not. Indeed, if I bind on one specific local IP address ( in this example) I do not get any data. But if I use the ANY "" address then I get what I want. Except that in that case I don't know where it comes from. It could be received by any network card!

Is it normal behavior ? Or do I need to read the sender_endpoint to know that information on Linux and filter afterwards?

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

int main(int argc, char* argv[])
    boost::asio::io_context io_context;

    // Setup UDP Socket
    udp::socket socket(io_context);;

    // Bind to specific network card and chosen port
    socket.bind(udp::endpoint(boost::asio::ip::address::from_string(""), 2368));

    // Prepare to receive data
    boost::array<char, 128> recv_buf;
    udp::endpoint sender_endpoint;
    size_t len = socket.receive_from(boost::asio::buffer(recv_buf), sender_endpoint);

    // Write data to std output
    std::cout.write(, len);
  catch (std::exception& e)
    std::cerr << e.what() << std::endl;

  return 0;

0 answers