title: Simple C udp client/server fails
body: <p>I am trying to write a server program that just displays any raw data it receives from anyone through udp. A client can send some data to it at any time. No errors appear except for &quot;Socket error: Resource temporarily unavailable&quot; which in other words mean that <code>recvfrom</code> has no data to receive (because it's presumably not getting any packets).</p>
  <p>The client just sends &quot;test!&quot; to 127.0.0.0:12345, while the server listens with a non-blocking socket on port 12345. It may be worth noting that I am on linux.</p>
  <p>Why is <code>server</code> not displaying the packets that it's obviously being sent from <code>client</code>? I don't see what I'm missing.</p>
  <h1>common.h</h1>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;fcntl.h&gt;
  
  typedef struct sockaddr_in sockaddr_in;
  typedef struct sockaddr sockaddr;
  
  int createSocket(uint16_t port)
  {
      int handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  
      if (handle &lt;= 0)
      {
          printf(&quot;failed to create socket\n&quot;);
          return 0;
      }
  
      sockaddr_in address;
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY;
      address.sin_port = htons(port);
  
      if (bind(handle, (const sockaddr*)&amp;address, sizeof(sockaddr_in)) &lt; 0)
      {
          printf(&quot;failed to bind socket\n&quot;);
          return 0;
      }
  
      int nonBlocking = 1;
      if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1)
      {
          printf(&quot;failed to set non-blocking\n&quot;);
          return 0;
      }
  
      return handle;
  }
  
  int sendData(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint16_t port, int handle, char* packet_data, int packet_size)
  {
      uint32_t address = (a &lt;&lt; 24) | (b &lt;&lt; 16) | (c &lt;&lt; 8) | d;
  
      sockaddr_in addr;
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = htonl(address);
      addr.sin_port = htons(port);
  
      int sent_bytes = sendto(handle, (const char*)packet_data, packet_size, 0, (sockaddr*)&amp;address, sizeof(sockaddr_in));
  
      if (sent_bytes != packet_size)
      {
          printf(&quot;failed to send packet (%d/%d)\n&quot;, sent_bytes, packet_size);
          return 0;
      }
  }
  
  </code></pre>
  <h1>server.c</h1>
  <pre><code>#include &quot;common.h&quot;
  #include &lt;time.h&gt;
  
  void main(void){
      int socket = createSocket(12345);
      if (!socket){
          perror(&quot;Socket error&quot;);
      }
      int true=1;
      setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (const void*)&amp;true, 1);
      // so that we can immediately reuse the port if the server stops running
  
      char buffer[100] = &quot;No data&quot;;
      if(!listen(socket, 0)){
          perror(&quot;Socket error&quot;);
      }
  
      //struct timespec delay = {0,33333333}; // 30Hz
      struct timespec delay = {0,100000000}; // 100Hz
  
      int running = 1;
      int ret = 0;
      while(running){
          ret = recvfrom(socket, &amp;buffer, 100, 0, NULL, NULL);
          if (ret &lt; 0){
              perror(&quot;Socket error&quot;);
          } else if (ret &gt; 0){
              printf(&quot;%d: %s\n&quot;, ret, buffer);
          }
  
          nanosleep(&amp;delay,NULL); 
      }
  }
  </code></pre>
  <h1>client.c</h1>
  <pre><code>#include &quot;common.h&quot;
  
  void main(void){
      int socket = createSocket(0);
      printf(&quot;Started client on port %d\n&quot;, socket);
  
      if(!sendData(127,0,0,0,12345, socket, &quot;test!&quot;, 5)){
          perror(&quot;Socket error&quot;);
      } else {
          printf(&quot;Sent data.\n&quot;);
      }
  }
  </code></pre>