title: Open 2 distinct sockets to 1 common remote endpoint using Boost::asio
body: <p>I am trying to test sending data from 2 distinct network adapters on the same machine to a common remote endpoint, but I keep getting &quot;bind: invalid argument&quot; AFTER the first bind comes through. What am I missing? I have searched, tried to modify the code, but I was not able to find any lead and I keep getting the same error. The same happens when I swap out the IPs.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;sstream&gt;
  #include &lt;thread&gt;
  #include &lt;chrono&gt;
  #include &lt;boost/random.hpp&gt;
  
  const unsigned int MS_INTERVAL = 100;
  enum CMD_ARG
  {
      PROG_NAME = 0,
      LOCAL_IP_1,
      LOCAL_IP_2,
      REMOTE_IP,
      REMOTE_PORT
  };
  
  using namespace boost::asio;
  using std::string;
  using std::cout;
  using std::endl;
  
  
  
  int main(int argc, char *argv[]) {
      if(argc == 5)
      {
          //Test data initialisation
          unsigned int counter = 0;
          boost::random::mt19937 randSeed;         // seed, produces randomness out of thin air
          boost::random::uniform_int_distribution&lt;&gt; randGen(-1000,1000); // Random number generator between -100 and 100
  
          //Initialise ASIO service
          io_service io_service;
  
          //socket creation and binding (one per network adapter)
          std::cout &lt;&lt; &quot;Opening and binding local sockets to &quot; &lt;&lt; argv[LOCAL_IP_1] &lt;&lt; &quot; and &quot; &lt;&lt; argv[LOCAL_IP_2] &lt;&lt; std::endl;
  
          ip::tcp::socket socket1(io_service);
          ip::tcp::socket socket2(io_service);
          socket1.open(ip::tcp::v4());
          socket2.open(ip::tcp::v4());
  
          socket1.bind(ip::tcp::endpoint(ip::address::from_string(argv[LOCAL_IP_1]), 0));
  
          std::cout &lt;&lt; &quot;1/2 done&quot; &lt;&lt; std::endl;
  
          socket2.bind(ip::tcp::endpoint(ip::address::from_string(argv[LOCAL_IP_2]), 0));
  
          //Connection to remote end point starting with defining the remote endpoint
          std::istringstream iss(argv[REMOTE_PORT]);
          unsigned int port = 0;
          iss &gt;&gt; port;
          ip::tcp::endpoint remoteEndpoint = ip::tcp::endpoint( ip::address::from_string(argv[REMOTE_IP]), port);
  
          std::cout &lt;&lt; &quot;Connecting to &quot; &lt;&lt; argv[REMOTE_IP] &lt;&lt; &quot; on port &quot; &lt;&lt; port &lt;&lt; std::endl;
  
          socket1.connect(remoteEndpoint);
  
          std::cout &lt;&lt; &quot;1/2 done&quot; &lt;&lt; std::endl;
  
          socket2.connect(remoteEndpoint);
  
          std::cout &lt;&lt; &quot;Ready&quot; &lt;&lt; std::endl;
  
          while(1)
          {
              //Build message
              std::ostringstream oss;
              oss &lt;&lt; counter &lt;&lt; &quot;,&quot; &lt;&lt; randGen(randSeed) &lt;&lt; &quot;,&quot; &lt;&lt; randGen(randSeed) &lt;&lt; &quot;,&quot; &lt;&lt; randGen(randSeed) &lt;&lt; std::endl;
  
              //Send message on both interfaces
              boost::system::error_code error1, error2;
              write(socket1, boost::asio::buffer(oss.str()), error1);
              write(socket2, boost::asio::buffer(oss.str()), error2);
          //Check errors
          if( !error1 &amp;&amp; !error2) {
              cout &lt;&lt; &quot;Sending: &quot; &lt;&lt; oss.str() &lt;&lt; endl;
              counter++;
          }
          else {
              cout &lt;&lt; &quot;Error: &quot; &lt;&lt; (error1?error1.message():error2.message()) &lt;&lt; endl;
          }
  
          std::this_thread::sleep_for(std::chrono::milliseconds(MS_INTERVAL));
      }
  }
  else
  {
      std::cout &lt;&lt; &quot;Usage: &lt;program&gt; &lt;local IP 1&gt; &lt;local IP 2&gt; &lt;remote server IP&gt; &lt;server's opened port&gt;&quot; &lt;&lt; argc &lt;&lt; std::endl;
  }
  
  return 0;
  </code></pre>
  <p>}</p>