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