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