title: First buffer from UDP connection contains junk - subsequent connections are fine
body: <p>I'm <em>really</em> going to try to avoid posting my entire project on here - unless it's needed :)
  I have a client/server program I'm writing where the client contacts the server via a UDP port, and expects to receive a packet in the format <code>"@7777~15~3701"</code> - the first time I connect, I get garbage in the buffer. However, once the client exits, and I launch it again, the next buffer <code>"@7777~15~3702"</code> comes through just fine.</p>
  
  <pre><code>int
  RoutingManager::SendMessage(struct sockaddr_in toNode, char buffer[1024])
  {
      #if logging &gt; 1
          cout &lt;&lt; "Sending: " &lt;&lt; buffer &lt;&lt; endl;
      #endif
  
      int n;
      unsigned int length = sizeof(struct sockaddr_in);
      //buffer = "@7777~15~3702"
      n = sendto(mySocket, buffer, strlen(buffer),0,
                  (const struct sockaddr *)&amp;toNode,length);
  
      if (n &lt; strlen(buffer))
          perror("Sendto");
  
      cout &lt;&lt; "Sent: " &lt;&lt; n &lt;&lt; " bytes of data\n";
  }
  </code></pre>
  
  <p>.. the buffer I pass in here is generated from:</p>
  
  <pre><code>//FIXED: This was the source of my issue it appears - a corrected
  //implementation of this method has been included at the bottom.
  char*
  RoutingManager::GenerateConnectionString(struct Node n)
  {
      char buffer[512];
      bzero(buffer,512);
      sprintf(buffer, "@7777~15~%d", n.id);
      cout &lt;&lt; MYPORT &lt;&lt; endl;
      return buffer;
  }
  </code></pre>
  
  <p><strong>Server Output:</strong></p>
  
  <pre><code>Sending: @7777~15~3701
  Sent: 1 bytes of data
  Waiting for nodes...
  </code></pre>
  
  <p><strong>Client Side:</strong></p>
  
  <pre><code>RoutingNode::GetMyID()
  {
      int n;
      char buffer[256];
      bzero(buffer,256);
      unsigned int length = sizeof(struct sockaddr_in);
      struct sockaddr_in from;
  
      n = sendto(mySocket,"!", strlen("!"),0,(const struct sockaddr *)&amp;server,length);
  
      if (n &lt; 0) 
          perror("Sendto");
  
      //once we have our information from the manager, let's hog some cpu
          //remove this crap when stuff gets more reliable
          fcntl(mySocket, F_SETFL, O_NONBLOCK);
  
      while(buffer[0] != '@')
          n = recvfrom(mySocket,buffer,256,0,(struct sockaddr *)&amp;from, &amp;length);
  
      if (n &lt; 0) 
          perror("recvfrom");
  
      parser.ParseMessage(buffer, fromNode, messages);
  }
  </code></pre>
  
  <p>When I parse the message:</p>
  
  <pre><code>bool
  RoutingMessage::ParseMessage(char* buffer, int &amp;fromNode, map&lt;int, string&gt; &amp;messages, const int MAX_CHARS_PER_LINE, 
                                  const int MAX_TOKENS_PER_LINE, const char* const DELIMITER)
  {
      #if logging &gt; 1
          cout &lt;&lt; "Buffer: " &lt;&lt; buffer &lt;&lt; endl;
      #endif
  
      if (buffer[0] != '@')
      {
          perror("Buffer malformated!");
          return false;
      }
  
      //remove the '@'
      buffer++;
  
      char buf[MAX_CHARS_PER_LINE];
      strcpy(buf, buffer);
      char* temp = strtok(buf, DELIMITER);
      if (temp == NULL)
      {
          perror("Buffer malformated!");
          return false;
      }
  
      fromNode = atoi(temp);
      temp = strtok(NULL, DELIMITER);
  
      vector&lt;string&gt; tokens;
      while(temp != NULL)
      {
          string val(temp);
          tokens.push_back(val);
          temp = strtok(NULL, DELIMITER);
      }
  
      //store messages in the map: &lt;message-type&gt;, &lt;message&gt;
      for (int i = 0; i &lt; tokens.size(); i+=2)
          messages.insert(pair&lt;int, string&gt;(atoi(tokens[i].c_str()), tokens[i+1]));
  
      //all good
      return true;
  }
  </code></pre>
  
  <p>And output the results, I get junk:</p>
  
  <pre><code>Buffer: &lt;junk-symbol&gt;
  Buffer malformated!: Success
  Node: 0
  </code></pre>
  
  <p>But when the client disconnects, and I relaunch the same executable, I get:</p>
  
  <pre><code>Buffer: @7777~15~3702
  Node: 7777
  Message Type: 15 Message: 3702
  </code></pre>
  
  <p>As I'd expect. Anyone know of something I could check?</p>
  
  <h2>Corrected Method --</h2>
  
  <pre><code>void
  RoutingManager::GenerateConnectionString(struct Node n, char* buffer)
  {
      bzero(buffer,512);
      sprintf(buffer, "@7777~15~%d", n.id);
  }
  </code></pre>
  
  <p>The above seems to solve my issues.</p>