title: Linux UDP ephemeral port bind occasionally fails to receive
body: <p>I am writing a set of tests that depend on two applications communicating (locally) with a UDP socket. These UDP sockets are originally bound using port 0, then the actual bound port is queried using getsockname and shared between the two applications.  Occasionally, the subsequent recv calls fail to return any data without any error reporting during binding etc.</p>
  <p>I've simplified my implementation to a bare-bones test an included it below.  It fails with &quot;Received failed, got -1 expeccted 6&quot; about 1 of 10 runs. What am I missing in the socket setup to reliable use an ephemeral, OS-assigned port?</p>
  <pre><code>#include &lt;arpa/inet.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;cstring&gt;
  #include &lt;thread&gt;
  
  int32_t createSocket(uint16_t&amp; rBoundPort)
  {
    int32_t s = socket(AF_INET, SOCK_DGRAM, 0);
  
    if (s &gt;= 0)
    {
      timeval timeout; 
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;
      
      if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &amp;timeout, sizeof(timeval)) &lt; 0)
      { 
        ::close(s);
        return -1;
      }
  
      int32_t r(1);
      if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &amp;r, sizeof(int)) &lt; 0)
      { 
        close(s);
        return -1;
      }
  
      int32_t bufSize(50000);
      if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &amp;bufSize, sizeof(bufSize)) &lt; 0)
      { 
        close(s);
        return -1;
      }
  
      // Setup local listening port
      sockaddr_in listenAddress;
      memset(reinterpret_cast&lt;char*&gt;(&amp;listenAddress), 0, sizeof(listenAddress));
      listenAddress.sin_family = AF_INET;
      inet_aton(&quot;127.0.0.1&quot;, &amp;listenAddress.sin_addr);
      listenAddress.sin_port = htons(0);
      
      if (bind(s, (struct sockaddr*)&amp;listenAddress,  sizeof(listenAddress)) != 0)
      { 
        close(s);
        return -1;
      }
  
      // Update the bound listen port
      socklen_t boundAddrLen = sizeof(sockaddr_in);
      getsockname(s, (struct sockaddr*)&amp;listenAddress, &amp;boundAddrLen);
      rBoundPort = ntohs(listenAddress.sin_port);
    }
  
    return s;
  }
  
  void mysockettest(int32_t s, uint16_t destPort)
  {
    const int32_t dataSize = 6;
    char aWrite[dataSize] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
    char aRx[dataSize];
  
    sockaddr_in dest;
    memset((char*)&amp;dest, 0, sizeof(dest));
    dest.sin_family = AF_INET;
    inet_aton(&quot;127.0.0.1&quot;, &amp;dest.sin_addr);
    dest.sin_port = htons(destPort);
  
    sendto(s, aWrite, dataSize, 0, (struct sockaddr*)&amp;dest, sizeof(sockaddr_in));
  
    sockaddr_in src;
    socklen_t srcSize = sizeof(sockaddr_in);
    memset((char*)&amp;src, 0, srcSize);
  
    int32_t bytesReceived = recvfrom(s, aRx, dataSize, 0, (struct sockaddr*)&amp;src, &amp;srcSize);
    if (bytesReceived != dataSize)
    {
      printf(&quot;Received failed, got %d expected %d\n&quot;, bytesReceived, dataSize);
    }
  }
  
  int main(int argc, char** pargv)
  {
    uint16_t s1Port(0);
    int32_t s1 = createSocket(s1Port);
    if (s1 &lt; 0)
    {
      printf(&quot;FAILED TO OPEN SOCKET 1\n&quot;);
      return -1;
    }
    if (s1Port &lt; 1)
    {
      printf(&quot;FAILED TO BIND SOCKET 1 TO PORT\n&quot;);
      return -1;
    }
  
    uint16_t s2Port(0);
    int32_t s2 = createSocket(s2Port);
    if (s2 &lt; 0)
    {
      printf(&quot;FAILED TOOPEN SOCKE 2\n&quot;);
    }
    if (s2Port &lt; 1)
    {
      printf(&quot;FAILED TO BIND SOCKET 2 TO PORT\n&quot;);
    }
  
    std::thread t1(mysockettest, s1, s2Port);
    std::thread t2(mysockettest, s2, s1Port);
  
    t1.join();
    t2.join();
  
    close(s1);
    close(s2);
  }
  ```
  </code></pre>