Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows winsock中的FD_关闭事件_Windows_Sockets_Events_Winsock - Fatal编程技术网

Windows winsock中的FD_关闭事件

Windows winsock中的FD_关闭事件,windows,sockets,events,winsock,Windows,Sockets,Events,Winsock,我正在使用事件驱动的winsock套接字开发http客户端。但我对如何处理FD_关闭消息感到困惑。在某些情况下,即使套接字中仍有数据要读取,我也会收到FD_CLOSE。所以我现在的代码是这样的: WSAEventSelect(s,hevent,FD_READ | FD_WRITE | FD_CLOSE); while (1) { WaitForSingleEvent(hevent,TIMEOUT); WSAEnumNetworkEvents(s,hevent,&netev

我正在使用事件驱动的winsock套接字开发http客户端。但我对如何处理FD_关闭消息感到困惑。在某些情况下,即使套接字中仍有数据要读取,我也会收到FD_CLOSE。所以我现在的代码是这样的:

WSAEventSelect(s,hevent,FD_READ | FD_WRITE | FD_CLOSE);
while (1)
{
    WaitForSingleEvent(hevent,TIMEOUT);
    WSAEnumNetworkEvents(s,hevent,&netevent);
    if ((netevent.lNetworkEvents & FD_READ) || (netevent.lNetworkEvents & FD_CLOSE))
    {
        do
        {
            resrecv=recv(s,buf,sizeof(buf),0);
            if (resrecv<0)
                throw "recv failed";
            if (resrecv==0)                 
                break;
           /*
           Handle the message here.
           */

        } while (netevents.lNetworkEvents & FD_CLOSE);
    }
    /*
    Write response to the socket. (If we received all of the request)
    */
    if (netevent.lNetworkEvents & FD_CLOSE)
    {
        closesocket(s);
        break;
    }
}
WSAEventSelect(s,hevent,FD|u READ | FD|u WRITE | FD|u CLOSE);
而(1)
{
WaitForSingleEvent(hevent,超时);
WSAEnumNetworkEvents(s、hevent和netevent);
if((netevent.lNetworkEvents&FD|u READ)| |(netevent.lNetworkEvents&FD|u CLOSE))
{
做
{
resrecv=recv(s,buf,sizeof(buf),0);

如果(resrecvIt在我看来不错,但我很惊讶你能同时得到它们。否则每个人都会有这个问题。你确定吗?当一端发送一条长消息,并在另一端完全收到之前优雅地关闭套接字时,这种情况似乎会发生。至少,在同一系统上运行的两个演示程序是这样说的。你最好完全忽略FD_CLOSE,而只是对FD_READ上的
recv()
取零做出反应。我想这会管用的。但是,为什么FD_CLOSE会存在呢?我想这是为了抓住你发现的那个极端情况。