gpt4 book ai didi

c - recv 返回旧数据

转载 作者:行者123 更新时间:2023-12-03 11:52:37 26 4
gpt4 key购买 nike

这个循环应该逐行从套接字中获取数据并将其放入缓冲区。出于某种原因,当没有新数据返回时, 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com