title: Linux to WinXP over UDP lag
body: <p>I'm having a problem with a UDPSocket wrapper I've written.  I have a high bandwidth low latency local network over which I'm sending UDP packets back and forth.  I don't have a concern too much for reliability of packet arrival, but it is incredibly important that the packets that do arrive do so quickly.  Here's the relevant code for setting up a socket:</p>
  
  <pre><code>bool UDPSocket::create() {
      int on = 1;
  #ifdef WIN32
      if(WSAStartup(MAKEWORD(1,1), &amp;SocketInfo) != 0) {
          MessageBox(NULL, "Cannot initialize WinSock", "WSAStartup", MB_OK);
      }
  #endif
      m_sock = socket(PF_INET, SOCK_DGRAM, 0);
  #ifdef WIN32
      if(setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&amp;on, sizeof(on)) == SOCKET_ERROR)
          return false;
  #else
      if(setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&amp;on, sizeof(on)) == -1)
          return false;
  #endif
      addrLen = sizeof(struct sockaddr);
      return true;
  }
  
  bool UDPSocket::bind(const int port) {
      if(!is_valid())
          return false;
      m_addr.sin_family = AF_INET;
      m_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      m_addr.sin_port = htons(port);
  
      if(::bind(m_sock, (struct sockaddr*)&amp;m_addr, sizeof(m_addr))&lt;0) {
          std::cout &lt;&lt; "UDPSocket: error on bind" &lt;&lt; std::endl;
          return false;
      }
      return true;
  }
  bool UDPSocket::send(const std::string s) const {
      const char* buf = s.c_str();
  
      ::sendto(m_sock, buf, strlen(buf), 0, (const sockaddr*)&amp;clientAddr, addrLen);
      return true;
  }
  
  bool UDPSocket::setDestination(const std::string ip, const int port) {
      memset(&amp;clientAddr, 0, sizeof(clientAddr));
  
      clientAddr.sin_family = AF_INET;
      clientAddr.sin_addr.s_addr = inet_addr(ip.c_str());
      clientAddr.sin_port = htons(port);
  
      return true;
  }
  
  int UDPSocket::recv(std::string&amp; s) const {
      char buffer[MAXRECV + 1];
      struct timeval tv;
      fd_set fdset;
      int rc, nread;
  
      memset(&amp;buffer, 0, sizeof(buffer));
  
      FD_ZERO(&amp;fdset);
      FD_SET(m_sock, &amp;fdset);
  
      tv.tv_sec = 0;
      tv.tv_usec = m_timeout;
  
      rc = select(m_sock + 1, &amp;fdset, (fd_set *) 0, (fd_set *) 0, &amp;tv);
  
      if(FD_ISSET(m_sock, &amp;fdset)) {
  #ifdef WIN32
          nread = ::recvfrom(m_sock, buffer, MAXRECV, 0, (sockaddr*)&amp;clientAddr, const_cast&lt; int * __w64 &gt;(&amp;addrLen));
  #else
          nread = ::recvfrom(m_sock, buffer, MAXRECV, 0, (sockaddr*)&amp;clientAddr, (socklen_t*)&amp;addrLen);
  #endif
          if(nread &lt; 0) {
              return -1;
          } else if(nread == 0) {
              return 0;
          }
          s = std::string(buffer);
          return nread;
      } else {
          return 0;
      }
  }
  void UDPSocket::set_non_blocking(const bool b) {
      mNonBlocking = b;
  #ifdef WIN32
      u_long argp = b ? 1 : 0;
      ioctlsocket(m_sock, FIONBIO, &amp;argp);
  #else
      int opts = fcntl(m_sock, F_GETFL);
      if(opts &lt; 0) return;
      if(b)
          opts |= O_NONBLOCK;
      else
          opts &amp;= ~O_NONBLOCK;
  
      fcntl(m_sock, F_SETFL, opts);
  #endif
  }
  </code></pre>
  
  <p>My user code, on both ends, create a "sending" and "receiving" UDPSocket and bind them to their respective ports, then use send() to send data and recv() to receive.  On one hand, the linux side seems to receive practically immediately, but the Windows side has a delay up to 1 second before receiving data.  However, ::recv() never returns 0 in this time.  Am I missing something obvious?</p>