c - recv 返回旧数据

标签 c sockets buffer recv

该循环应该从套接字逐行获取数据并将其放入缓冲区中。由于某种原因,当没有新数据返回时,recv 返回它得到的最后几行。我能够通过注释掉第一个recv来阻止该错误,但随后我无法判断下一行会有多长。我知道这不是一个

while(this->connected){
    memset(buf, '\0', sizeof(buf));
    recv(this->sock, buf, sizeof(buf), MSG_PEEK);           //get length of next message
    ptr = strstr(buf, "\r\n");
    if (ptr == NULL) continue;
    err = recv(this->sock, buf, (ptr-buf), NULL);    //get next message

    printf("--%db\n%s\n", err, buf);

    tok[0] = strtok(buf, " ");
    for(i=1;tok[i-1]!=NULL;i++) tok[i] = strtok(NULL, " ");

//do more stuff
}

最佳答案

manual状态:

MSG_PEEK This flag causes the receive operation to return data from the beginning of the receive queue without removing that data from the queue. Thus, a subsequent receive call will return the same data.

所以我认为你得到了正确的行为,但也许期待其他的东西。

关于c - recv 返回旧数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2728086/

相关文章:

ios - CCRenderTexture 端的 cocos2D-iphone 中的 OpenGL 500 错误

c - Postfix 评估中的数据类型转换

c++ - 如何将数据传递给正在运行的线程

c++ - 简单的套接字程序偶尔会失败

c# - Socket 的扩展方法内存泄漏 'ReceiveAsync' !

c++ - rdbuf() 奇怪的行为

c - 在 C 中嵌入 ruby​​ 代码

c - 我正在尝试将此功能转换为程序集,但我不明白它在做什么

sockets - 处理传入消息的最佳方式是什么?

c - 从文件读取到缓冲区时出现段错误