Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:30:39+00:00 2026-06-10T23:30:39+00:00

In my socket server I use WSAEventSelect() and WSAEnumNetworkEvents() to detect FD_ACCEPT and FD_CLOSE

  • 0

In my socket server I use WSAEventSelect() and WSAEnumNetworkEvents() to detect FD_ACCEPT and FD_CLOSE events.

Here is the simplified version of my code:

HANDLE sockEv=CreateEvent(NULL,TRUE,FALSE,NULL);
WSAEventSelect(servSocket,sockEv,FD_ACCEPT|FD_CLOSE);
for(;;) {
  if(WSAWaitForMultipleEvents(1,&sockEv,FALSE,INFINITE,FALSE)==WSA_WAIT_EVENT_0) {
    WSANETWORKEVENTS wsaEvents={0};
    WSAEnumNetworkEvents(servSocket,sockEv,&wsaEvents);
    if((wsaEvents.lNetworkEvents & FD_ACCEPT)==FD_ACCEPT) {
      SOCKET clntSock=accept(servSocket,(sockaddr*)clientAddr,&addrlen);
      ....
    }
    else if (wsaEvents.lNetworkEvents==0) {
    // this event occurs after each FD_ACCEPT event
    }
    ....
  }
  ....
}

The problem is that each time a new connection is accepted from a client, I receive 2 events: first with WSANETWORKEVENTS.lNetworkEvents set to FD_ACCEP, then with WSANETWORKEVENTS.lNetworkEvents set to zero. That is the for(;;) loop is executed 2 times each time a new client is accepted. Or maybe the sockEv is not reset by WSAEnumNetworkEvents after detecting FD_ACCEPT.

What does this zero event mean and why it happens?
I didn’t find anything about it in the documentation.

UPDATE: Compilable sample

int _tmain(int argc, _TCHAR* argv[])
{
  WSADATA wsaData;
  WSAStartup(MAKEWORD(2,2),&wsaData);
  try {
    SOCKET servSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    sockaddr_in serv={0};
    serv.sin_family=AF_INET;
    serv.sin_addr.s_addr=htonl(INADDR_ANY);
    serv.sin_port=htons(18081);
    memset(serv.sin_zero,0,sizeof(serv.sin_zero));

    if(bind(servSocket,(sockaddr*)&serv,sizeof(serv))==SOCKET_ERROR)
      throw WSAGetLastError();
    if(listen(servSocket,SOMAXCONN)==SOCKET_ERROR)
      throw WSAGetLastError();
    HANDLE sockEv=CreateEvent(NULL,TRUE,FALSE,NULL);
    if(WSAEventSelect(servSocket,sockEv,FD_ACCEPT|FD_CLOSE)==SOCKET_ERROR) 
      throw WSAGetLastError();

    for(;;) {
      if(WSAWaitForMultipleEvents(1,&sockEv,FALSE,INFINITE,FALSE)!=WSA_WAIT_EVENT_0)
        throw -1;
      WSANETWORKEVENTS wsaEvents={0};
      if(WSAEnumNetworkEvents(servSocket,sockEv,&wsaEvents)==SOCKET_ERROR)
        throw WSAGetLastError();
      if((wsaEvents.lNetworkEvents & FD_ACCEPT)==FD_ACCEPT) {
        SOCKET clntSocket=accept(servSocket,0,0);
        closesocket(clntSocket);
        puts("accept");
      }
      else if(wsaEvents.lNetworkEvents==0) {
        puts("zero event");
      }
    }
  }
  catch(int errorCode) {
    printf("Last Error: %d\n",errorCode);
  }
  WSACleanup();
  return 0;
}

I tried this code on WinXP 32bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:30:41+00:00Added an answer on June 10, 2026 at 11:30 pm

    Answering my own question. It seems I found why and when this happens. This zero event occurs after the client socket (the socket returned by accept()) is closed. It won’t happen if you don’t use FD_CLOSE with WSAEventSelect. So one can just ignore it.
    MSDN says nothing about this behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a server-side socket will fail if I'm trying to use the same port
in server -(multi)client application [TCP]. I use Socket, NetworkStream, StreamReader and StreamWriter for each
In which situration should we use an async socket (either Tcp or Udp) server
The following code runs a socket server on a thread. The client socket sends
I really need to use SSL in my Socket Server\Client Communication but paying for
I have code (my c++ socket server) but I don't know How can I
I have a c# socket based server which serves TCP clients. I use telnet
I am pasting my code for a simple socket server in C and a
I'm writing a socket server (no web-application !) application and want to use method-based
This is my socket server with fork: #include <stdio.h> #include <string.h> #include <stdlib.h> #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.