如何在不调用GetQueuedCompletionStatus()的情况下知道WSASend()操作是否已完成?

时间:2017-02-24 10:06:26

标签: c sockets winapi winsock iocp

调用WSASend()时,我必须将WSAOVERLAPPED实例传递给它,在上一次WSAOVERLAPPED操作完成之前,我无法重复使用此WSASend()实例(即完成数据包已放入完成端口时。)

我是否可以在不致电WSASend()的情况下了解GetQueuedCompletionStatus()操作是否已完成?

2 个答案:

答案 0 :(得分:2)

您需要将自己的套接字绑定到系统创建的IOCP,因为操作完成后,您的回调将被称为自动。你有两个选择:

创建套接字后

SOCKET socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED)

您需要致电BindIoCompletionCallback((HANDLE)socket, *,0)CreateThreadpoolIo((HANDLE)socket, *, 0, 0);

等等。您不需要致电GetQueuedCompletionStatus()或等待

答案 1 :(得分:0)

您可以使用WSAGetOverlappedResult获取WSASend进展:

/* size bytes to send */
WSASend(Sock, &aBuf, 1, NULL, 0, &overlap, NULL);

/* warning no test for error cases */
DWORD dummy;
DWORD sent;   
do 
{
    /* Ask for the sending progression without waiting */
    WSAGetOverlappedResult(Sock, &SendOverlapped, &sent, FALSE, &dummy);
    /* to prevent too much CPU usage */
    Sleep(15);
} while (size != sent);