title: Unable to transmit fast via UDP broadcast on a wireless network
body: <p>I have the written the following code for transmitting UDP packets via broadcasting on a wireless network. The application that I have trying to develop requires the packets to be transmitted very fast, but unfortunately I cannot do so and need to add a sleep time. I find that below 500us sleep time, I am unable to send all the packets successfully.</p>
  
  <ol>
  <li>Why does the sleep time have to be so high?</li>
  <li>Is it possible to reduce this time by further optimization of this code?</li>
  <li>If I do not process the received packets buffer, is it okay? Or does this create problems?</li>
  </ol>
  
  <p>Note that I am running this code on a wireless radio which runs using <a href="http://openwrt.org/" rel="nofollow">OpenWrt</a>.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;arpa/inet.h&gt;  /* for sockaddr_in */
  
  #define BROADCAST_IP "192.168.255.255"
  #define BROADCAST_PORT 45454
  
  int b_sock=-1;
  
  void init_socket()
  {
    unsigned short b_port = BROADCAST_PORT;
    struct sockaddr_in b_addr;
    int broadcastPermission;
    char* rx_ip = BROADCAST_IP;
  
    if ((b_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0)
      perror("socket() failed");
  
    /* Set socket to allow broadcast */
    broadcastPermission = 1;
    if (setsockopt(b_sock, SOL_SOCKET, SO_BROADCAST, (void *) &amp;broadcastPermission, sizeof(broadcastPermission)) &lt; 0)
      perror("setsockopt() failed");
  
    int opts;
    opts = fcntl(b_sock,F_GETFL);
    if(opts &lt; 0)
      perror("fcntl get failed");
  
    opts = (opts | O_NONBLOCK);
    if(fcntl(b_sock,F_SETFL,opts) &lt; 0)
      perror("fcntl set failed");
  
    memset(&amp;b_addr, 0, sizeof(b_addr));   /* Zero out structure */
    b_addr.sin_family = AF_INET;                 /* Internet address family */
    b_addr.sin_addr.s_addr = inet_addr(rx_ip);/* Broadcast IP address */
    b_addr.sin_port = htons(b_port);         /* Broadcast port */
  
    if (bind(b_sock, (struct sockaddr *) &amp;b_addr, sizeof(b_addr)) &lt; 0)
      perror("rx bind() failed");
  }
  
  void send_thread_body(long int buf, struct sockaddr_in tx_addr)
  {
    if(sendto(b_sock, &amp;buf, sizeof(long int), 0, (struct sockaddr *)&amp;tx_addr, sizeof(tx_addr)) &lt; 0)
      printf("tx sent diff num bytes than expected: %d\n",buf);
  }
  
  
  int main(int argc, char *argv[])
  {
    init_socket();
    {
      timeval start, end;
      double diff = 0;
      long int num = 0;
  
      char *tx_ip = BROADCAST_IP;
      unsigned short tx_port = BROADCAST_PORT;
      struct sockaddr_in tx_addr;
  
      memset(&amp;tx_addr, 0, sizeof(tx_addr));   /* Zero out structure */
      tx_addr.sin_family = AF_INET;                 /* Internet address family */
      tx_addr.sin_addr.s_addr = inet_addr(tx_ip);/* Broadcast IP address */
      tx_addr.sin_port = htons(tx_port);         /* Broadcast port */
  
      double next = 0;
      double st = 0;
  
      while (num&lt;50000)
      {
        while (st &lt;= next)
        {
          gettimeofday(&amp;start,NULL);
          st = start.tv_sec*1000 + ((double)start.tv_usec)/1000.0;
        }
  
        send_thread_body(num,tx_addr);
  
        gettimeofday(&amp;end, NULL);
        diff += ((double)(((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec))))/1000000.0;
  
        num++;
  
        next = end.tv_sec*1000 + ((double)end.tv_usec)/1000.0 + 0.7;
      }
  
      printf("Avg time diff: %f\n",diff/50000.0);
    }
    close(b_sock);
    return 0;
  }
  </code></pre>