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



真的要尽量避免在这里发布我的整个项目-除非它是必要的:)我有一个客户端/服务器程序,我正在编写客户端通过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 datan";
}

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

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

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

问题似乎是在GenerateConnectionString()功能:

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

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

最新更新