title: UDP bind: Address already in use
body: <p>I have no knowledge in <code>node.js</code>.</p>
  
  <p>I am sending <code>UDP</code> packets in <code>C++</code> and I want to show them in <code>node.js</code>.</p>
  
  <p>The problem is that both try to occupy the IP/port. Depending on which one I run, the other one fails. How should I fix this problem?</p>
  
  <p>main.cpp</p>
  
  <pre><code>// g++ main.cpp -o main -std=c++11 -lboost_system -pthread
  
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using boost::asio::ip::udp;
  
  
  int main(int argc, char* argv[])
  {
  
      const int port=1414;
      const std::string ip="127.0.0.1";
  
      boost::asio::io_service io_service;
      udp::resolver resolver(io_service);
  
      udp::endpoint client_endpoint = *resolver.resolve({udp::v4(), ip, std::to_string(port)});
      udp::socket socket(io_service, udp::endpoint(udp::v4(), port));
      boost::asio::socket_base::reuse_address option(true);
      socket.set_option(option);
  
      std::string data;
      for(long i=0;true;i++)
      {
          data=std::to_string(i+1)+"th packet";
          socket.send_to(boost::asio::buffer(data.c_str(), data.length()+1), client_endpoint);
          usleep(10);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>app.js</p>
  
  <pre><code>var PORT = 1414;
  var HOST = '127.0.0.1';
  
  var dgram = require('dgram');
  var server = dgram.createSocket('udp4');
  
  server.on('listening', function () {
      var address = server.address();
      console.log('UDP Server listening on ' + address.address + ":" + address.port);
  });
  
  server.on('message', function (message, remote) {
      console.log(remote.address + ':' + remote.port +' - ' + message);
  
  });
  
  server.bind(PORT, HOST);
  </code></pre>
  
  <p>If I run the <code>C++</code> file and then the <code>js</code> one, the error is this:</p>
  
  <pre><code>events.js:160
        throw er; // Unhandled 'error' event
        ^
  
  Error: bind EADDRINUSE 127.0.0.1:1414
      at Object.exports._errnoException (util.js:1012:11)
      at exports._exceptionWithHostPort (util.js:1035:20)
      at dgram.js:221:18
      at _combinedTickCallback (internal/process/next_tick.js:77:11)
      at process._tickCallback (internal/process/next_tick.js:98:9)
      at Module.runMain (module.js:577:11)
      at run (bootstrap_node.js:352:7)
      at startup (bootstrap_node.js:144:9)
      at bootstrap_node.js:467:3
  </code></pre>
  
  <p>While, if I run the <code>js</code> file first and then the <code>C++</code> one, I receive:</p>
  
  <pre><code>terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;boost::system::system_error&gt; &gt;'
    what():  bind: Address already in use
  Aborted (core dumped)
  </code></pre>