title: Bind UDP socket to specific network interface using Boost.Asio
body: <p>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.</p>
  
  <p>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 (192.168.1.1 in this example) I do not get any data. But if I use the ANY "0.0.0.0" 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! </p>
  
  <p>Is it normal behavior ? Or do I need to read the <code>sender_endpoint</code> to know that information on Linux and filter afterwards?</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using boost::asio::ip::udp;
  
  int main(int argc, char* argv[])
  {
    try
    {
      boost::asio::io_context io_context;
  
      // Setup UDP Socket
      udp::socket socket(io_context);
      socket.open(udp::v4());
  
      // Bind to specific network card and chosen port
      socket.bind(udp::endpoint(boost::asio::ip::address::from_string("192.168.1.1"), 2368));
  
      // Prepare to receive data
      boost::array&lt;char, 128&gt; 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(recv_buf.data(), len);
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
  
    return 0;
  }
  </code></pre>