2015-10-08 64 views
0

此代碼編譯並運行。當客戶端連接時,WSAWaitForMultipleEvents返回nReturnCode = 0(相當於WSA_WAIT_EVENT_0)。但是代碼總是顯示「EVENT:NOT ACCEPT」,當我在調試模式下查看NetworkEvents時,它完全填充了零。WSAWaitForMultipleEvents成功返回,但WSANETWORKEVENTS對象爲空

我沒想到這段代碼能夠工作,因爲我不知道如何初始化變量NetworkEvents。我不必以某種方式將它「綁定」到套接字上嗎?結構應該如何知道它應該存儲ListenSocket的網絡事件?

WSAEVENT event = WSACreateEvent(); 
    WSAEventSelect(ListenSocket, event, FD_CONNECT | FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE); 
    //DWORD cEvents = MAKELONG(MAKEWORD(0, 0), MAKEWORD(0, 1)); 
    WSAEVENT lphEvents[1] = {event}; 
    //lphEvents[0] = event; 
    //DWORD dwTimeout = MAKELONG(MAKEWORD(0, 0), MAKEWORD(0, 0)); 
    WSANETWORKEVENTS NetworkEvents = {0}; 
    int nReturnCode = WSAWaitForMultipleEvents(1, &lphEvents[0], false, WSA_INFINITE, false); 

    if ((NetworkEvents.lNetworkEvents & FD_ACCEPT) == FD_ACCEPT) { 
     std::cout << "EVENT: ACCEPT !" << std::endl; 
    } else { 
     std::cout << "EVENT: NOT ACCEPT !" << std::endl; 
    } 

回答

0

您在WSANETWORKEVENTS NetworkEvents = {0};一切設置爲0,而且也沒有其他地方的代碼,使用的是可變的,所以這將是零,當你檢查結構。

你以後WSAWaitForMultipleEvents

if (WSAEnumNetworkEvents(ListenSocket, event, &NetworkEvents) == SOCKET_ERROR) { 
    printf("WSAEnumNetworkEvents() failed with error %d\n", WSAGetLastError()); 
} 
+0

這就是答案可能缺少WSAEnumNetworkEvents通話。謝謝 ! – Earnie

+0

WSANETWORKEVENTS :: lNetworkEvents可以包含多個事件嗎? – Quest