English 中文(简体)
C++/Win。 不要忘记FD_CLOSE
原标题:C++/Win. Not getting FD_CLOSE

我的袖珍袖珍,呼吁把WSA_WOULD_BLOCK归还给我们。 因此,我开始“接受/阅读”读写和签署FD_。 READ and FD_CLOSE.

故事是:由于服务器没有运行和运行,连接将依次失败。 我的理解是,我的座右铭应当很快获得FD_CLOSE,我需要就清洁问题采取后续行动。

它没有发生。 我应多快收到FD_CLOSE? 这种做法是否适当? 是否有其他办法理解这一连接失败? 如果有人牵线搭桥,我会收到FD_CLOSE。

我在成功地向DoConnect(DoConnect())发出呼吁之后,确实开始接受我的听话,我担心,令人沮丧的状况使我无法获得FD_CLOSE。

这里有一些法典:

int RecvSocketThread::WaitForData()
{
     int retVal = 0
     while (!retVal)
     {
         // sockets to pool can be added on other threads.
         // please validate that all of them in the pool are connected
         // before doing any reading on them
         retVal = DoWaitForData();
     }
}

int RecvSocketThread::DoWaitForData()
{
    // before waiting for incoming data, check if all sockets are connected
    WaitForPendingConnection_DoForAllSocketsInThePool();


    // other routine to read (FD_READ) or react to FD_CLOSE
    // create array of event (each per socket) and wait
}
void RecvSocketThread::WaitForPendingConnection_DoForAllSocketsInThePool()
{
    // create array and set it for events associated with pending connect sockets
    HANDLE* EventArray = NULL;
    int counter = 0;
    EventArray = new HANDLE[m_RecvSocketInfoPool.size()];

    // add those event whose associated socket is still not connected
    // and wait for FD_WRITE and FD_CLOSE. At the end of this function
    // don t forget to switch them to FD_READ and FD_CLOSE
    while (it != m_RecvSocketInfoPool.end())
    {
         RecvSocketInfo* recvSocketInfo = it->second;
         if (!IsEventSet(recvSocketInfo->m_Connected, &retVal2))
         {
             ::WSAEventSelect(recvSocketInfo->m_WorkerSocket, recvSocketInfo->m_Event, FD_WRITE | FD_CLOSE);
             EventArray[counter++] = recvSocketInfo->m_Event;
         }
         ++it;
    }
    if (counter)
    {
        DWORD indexSignaled = WaitForMultipleObjects(counter, EventArray, WaitAtLeastOneEvent, INFINITE);

        // no matter what is further Wait doen t return for failed to connect socket

        if (WAIT_OBJECT_0 <= indexSignaled &&
                   indexSignaled < (WAIT_OBJECT_0 + counter))
        {
            it = m_RecvSocketInfoPool.begin();
            while (it != m_RecvSocketInfoPool.end())
            {
                RecvSocketInfo* recvSocketInfo = it->second;
                if (IsEventSet(recvSocketInfo->m_Event, NULL))
                {
                  rc = WSAEnumNetworkEvents(recvSocketInfo->m_WorkerSocket,
                  recvSocketInfo->m_Event, &networkEvents);

                   // Check recvSocketInfo->m_Event using WSAEnumnetworkevents
                   // for FD_CLOSE using FD_CLOSE_BIT
                   if ((networkEvents.lNetworkEvents & FD_CLOSE))
                   {
                       recvSocketInfo->m_FD_CLOSE_Recieved = 1;
                       *retVal = networkEvents.iErrorCode[FD_CLOSE_BIT];
                   }
                   if ((networkEvents.lNetworkEvents & FD_WRITE))
                   {
                       WSASetEvent(recvSocketInfo->m_Connected);
                       *retVal = networkEvents.iErrorCode[FD_WRITE_BIT];
                   }
                }
                ++it;
            }
        }

        // if error - DoClean, if FD_WRITE (socket is writable) check if m_Connected
        // before do any sending
    }
}
问题回答

如果<条码>链接(<>>>>/代码>故障,你将不收到<条码>通知。 您必须签署<代码>FD_CONNECT,以发现这一点。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625.aspx”rel=“nofollow”>。

With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios:

•Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.

•If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).

•If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).

link(>>)是成功的,否则将成为WinSock错误代码。

If you call connect() and get a blocking notification you have to write more code to monitor for connect() completion (success or failure) via one of three methods as described here.

With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios:

•Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.

•If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).

•If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).

I think I need to start Receving thread once socket handle is created, but before connect is called. It is too late to create it after connect was called on asynchronous socket. For synchronous socket those two calls createsocket() and connect() was just two consequitive lines. Does not work for non-blocking.

在此情况下,在开始接收线索时,我需要检查FD_CONNECT和(或)FLD_ITE,以便了解联系图。





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签