此代码编译并运行。当客户端连接时,WSAWaitForMultipleEvents将返回nReturnCode = 0(等于WSA_WAIT_EVENT_0)。但代码始终显示" EVENT:NOT ACCEPT"当我在调试模式下查看NetworkEvents时,它完全被零填充。
我没想到这段代码能够运行,因为我真的不知道如何初始化变量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 :(得分:0)
你在WSANETWORKEVENTS NetworkEvents = {0};
将所有内容设置为0,并且在其他地方没有使用该变量的代码,所以当你检查那个结构时它会变为零。
您可能在WSAEnumNetworkEvents
WSAWaitForMultipleEvents
的来电
if (WSAEnumNetworkEvents(ListenSocket, event, &NetworkEvents) == SOCKET_ERROR) {
printf("WSAEnumNetworkEvents() failed with error %d\n", WSAGetLastError());
}