title: Do I have to bind a UDP socket in my client program to receive data? (I always get WSAEINVAL)
body: <p>I am creating a UDP socket (<code>AF_INET</code>, <code>SOCK_DGRAM</code>, <code>IPPROTO_UDP</code>) via Winsock and trying to <code>recvfrom</code> on this socket, but it always returns -1 and I get WSAEINVAL (10022). Why?</p>
  
  <p>When I <code>bind()</code> the port, that does not happen, but I have read that it is very lame to bind the client's socket.</p>
  
  <p>I am sending data to my server, which answers, or at least, tries to.</p>
  
  <pre><code>Inc::STATS CConnection::_RecvData(sockaddr* addr, std::string &amp;strData)
  {
      int ret;            // return code
      int len;            // length of the data
      int fromlen;        // sizeof(sockaddr)
      char *buffer;       // will hold the data
      char c;
  
      //recv length of the message
      fromlen = sizeof(sockaddr);
      ret = recvfrom(m_InSock, &amp;c, 1, 0, addr, &amp;fromlen);
      if(ret != 1)
      {
  #ifdef __MYDEBUG__
          std::stringstream ss;
          ss &lt;&lt; WSAGetLastError();
          MessageBox(NULL, ss.str().c_str(), "", MB_ICONERROR | MB_OK);
  #endif
          return Inc::ERECV;
      }
      ...
  </code></pre>
  
  <p>This is a working example I wrote a few moments ago, and it works without the call to <code>bind()</code> in the client:</p>
  
  <pre><code>#pragma comment(lib, "Ws2_32.lib")
  
  #define WIN32_LEAN_AND_MEAN
  
  #include &lt;WS2tcpip.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      SOCKET sock;
      addrinfo* pAddr;
      addrinfo hints;
      sockaddr sAddr;
      int fromlen;
      const char czPort[] = "12345";
      const char czAddy[] = "some ip";
  
      WSADATA wsa;
      unsigned short usWSAVersion = MAKEWORD(2,2);
  
      char Buffer[22] = "TESTTESTTESTTESTTEST5";
      int ret;
  
      //Start WSA
      WSAStartup(usWSAVersion, &amp;wsa);
  
      //Create Socket
      sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  
      //Resolve host address
      memset(&amp;hints, 0, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_protocol = IPPROTO_UDP;
      hints.ai_socktype = SOCK_DGRAM;
  
      if(getaddrinfo(czAddy, czPort, &amp;hints, &amp;pAddr))
      {
          std::cerr &lt;&lt; "Could not resolve address...\n";
          std::cin.get();
          return 1;
      }
  
      //Start Transmission
      while(1)
      {
          ret = sendto(sock, Buffer, sizeof(Buffer), 0, pAddr-&gt;ai_addr,
                       pAddr-&gt;ai_addrlen);
          if(ret != sizeof(Buffer))
          {
              std::cerr &lt;&lt; "Could not send data\n";
              std::cin.get();
              return 1;
          }
  
          fromlen = sizeof(SOCKADDR);
          ret = recvfrom(sock, Buffer, sizeof(Buffer), 0, &amp;sAddr, &amp;fromlen);
          if(ret != sizeof(Buffer))
          {
              std::cout &lt;&lt; "Could not receive data -  error: " &lt;&lt; 
                            WSAGetLastError() &lt;&lt; std::endl;
              std::cin.get();
              return 1;
          }
  
          Buffer[ret-1] = '\0';
          std::cout &lt;&lt; "Received: " &lt;&lt; Buffer &lt;&lt; std::endl;
      }
      return 0;
  }
  </code></pre>