English 中文(简体)
WIN32 Socket API:使用基于事件的补全通知取消对套接字的发送/记录
原标题:WIN32 Socket API: Canceling Send/Recv on socket using event-based completion notification

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 };

然后打电话给WSASend,

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

和继 继 继 继 继 继

 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.

我不清楚的是,WSASend可能仍然在背景中运作,删除缓冲在最好的情况下会造成数据腐败。

< energ> 如果插座需要立即用于其它东西, 那么阻止背景发送/ 接收的正确方式是什么?

I looked at the CanceleIO (), 但是 MSDN 从未提及它与索凯茨的关系, 它是否只与基于文件 IO 一起工作?

问题回答

尝试在发送后取消它是没有道理的。 即使您成功, 您也会遇到问题, 因为接收程序不会知道传输被中断。 您的新信件将会被错误地误解为旧信件的结尾 。

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

  • Send in chunks and check for cancellation in between chunks. Ensure you have a way of communicating to the receiver that the transmission was cancelled.
  • Close the socket to cancel. Again, ensure the client has a way to know that this is an interrupted transmission (for example if the client knows the total length in advance they will recognise an interrupted transmission).
  • Just wait for it to succeed in the background and don t worry. If you have urgent messages use a separate connection for them.

对于您的特殊问题“ 强/ 强”, “ 阻止背景发送/ 接收的适当方式是什么, 如果套接字需要立即用于其它东西, ”





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?