title: UDP Sockets - One server, mulitple clients. Not sent to all clients
body: <p>I am writing in windows, using Visual Studio, and am trying to have one application use UDP sockets to broadcast a message to other applications.  This must be done with UDP sockets.</p>
  <p>It works well with just one client, but it does not when I add another client, and all listen on the same port.</p>
  <p>TX (Server)</p>
  <pre><code>/* Example from: http://matrixsust.blogspot.com/2011/10/udp-server-client-in-c.html */
  
  /* Use legacy sockets */
  #define _WINSOCK_DEPRECATED_NO_WARNINGS
  
  /* Add a linker to ws2_32.lib, the Winsock2 library */
  #pragma comment(lib, &quot;ws2_32.lib&quot;)
  
  #include &lt;stdio.h&gt;
  #include &lt;winsock2.h&gt;
  
  
  int main()
  {
      WSADATA wsaData;
      int sock, sin_size;
      struct sockaddr_in server_addr;
      struct hostent* host;
      char send_data[1024];
      boolean keep_running = 1;
      unsigned int cmd_no_block_arg = 1;
      int SERVER_PORT;
  
  
      /* Obtain a valid port */
      printf(&quot;Enter a port number to connect to (i.e. 7171):\n&quot;);
      scanf_s(&quot;%d&quot;, &amp;SERVER_PORT);
       
      if ((SERVER_PORT &lt; 1) || (SERVER_PORT &gt; 665534))
      {
          SERVER_PORT = 7171;
      }
  
      /* Initialize Winsock version 2.2 */
      WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
  
  
  
  
      /* CLIENT SIDE */
      host = (struct hostent*) gethostbyname((char*)&quot;127.0.0.1&quot;);
  
  
      if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
      {
          perror(&quot;socket&quot;);
          exit(1);
      }
  
      /* SETUP */
      server_addr.sin_family = AF_INET;
      server_addr.sin_port = htons(SERVER_PORT);
      server_addr.sin_addr = *((struct in_addr*)host-&gt;h_addr);
     
      memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
      sin_size = sizeof(struct sockaddr);
  
  
      /* Bind the socket, then enable non-blocking semantics */
      if ((ioctlsocket(sock, FIONBIO, &amp;cmd_no_block_arg) == SOCKET_ERROR))
      {
          printf(&quot;Could not non-bind socket. Error Code %i\n&quot;, WSAGetLastError());
      }
  
  
  
      
  
      while (keep_running)
      {
  
          printf(&quot;Type Something (q or Q to quit):&quot;);
          gets_s(send_data, sizeof(send_data));
  
          if ((strcmp(send_data, &quot;q&quot;) == 0) || strcmp(send_data, &quot;Q&quot;) == 0)
          {
              keep_running = 0;
          }
          else
          {
              sendto(sock, send_data, strlen(send_data), 0, (struct sockaddr*) &amp; server_addr, sizeof(struct sockaddr));
          }
  
      } // While
  
  }
  
  
  </code></pre>
  <p>RX (Clients)</p>
  <pre><code>/* Example from: http://matrixsust.blogspot.com/2011/10/udp-server-client-in-c.html */
  
  #define _WINSOCK_DEPRECATED_NO_WARNINGS
  
  /* Add a linker to ws2_32.lib, the Winsock2 library */
  #pragma comment(lib, &quot;ws2_32.lib&quot;)
  
  #include &lt;stdio.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      WSADATA wsaData;
      int sock;
      int addr_len, bytes_read;
      char recv_data[1024], send_data[1024];
      struct sockaddr_in server_addr, client_addr;
      int                sharing_flag = 1; /* Allow sharing */
      unsigned int       non_block_flag = 1; 
      unsigned int       SERVER_PORT = 7171;
      
  
      /* Obtain a valid port to connect to */
      printf(&quot;Enter a port number to connect to (i.e. 7171):\n&quot;);
      scanf_s(&quot;%d&quot;, &amp;SERVER_PORT);
  
      if ((SERVER_PORT &lt; 1) || (SERVER_PORT &gt; 65535))
      {
          SERVER_PORT = 7171;
      }
  
  
      /* Initialize Winsock version 2.2 */
      WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
  
  
      /* SERVER SIDE: bind(), recvfrom(), sendto() */
      if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
          perror(&quot;Socket&quot;);
          exit(1);
      }
  
      server_addr.sin_family = AF_INET;
      server_addr.sin_port = htons(SERVER_PORT);
      
      server_addr.sin_addr.s_addr = INADDR_ANY;
     
      memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
      
      /* Eliminates &quot;ERROR on binding: Address already in use&quot; error */
      if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&amp;sharing_flag, sizeof(sharing_flag)) == SOCKET_ERROR)
      {
          printf(&quot;Error on socket opt: %i\n&quot;, WSAGetLastError());
  
      }
  
      if ((bind(sock, (struct sockaddr*) &amp; server_addr, sizeof(struct sockaddr)) == SOCKET_ERROR) ||
          (ioctlsocket(sock, FIONBIO, &amp;non_block_flag) == SOCKET_ERROR))
      {
          printf(&quot;Error on socket bind: %i\n&quot;, WSAGetLastError());
   
      }
  
      addr_len = sizeof(struct sockaddr);
  
      printf(&quot;\nUDPServer Waiting for client on port %i\n&quot;, SERVER_PORT);
  
     
      while (1)
      {
          
          bytes_read = recvfrom(sock, recv_data, 1024, 0, (struct sockaddr*) &amp; client_addr, &amp;addr_len);
          
          if (bytes_read &gt;= 0)
          {
              recv_data[bytes_read] = '\0';
              
              printf(&quot;\n(%s , %d) said : &quot;, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
              printf(&quot;%s\n&quot;, recv_data);
                        
              fflush(stdout);
  
          } /* if we read in bytes */
  
      } /* while */
  
      return 0;
  }
  </code></pre>