title: IPv6 Server in c - recvfrom failed
body: <p>i have written a small c-program for an IPv6 server with winsock2.h When I run the program in Visual Studio, I get the following message all the time: recvfrom failed</p>
  
  <p>I just can't find the error in the recvfrom function. Maybe someone can see why my program does not work at this point, thanks!  :-)</p>
  
  <p>Best regards,
  Ken</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  int main(int argc, char* argv[]) {
  
      SOCKET server_socket;
      struct sockaddr_in6 server_addr, client_addr;
      socklen_t client_len;
      char buf[1024];
      char clientIP[256];
  
      WSADATA data;
      WORD version = MAKEWORD(2, 2);
      if (WSAStartup(version, &amp;data) == SOCKET_ERROR) {
  
          printf("WSASStartup failed\n");
          WSACleanup();
          exit(EXIT_FAILURE);
      }
  
      server_socket = socket(AF_INET6, SOCK_DGRAM, 0);
      if (server_socket == -1) {
  
          printf("creating socket failed\n");
          WSACleanup();
          exit(EXIT_FAILURE);
      }
      else {
  
          printf("creating socket successful\n");
      }
  
      memset(&amp;server_addr, 0, sizeof(server_addr));
      server_addr.sin6_addr = in6addr_any;
      server_addr.sin6_family = AF_INET6;
      server_addr.sin6_port = htons(5001);
  
      if (bind(server_socket, (const struct sockaddr*) &amp; server_addr,
          sizeof(server_addr)) == -1) {
  
          printf("bind socket failed\n");
          WSACleanup();
          exit(EXIT_FAILURE);
      }
      else {
  
          printf("bind socket successful\n");
      }
  
      while (1) {
  
          memset(&amp;client_addr, 0, sizeof(client_addr));
          memset(buf, 0, sizeof(buf));
  
          if (recvfrom(server_socket, (char*)buf, 1024, 0,
              (struct sockaddr*) &amp; client_addr,
              sizeof(client_addr)) == -1) {
  
              printf("recvfrom failed\n");
              WSACleanup();
              exit(EXIT_FAILURE);
          }
          else {
  
              printf("recvfrom successful");
          }
  
          printf("%s\n", buf);
          printf("IP: %s\n", inet_ntop(AF_INET6, &amp;client_addr.sin6_addr,
              clientIP, 256));
      }
  
      closesocket(server_socket);
  
      WSACleanup();
  
      return 0;
  }
  </code></pre>