来自UDP连接的第一个缓冲区包含垃圾 - 后续连接很好

时间:2014-03-11 06:08:26

标签: c++ sockets

真的试图避免在这里发布我的整个项目 - 除非它需要:) 我有一个客户端/服务器程序我写的是客户端通过UDP端口与服务器联系,并期望以"@7777~15~3701"格式接收数据包 - 我第一次连接时,我得到垃圾邮件缓冲。但是,一旦客户端退出,我再次启动它,下一个缓冲区"@7777~15~3702"就可以了。

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";
}

..我在这里传递的缓冲区是从:

生成的
//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;
}

服务器输出:

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

客户端:

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);
}

当我解析消息时:

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;
}

输出结果,我得到垃圾:

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

但是当客户端断开连接并重新启动相同的可执行文件时,我得到:

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

正如我所期待的那样。有人知道我能检查的东西吗?

更正方法 -

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

以上似乎解决了我的问题。

1 个答案:

答案 0 :(得分:4)

问题似乎出现在GenerateConnectionString()函数中:

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

它返回一个指向局部变量的指针。 所以返回的指针指向堆栈。因此,当程序使用该区域的堆栈时,数据将会被破坏。