title: How do I receive udp packets with winsock in c++?
body: <p>As an attempt at wrapping my head around udp sockets I've tried to port the code from this tutorial page <a href="http://www.linuxhowtos.org/C_C++/socket.htm" rel="nofollow noreferrer">http://www.linuxhowtos.org/C_C++/socket.htm</a> to winsock (running on win 8 if it matters). [direct links below]</p>
  
  <p>The code currently compiles and runs but I never receive a packet, both programs just wait and block on recvfrom().  I keep having this same issue (for example with a modified version of this code <a href="https://stackoverflow.com/a/679529/873217">https://stackoverflow.com/a/679529/873217</a> that includes a recvfrom() call. And with this code <a href="https://stackoverflow.com/questions/6538707/c-problem-with-datagram-udpwinsocket-to-sendto-and-recvfrom-on-the-same-sock">C++ problem with Datagram (UDP)winsocket to sendto and recvfrom on the same socket through the loopback adapter</a> making the changes suggested).  I imagine I'm making some simple and fundamental mistake; but I'm unable to locate it on my own.  Hopefully someone with more experience can shine some light on this issue for me. Thanks. </p>
  
  <p>Additional notes:<br>
  I am running the server exe and then the client exe on the same computer. It is connected to the internet.  And I have tried using a domain that resolves to my IP address as well.  I have also tried disabling the firewall, though I have permitted both programs full access.</p>
  
  <p>direct links to original client and server code:<br>
  <a href="http://www.linuxhowtos.org/data/6/client_udp.c" rel="nofollow noreferrer">http://www.linuxhowtos.org/data/6/client_udp.c</a><br>
  <a href="http://www.linuxhowtos.org/data/6/server_udp.c" rel="nofollow noreferrer">http://www.linuxhowtos.org/data/6/server_udp.c</a></p>
  
  <p>My current attempt:</p>
  
  <p>both linked to libws2_32.a </p>
  
  <p>client code:  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;string.h&gt;
  
  void error(const char *);
  
  int main()
  {
      WSAData data;
      WSAStartup( MAKEWORD( 2, 2 ), &amp;data );
  
      int sock, n;
      int length;
      struct sockaddr_in server, from;
      struct hostent *hp;
      char buffer[256];
      unsigned short serverPort = 27072;
      sock = socket(AF_INET, SOCK_DGRAM, 0);
      if (sock &lt; 0) error("socket");
      server.sin_family = AF_INET;
      hp = gethostbyname("localhost");//have also tried my url
      if (hp==0) error("Unknown host");
      memmove((char *)hp-&gt;h_addr, (char *)&amp;server.sin_addr, hp-&gt;h_length);
      server.sin_port = htons(serverPort);
      length = sizeof(struct sockaddr_in);
      printf("Please enter the message: ");
      memset(buffer, 0, 256);
      fgets(buffer, 255, stdin);
      n = sendto (sock, buffer, strlen(buffer), 0, (const struct sockaddr *)&amp;server, length);
      if (n &lt; 0) error("Sendto");
      printf("sent %i bytes\n", n);
      n = recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&amp;server, &amp;length);
      if (n &lt; 0) error("recvfrom");
      //write(1,"Got an ack: ", 12);
      //write(1, buffer, n);
      printf("Got msg: %s\n",buffer);
  
      closesocket(sock);
      WSACleanup();
      return(0);
  }
  
  void error(const char *msg)
  {
      perror(msg);
      exit(0);
  }
  </code></pre>
  
  <p>server code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;string.h&gt;
  
  void error(const char *);
  
  int main()
  {
      WSAData data;
      WSAStartup( MAKEWORD( 2, 2 ), &amp;data );
  
      int sock, length, n;
      int fromlen;
      struct sockaddr_in server;
      struct sockaddr_in from;
      char buf[1024];
      unsigned short serverPort = 27072;
      sock=socket(AF_INET, SOCK_DGRAM, 0);
      if (sock &lt; 0) error("Opening socket");
      length = sizeof(server);
      memset(&amp;server, 0, length);
      server.sin_family = AF_INET;
      server.sin_addr.s_addr=INADDR_ANY;
      server.sin_port=htons(serverPort);
      if (bind(sock,(struct sockaddr *)&amp;server, length)&lt;0) error ("binding");
      fromlen = sizeof(struct sockaddr_in);
      while(1)
      {
          n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&amp;from, &amp;fromlen);
          if (n&lt;0) error("recvfrom");
          //write(1,"Received a datagram: ", 21);
          //write(1,buf,n);
          printf("Received a datagram: %s", buf);
          n = sendto(sock, "Got your message\n",17,0,(struct sockaddr *)&amp;from,fromlen);
          if (n&lt;0)error("sendto");
      }
      closesocket(sock);
      WSACleanup();
      return 0;
  }
  
  
  void error (const char *msg)
  {
      perror(msg);
      exit(0);
  }
  </code></pre>