如何清除WinSock中的接收缓冲区
我去寻找这个问题,发现至少有一个人提出了这个问题,但没有得到答案。我认为需要启动发送和接收段(同一套接字连接上较大进程的一部分)并期望接收缓冲区中已经没有任何内容。
我没有看到答案,但在我自己意识到另一种看起来更干净的方法之前确实想到了这个拼凑(读到下一段的“结尾”)。只需将 RecieveTimeout 设置为 1,或者,如果您认为发送方仍在发送过程中,则设置一些稍大的值。然后只需使用 socketflags = 0 调用 Receive 并丢弃结果。
有人有更好的方法吗?
I went searching for this and found at least one person who asked but got no answer. I think there is a need to start a send and receive segment (part of a larger process on same socket connection) and expect that there is nothing already in the receive buffer.
I saw no answer but did think of this kludge before I myself realized another way around it that seemed cleaner (read to "ends with" on the next segment). Simply set the RecieveTimeout to 1 or, if you think the sender is still in the process of sending, some slightly larger value. Then just call Receive with socketflags = 0 and discard the results.
Does anyone have a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个要求的由来是什么?您真的需要丢弃数据吗?
What is the origin of this requirement? Do you really have a need to throw data away?
我当时以为我这样做了,但后来只是读取并缓存了结果,然后从缓存中删除了。我正在搜索发送命令时出现的不同结果序列(字符串)。发送的下一个命令不能假设接收缓冲区中不再有任何字符,因为协议“标准”指定响应可能以 CR 或 LF 或 CRLF 或 LFCR 或什么都没有结束。由于该协议的定义非常松散,因此不同的组织以不同的方式实现该协议。
I thought I did at the time but then just read and cached the results and then removed from cache instead. I'm searching for different result sequences (strings) that come about when sending a command. The next command sent can't assume that there are no longer any chars in the receive buffer since the protocol "standard" specifies that a response may end with a CR or LF or CRLF or LFCR or nothing. This protocol is implemented differently by different organizations since it is so loosely defined.