title: UDP receiver in C (using Winsock)
body: <p>I'm trying to learn socket programming in C using winsock libraries. I managed to knock up the following simple example:</p>
  
  <pre><code>#include&lt;stdio.h&gt; 
  #include&lt;string.h&gt; 
  #include&lt;stdlib.h&gt;
  #include&lt;WinSock2.h&gt;
  #include&lt;conio.h&gt;
  
  #pragma comment(lib, "ws2_32.lib")
  
  #define BUFFERSZ 1024
  #define PORT 19010  
  
  typedef enum
  {
      BLOCKING = 0,
      NONBLOCKING = 1
  } BLOCKING_MODE;
  
  int main(void)
  {
      struct sockaddr_in UserApp;
      struct sockaddr_in Server;
  
      int ServerLen = sizeof(Server);
      int ReceivedDataLen;
      UINT8 ReceiveBuffer[BUFFERSZ];
  
      WORD wVersionRequested = MAKEWORD(2, 2);
      WSADATA wsaData;
      SOCKET ListenSocket;
      UINT32 Mode = (UINT32)NONBLOCKING;
      UINT32 bufferdata;
  
      int loop;
  
      if (WSAStartup(wVersionRequested, &amp;wsaData) == 0)
      {
          /* Create UDP socket */
          if ((ListenSocket = WSASocketW(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED)) != INVALID_SOCKET)
          {
              int SocketOption = 1;
              setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&amp;SocketOption, sizeof(SocketOption));
  
              UserApp.sin_family = AF_INET;
              UserApp.sin_port = htons(PORT);
              UserApp.sin_addr.s_addr = htonl(INADDR_ANY);
  
              // Bind socket
              if (bind(ListenSocket, (PSOCKADDR)&amp;UserApp, sizeof(UserApp)) != SOCKET_ERROR)
              {
                  /* Using ioctlsocket() */
                  if (ioctlsocket(ListenSocket, FIONBIO, &amp;Mode) != SOCKET_ERROR)
                  {
                      ioctlsocket(ListenSocket, FIONREAD, &amp;bufferdata);
  
                      /* Read data from buffer */
                      if (bufferdata != 0)
                      {
                          ReceivedDataLen = recvfrom(ListenSocket, ReceiveBuffer, BUFFERSZ, 0, (struct sockaddr *)&amp;Server, &amp;ServerLen);
                      }
  
                      /* Display received data */
                      printf("\n%d :: \n", __LINE__);
                      for (loop = 0; loop &lt; ReceivedDataLen; loop++)
                      {
                          printf("%02X ", ReceiveBuffer[loop]);
                      }
                      printf("\n");
                  }
  
                  /* Using select() */
                  FD_SET ReadSet;
                  FD_ZERO(&amp;ReadSet);
  
                  FD_SET(ListenSocket, &amp;ReadSet);
  
                  struct timeval TimeOut;
                  TimeOut.tv_usec = 100000; /* 0.1sec timeout */
                  bufferdata = 0;
                  bufferdata = select(0, &amp;ReadSet, NULL, NULL, &amp;TimeOut);
  
                  if (bufferdata != 0)
                  {
                      ReceivedDataLen = recvfrom(ListenSocket, ReceiveBuffer, BUFFERSZ, 0, (struct sockaddr *) &amp;Server, &amp;ServerLen);
                  }
  
                  /* Display received data */
                  printf("\n%d :: \n", __LINE__);
                  for (loop = 0; loop &lt; ReceivedDataLen; loop++)
                  {
                      printf("%02X ", ReceiveBuffer[loop]);
                  }
                  printf("\n");
  
                  closesocket(ListenSocket);
              }
          }
      }   
  
      WSACleanup();
  
      _getch();
      return 0;
  }
  </code></pre>
  
  <p>It does compile under Visual Studio 2017.</p>
  
  <p>I have an application transmitting UDP on localhost:19010. When I run it using visual studio it does not seem to receive any messages sent by the application.</p>
  
  <p>However, when I single-step through the code, it seems to receive and process (display) the UDP packets.</p>
  
  <p>Not quite sure what I'm doing wrong.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Cheers!</p>