title: UDP Sender program verification
body: <p>I have wrote a simple program that is supposed to send UDP Packages with changing length. The sizes of UDP packets can change anytime.
  The user can input through command line the port number and the minimum and maximum length of the message. At the first 32 bits of the payload is the UDP Packet Number.
  I used the sendto() function and I am receiving a valid return number  but when I try to verify it using a UDP Server program (for example SocketTest) I cannot seeany data on the other end.
  Following is my code :</p>
  
  <pre><code>#define DEFAULT_PORT 355
  
  int   Port = DEFAULT_PORT;          // Port to send data to
  int Max_Length = 100, Min_Length = 8; //default values
  static const char alphanum[] =
      "0123456789"
      "!@#$%^&amp;*"
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      "abcdefghijklmnopqrstuvwxyz";
  int stringLength = sizeof(alphanum);
  
  char genRandom()
  {
      return alphanum[rand() % stringLength];
  }
  
  void usage()
  {
      cout &lt;&lt; "-m:int Define minimum UDP packet size" &lt;&lt; endl;
      cout &lt;&lt; " -M:int Define maximum UDP packet size" &lt;&lt; endl;
      cout &lt;&lt; " -p:int Define remote port" &lt;&lt; endl;
  
  }
  
  void ValidateArgs(int argc, char **argv)
  {
      int i;
  
      for (i = 1; i &lt; argc; i++)
      {
          if ((argv[i][0] == '-'))
          {
              switch (argv[i][1])
              {
              case 'm':
                  if (strlen(argv[i]) &gt; 3) {
                      Min_Length = (double)atoi(&amp;argv[i][3]);
                  }
                  break;
              case 'M':
                  if (strlen(argv[i]) &gt; 3) {
                      Max_Length = (double)atoi(&amp;argv[i][3]);
                  }
                  break;
              case 'p':
                  if (strlen(argv[i]) &gt; 3) {
                      Port = atoi(&amp;argv[i][3]);
                  }                   
                  break;
              default:
                  usage();
                  break;
              }
          }
      }
  }
  int main(int argc, char **argv)
  {
      sockaddr_in my_addr;
      ValidateArgs(argc, argv);
      WSADATA wsd;
      int Result = WSAStartup(MAKEWORD(2, 2), &amp;wsd);
      if (Result != NOERROR) {
          cout &lt;&lt; "WSAStartup Failed!" &lt;&lt; endl;
          exit(0);
      }
      SOCKET sock;
      sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
      if (sock == INVALID_SOCKET) {
          cout &lt;&lt; "Socket Failer with error: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          WSACleanup();
          exit(1);
      }
  
      my_addr.sin_family = AF_INET;
      my_addr.sin_addr.s_addr = inet_addr("192.68.0.31");
      my_addr.sin_port = htons((short)Port);
      string data = "";
      /*Get a Random String Length*/
      random_device seeder;
      int UDP_Packet_number = 1;
      string buffer_data;
      mt19937 engine(seeder());
      uniform_int_distribution&lt;int&gt; dist(Min_Length, Max_Length);// user input from command line
      int StrLen = dist(engine);
      while (true) {
          /*Get aRandom String*/
          for (int i = 0; i &lt; StrLen; ++i)
          {
              data += genRandom();
  
          }
          srand(time(0));
          StrLen = dist(engine);
          char numbr_pkg[4];
          numbr_pkg[0] = (UDP_Packet_number &gt;&gt; 24) &amp; 0xff;
          numbr_pkg[1] = (UDP_Packet_number &gt;&gt; 16) &amp; 0xff;
          numbr_pkg[2] = (UDP_Packet_number &gt;&gt; 8) &amp; 0xff;
          numbr_pkg[3] = UDP_Packet_number &amp; 0xff;
          /*First 32 Bits is the packet Number*/
          for (int i = 0; i &lt; 4; i++) {
              buffer_data += numbr_pkg[i];
          }       
  
          buffer_data += data;
          data = "";
          int result = sendto(sock, buffer_data.c_str(), buffer_data.length(), 0, (SOCKADDR *)&amp;my_addr, sizeof(my_addr));
          //cout &lt;&lt; result &lt;&lt; "  " &lt;&lt; buffer_data.length() &lt;&lt; endl;
          buffer_data = "";
          UDP_Packet_number++;
  
      }
      return 0;
  }
  </code></pre>
  
  <p>Can you please tell me what am I doing wrong? I am trying to find a solution for a long time.</p>