MrDuk
MrDuk

Reputation: 18242

First buffer from UDP connection contains junk - subsequent connections are fine

I'm really 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 "@7777~15~3701" - the first time I connect, I get garbage in the buffer. However, once the client exits, and I launch it again, the next buffer "@7777~15~3702" comes through just fine.

int
RoutingManager::SendMessage(struct sockaddr_in toNode, char buffer[1024])
{
    #if logging > 1
        cout << "Sending: " << buffer << 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 *)&toNode,length);

    if (n < strlen(buffer))
        perror("Sendto");

    cout << "Sent: " << n << " bytes of data\n";
}

.. the buffer I pass in here is generated from:

//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 << MYPORT << endl;
    return buffer;
}

Server Output:

Sending: @7777~15~3701
Sent: 1 bytes of data
Waiting for nodes...

Client Side:

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 *)&server,length);

    if (n < 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 *)&from, &length);

    if (n < 0) 
        perror("recvfrom");

    parser.ParseMessage(buffer, fromNode, messages);
}

When I parse the message:

bool
RoutingMessage::ParseMessage(char* buffer, int &fromNode, map<int, string> &messages, const int MAX_CHARS_PER_LINE, 
                                const int MAX_TOKENS_PER_LINE, const char* const DELIMITER)
{
    #if logging > 1
        cout << "Buffer: " << buffer << 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<string> tokens;
    while(temp != NULL)
    {
        string val(temp);
        tokens.push_back(val);
        temp = strtok(NULL, DELIMITER);
    }

    //store messages in the map: <message-type>, <message>
    for (int i = 0; i < tokens.size(); i+=2)
        messages.insert(pair<int, string>(atoi(tokens[i].c_str()), tokens[i+1]));

    //all good
    return true;
}

And output the results, I get junk:

Buffer: <junk-symbol>
Buffer malformated!: Success
Node: 0

But when the client disconnects, and I relaunch the same executable, I get:

Buffer: @7777~15~3702
Node: 7777
Message Type: 15 Message: 3702

As I'd expect. Anyone know of something I could check?

Corrected Method --

void
RoutingManager::GenerateConnectionString(struct Node n, char* buffer)
{
    bzero(buffer,512);
    sprintf(buffer, "@7777~15~%d", n.id);
}

The above seems to solve my issues.

Upvotes: 1

Views: 344

Answers (1)

SKi
SKi

Reputation: 8466

The problem seems to be in GenerateConnectionString() function:

char*
RoutingManager::GenerateConnectionString(struct Node n)
{
    char buffer[512];
    ....
    return buffer;
}

It returns a pointer to a local variable. So the returned pointer points to the stack. So the data will be corrupted later when the program uses stack from that area.

Upvotes: 4

Related Questions