title: Long delays in sending UDP packets
body: <p>I have an application that receives, processes, and transmits UDP packets.</p>
  
  <p>Everything works fine if the port numbers for reception and transmission are different.</p>
  
  <p>If the port numbers are the same and the IP addresses are different it usually works fine EXCEPT when the IP address are on the same subnet as the machine running the application.  In that last case the send_to function requires several seconds to complete, instead of a few milliseconds as is usual.</p>
  
  <pre><code>Rx Port  Tx IP          Tx Port    Result
  
  5001     Same           5002       OK  Delay ~ 0.001 secs
           subnet     
  
  5001     Different      5001       OK  Delay ~ 0.001 secs
           subnet
  
  5001     Same           5001       Fails  Delay &gt; 2 secs
           subnet
  </code></pre>
  
  <p>Here is a short program that demonstrates the problem.</p>
  
  <pre><code>#include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using boost::asio::ip::udp;
  using std::cout;
  using std::endl;
  
  int test( const std::string&amp; output_IP)
  {
      try
      {
          unsigned short prev_seq_no;
  
          boost::asio::io_service io_service;
  
          // build the input socket
  
          /* This is connected to a UDP client that is running continuously
          sending messages that include an incrementing sequence number
          */
  
          const int input_port = 5001;
          udp::socket input_socket(io_service, udp::endpoint(udp::v4(), input_port ));
  
          // build the output socket
  
          const std::string output_Port = "5001";
          udp::resolver resolver(io_service);
          udp::resolver::query query(udp::v4(), output_IP, output_Port );
          udp::endpoint output_endpoint = *resolver.resolve(query);
          udp::socket output_socket( io_service );
          output_socket.open(udp::v4());
  
         // double output buffer size
         boost::asio::socket_base::send_buffer_size option( 8192 * 2 );
         output_socket.set_option(option);
  
          cout  &lt;&lt; "TX to " &lt;&lt; output_endpoint.address() &lt;&lt; ":"  &lt;&lt; output_endpoint.port() &lt;&lt; endl;
  
  
  
          int count = 0;
          for (;;)
          {
              // receive packet
              unsigned short recv_buf[ 20000 ];
              udp::endpoint remote_endpoint;
              boost::system::error_code error;
              int bytes_received = input_socket.receive_from(boost::asio::buffer(recv_buf,20000),
                                   remote_endpoint, 0, error);
  
              if (error &amp;&amp; error != boost::asio::error::message_size)
                  throw boost::system::system_error(error);
  
              // start timer
              __int64 TimeStart;
              QueryPerformanceCounter( (LARGE_INTEGER *)&amp;TimeStart );
  
              // send onwards
              boost::system::error_code ignored_error;
              output_socket.send_to(
                  boost::asio::buffer(recv_buf,bytes_received),
                  output_endpoint, 0, ignored_error);
  
              // stop time and display tx time
              __int64 TimeEnd;
              QueryPerformanceCounter( (LARGE_INTEGER *)&amp;TimeEnd );
              __int64 f;
              QueryPerformanceFrequency( (LARGE_INTEGER *)&amp;f );
              cout &lt;&lt; "Send time secs " &lt;&lt; (double) ( TimeEnd - TimeStart ) / (double) f &lt;&lt; endl;
  
              // stop after loops
              if( count++ &gt; 10 )
                  break;
          }
      }
      catch (std::exception&amp; e)
      {
          std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
      }
  
  }
  int main(  )
  {
  
      test( "193.168.1.200" );
  
      test( "192.168.1.200" );
  
      return 0;
  }
  </code></pre>
  
  <p>The output from this program, when running on a machine with address 192.168.1.101</p>
  
  <pre><code>TX to 193.168.1.200:5001
  Send time secs 0.0232749
  Send time secs 0.00541566
  Send time secs 0.00924535
  Send time secs 0.00449014
  Send time secs 0.00616714
  Send time secs 0.0199299
  Send time secs 0.00746081
  Send time secs 0.000157972
  Send time secs 0.000246775
  Send time secs 0.00775578
  Send time secs 0.00477618
  Send time secs 0.0187321
  TX to 192.168.1.200:5001
  Send time secs 1.39485
  Send time secs 3.00026
  Send time secs 3.00104
  Send time secs 0.00025927
  Send time secs 3.00163
  Send time secs 2.99895
  Send time secs 6.64908e-005
  Send time secs 2.99864
  Send time secs 2.98798
  Send time secs 3.00001
  Send time secs 3.00124
  Send time secs 9.86207e-005
  </code></pre>
  
  <p>Why is this happening?  Is there any way I can reduce the delay?</p>
  
  <p>Notes:</p>
  
  <ul>
  <li><p>Built using code::blocks, running under various flavours of Windows</p></li>
  <li><p>Packet are 10000 bytes long</p></li>
  <li><p>The problem goes away if I connect the computer running the application to a second network.  For example a WWLAN ( cellular network "rocket stick" )</p></li>
  </ul>
  
  <p>As far as I can tell, this is the situation we have:</p>
  
  <p>This works ( different ports, same LAN ):</p>
  
  <p><a href="https://i.stack.imgur.com/vdS2P.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vdS2P.jpg" alt="enter image description here"></a></p>
  
  <p>This also works ( same ports, different LANS ):</p>
  
  <p><a href="https://i.stack.imgur.com/C1GIz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C1GIz.jpg" alt="enter image description here"></a></p>
  
  <p><strong>This does NOT work ( same ports, same LAN )</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/ZddeU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZddeU.jpg" alt="enter image description here"></a></p>
  
  <p>This seems to work ( same ports, same LAN, dual homed Module2 host )</p>
  
  <p><a href="https://i.stack.imgur.com/JgTiI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JgTiI.jpg" alt="enter image description here"></a></p>