2

using socket with the overlapped operation selected the event-based completion notification; Have 2 events, one for data, the other to cancel long send/recv:

 HANDLE events[] = { m_hDataEvent, m_hInterruptEvent };

then calling WSASend,

 WSASend(m_Socket, &DataBuf, 1, NULL, 0, &SendOverlapped, NULL);

followed by

 WSAWaitForMultipleEvents(2, events, FALSE, INFINITE, FALSE);

which is setup to return on any one event signaled. Now assume send is in progress, and m_hInterruptEvent is signaled. WSAWaitForMultipleEvents returns, technically the function calling send can return as well and delete internally allocated buffers.

What is not clear to me, the WSASend may still be working in background, and deleting buffers will cause data corruption in best case.

What would be the proper way to stop the background Send/Receive, if the socket needs to be used for something else immediately?

I looked at the CancelIO(), but the MSDN never mentions it in relation to Sockets, does it work with file based IO only?

4

1 回答 1

1

发送后尝试取消它是没有意义的。即使你成功了,你也会遇到问题,因为接收应用程序不会知道传输被中断。您的新消息将被误认为是旧消息的结尾。

如果您觉得需要取消长发送,您可能应该查看您的应用程序设计。

  • 发送块并检查块之间的取消。确保您有办法与接收方沟通传输已取消。
  • 关闭套接字以取消。同样,确保客户端有办法知道这是一个中断的传输(例如,如果客户端提前知道总长度,他们将识别出中断的传输)。
  • 只需等待它在后台成功,不要担心。如果您有紧急消息,请为它们使用单​​独的连接。

对于您的特定问题“停止后台发送/接收的正确方法是什么,如果套接字需要立即用于其他用途”,答案是:套接字很便宜 - 只需使用两个- 一个用于慢速传输其他用于紧急消息。

于 2012-05-24T21:59:40.953 回答