c - WSAAsyncSelect/Event 上的 FD_WRITE 处理?

标签 c sockets winapi events networking

好的,关于下面的引述,我有以下粗体问题:

The FD_WRITE network event is handled slightly differently. An FD_WRITE network event is recorded when a socket is first connected with a call to the connect, ConnectEx, WSAConnect, WSAConnectByList, or WSAConnectByName function or when a socket is accepted with accept, AcceptEx, or WSAAccept function and then after a send fails with WSAEWOULDBLOCK and buffer space becomes available. Therefore, an application can assume that sends are possible starting from the first FD_WRITE network event setting and lasting until a send returns WSAEWOULDBLOCK. After such a failure the application will find out that sends are again possible when an FD_WRITE network event is recorded and the associated event object is set.

因此,当连接建立/接受或 send() 在缓冲区再次空闲后因 WSAWOULDBLOCK 而失败时,将生成 FD_WRITE。

您将如何设计一个程序来使用这样的事件?我的方法将采用以下伪代码 - 我想知道我的做法是否正确:

Call a function
send
if returns WSAWOULDBLOCK
save state of pointers and buffers globally
end function
..
.. //message loop
..
FD_WRITE event!
resume last state of the buffer/send to corresponding socket.
continue sending.

是否有更好的现有模式或...?谢谢。

编辑:刚刚想到了另一个想法:

Create a queue of "workload"
push data in the queue
if FD_WRITE generated already & sending later - manually signal FD_WRITE to get inside routine
FD_WRITE routine will process sending in queue
if WSAWOULDBLOCK, save queue position & return
..
.. //event loop
..
FD_WRITE event!
continue sending from queue.

最佳答案

How would you design a program to use such an event?

为所有传出数据创建缓冲区。

每当您需要发送数据时,请先检查缓冲区。如果缓冲区中有任何未发送的数据,则将所有新数据附加到缓冲区的末尾以供以后重新发送。否则,尝试通过套接字发送尽可能多的数据,如果报告了 WSAEWOULDBLOCK 则停止发送并将剩余的未发送数据部分附加到缓冲区的末尾以供以后重新发送发送。

每当您收到 FD_WRITE 事件时,检查缓冲区。如果它不为空,则尽可能多地通过套接字发送缓冲数据,删除成功发送的数据的任何部分,如果报告 WSAEWOULDBLOCK 则停止发送并保留剩余的未发送数据在缓冲区中供以后重新发送。

关于c - WSAAsyncSelect/Event 上的 FD_WRITE 处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739861/

相关文章:

具有两个条件的 C 循环 - 差异

C:关于指针使用的困惑

c - 重新分配指针的指针的指针

bash - 有没有办法将命令输出的连续流发送到远程监听器

c - 静态变量在栈上的空间分配

python - 如何解决 ssl.socket 问题

java - 让 PrintWriter 在 Servlet 执行后工作 - 当serversocket接受连接时由套接字客户端调用

c++ - 一个 DWORD 可以容纳多少个标志?

windows - 有没有人听说过标准 Windows 消息框的这个奇怪错误?

windows - Windows 到底是什么版本?