title: C++ UDP server send message just when get client's address config
body: <p>My purpose is to create a UDP sender to broadcast message to a specific IP and Port, without any config requirment. I defined <code>struct sockaddr_in si_me</code> variable to set the server config on it and bind it:</p>
  
  <pre><code>si_me.sin_family = AF_INET;
  si_me.sin_port = htons(PORT);
  si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  if( bind(s , (struct sockaddr*)&amp;si_me, sizeof(si_me) ) == -1)
      {
          die("bind");
      }
  </code></pre>
  
  <p>I expected when I send data to configured IP and Port in <code>si_me</code> variable, every client could receive the message but it doesn't happen and using both UDP client and <code>nc 127.0.0.1 9090 -u</code> command on Linux terminal, I couldn't receive any message!
  I found that for each client I should create a <code>struct sockaddr_in si_other</code> variable. The client should send me a message so I can get client's config:</p>
  
  <pre><code>if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &amp;si_other, (socklen_t*)&amp;slen)) == -1)
          {
              die("recvfrom()");
          }
  </code></pre>
  
  <p>So now using <code>si_other</code> variable I can send messages successfully. I checked the <code>si_other</code> variable and I found the IP is the same with server IP but the Port is different for each client so because of this I couldn't send data to the client without knowing what is the destination Port!</p>
  
  <p>Here is the complete source code:</p>
  
  <pre><code>int main(void)
  {
      struct sockaddr_in si_me, si_other;
  
      int s, i, slen = sizeof(si_other) , recv_len;
      char* msg = "Hellooooo\n";
      char buf[BUFLEN] ;
  
      //create a UDP socket
      if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
      {
          die("socket");
      }
  
      // zero out the structure
      memset((char *) &amp;si_me, 0, sizeof(si_me));
  
      si_me.sin_family = AF_INET;
      si_me.sin_port = htons(PORT);
      si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  
      std::cout &lt;&lt; si_me.sin_addr.s_addr &lt;&lt; std::endl ;
      std::cout &lt;&lt; si_me.sin_port &lt;&lt; std::endl ;
      std::cout &lt;&lt; si_other.sin_addr.s_addr &lt;&lt; std::endl ;
      std::cout &lt;&lt; si_other.sin_port &lt;&lt; std::endl ;
      if( bind(s , (struct sockaddr*)&amp;si_me, sizeof(si_me) ) == -1)
          {
               die("bind");
          }
  
      // waiting to recieve client's message
      std::cout &lt;&lt; "Waiting for start command ... " &lt;&lt; std::endl ;
      if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &amp;si_other, (socklen_t*)&amp;slen)) == -1)
          {
              die("recvfrom()");
          }
      std::cout &lt;&lt; si_me.sin_addr.s_addr &lt;&lt; std::endl ;
      std::cout &lt;&lt; si_me.sin_port &lt;&lt; std::endl ;
      std::cout &lt;&lt; si_other.sin_addr.s_addr &lt;&lt; std::endl ;
      std::cout &lt;&lt; si_other.sin_port &lt;&lt; std::endl ;
      long count = 0 ;
      while(0)
      {
  
              if (sendto(s, msg, strlen(msg), 0, (struct sockaddr*) &amp;si_other, slen) == -1)
              {
                  die("sendto()");
              }
              std::cout &lt;&lt; "Message number " &lt;&lt; ++ count &lt;&lt; " has been sent" &lt;&lt; std::endl ;
  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>So can anyone tell me how should I change the source to broadcast messages without the need of getting client message to configure its destination IP and Port?</p>