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
}
This loop is supposed to take data from a socket line by line and put it in a buffer. For some reason, when there is no new data to return, recv returns the last couple lines it got. I was able to stop the bug by commenting out the first recv, but then I cant tell how long the next line will be. I know it's not a
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的问题是,当你使用带有MSG_PEEK的recv时,你给recv缓冲区的整个大小,如果已经有两行,比如“HELLO\r\nHELLO\r\n”,它会将它们读入你的buff中。
ptr会指向第一个\r\n,然后你用(ptr - buff)调用recv,这将使recv只读取第一个HELLO,进入buf,但由于你已经将该信息读入buff,你将处理这两个行,但将 \r\nHELLO\r\n 留在队列中,因为您没有完全阅读它们。
下次您查看它时,会看到已经处理过的信息,让您相信您正在获取重复的数据。
(我希望我写得足够清楚,这是一个非常令人困惑的错误:)
Your problem is that when you use recv with MSG_PEEK, you are giving recv the whole size of your buffer, if there are two lines already there, like "HELLO\r\nHELLO\r\n" it will read them into your buff.
ptr would point to the first \r\n, then you call recv with (ptr - buff) which will make recv to read only the first HELLO, into buf, but since you already READ that info into buff, you will process the two lines, but leaving \r\nHELLO\r\n in your queue, because you did not fully read them.
Next time you would peek into it and have info hanging that you already processed, leading you to believe that you are getting repeated data.
(I hope I wrote this clear enough, it is a very confusing bug you got there :)
我需要在第二个recv 的长度上添加2,所以我会采用“\r\n”。否则,它看到第一个“\r\n”并认为结尾的行是 buf[0]。
I needed to add 2 to the length of the second recv so I'd take the "\r\n". Otherwise, it sees the first "\r\n" and thinks the line of the end is buf[0].
嗨,我找到了解决方案:
Hi i find the solution :
手册指出:
所以我认为你得到了正确的行为,但也许期待其他的东西。
The manual states:
So I think you're getting the correct behavior, but perhaps expecting something else.