WSAEnumNetworkEvents

Retrieves the network events that have occurred on a socket and the associated network error codes.

Syntax

int WSAEnumNetworkEvents(
    SOCKET          s,
    WSAEVENT        hEventObject,
    LPWSANETWORKEVENTS lpNetworkEvents
);

Parameters

sType: SOCKET.
Socket handle on which to check for network events.
hEventObjectType: WSAEVENT.
Handle of an event object created by WSACreateEvent that will be signaled when the requested network events occur. Pass NULL if not using an event object.
lpNetworkEventsType: LPWSANETWORKEVENTS.
Pointer to a WSANETWORKEVENTS structure that receives the events and associated error codes.

Return Value

Returns 0 on success. On failure, -1 is returned and a specific error code can be retrieved by calling WSAGetLastError. Common error codes include:

Remarks

The lpNetworkEvents structure contains a set of Boolean flags indicating which events have occurred and, for each, the associated error code (if any). The flags correspond to the event mask specified in a previous call to WSAEventSelect or WSAAsyncSelect.

If hEventObject is non‑NULL, the function also clears the event object after returning the events.

It is recommended to call WSAEnumNetworkEvents inside the event handler that was signaled by WSAEventSelect or inside a message loop for WSAAsyncSelect.

Example

The following example demonstrates how to monitor a socket for readability and closure using WSAEventSelect and WSAEnumNetworkEvents.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main(void) {
    WSADATA wsaData;
    SOCKET s = INVALID_SOCKET;
    WSAEVENT hEvent = WSACreateEvent();
    WSANETWORKEVENTS netEvents;
    int iResult;

    WSAStartup(MAKEWORD(2,2), &wsaData);
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET) { printf("socket failed\n"); return 1; }

    /* Set socket to non‑blocking */
    u_long mode = 1;
    ioctlsocket(s, FIONBIO, &mode);

    /* Request FD_READ and FD_CLOSE events */
    WSAEventSelect(s, hEvent, FD_READ | FD_CLOSE);

    /* Connect to a server (example.com:80) */
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("93.184.216.34"); // example.com
    server.sin_port = htons(80);
    iResult = connect(s, (SOCKADDR*)&server, sizeof(server));
    if (iResult == SOCKET_ERROR) {
        int err = WSAGetLastError();
        if (err != WSAEWOULDBLOCK) { printf("connect failed: %d\n", err); return 1; }
    }

    /* Wait for an event */
    while (true) {
        DWORD dw = WaitForSingleObject(hEvent, INFINITE);
        if (dw == WAIT_OBJECT_0) {
            if (WSAEnumNetworkEvents(s, hEvent, &netEvents) == SOCKET_ERROR) {
                printf("WSAEnumNetworkEvents failed: %d\n", WSAGetLastError());
                break;
            }
            if (netEvents.lNetworkEvents & FD_READ) {
                char buf[512];
                int bytes = recv(s, buf, sizeof(buf)-1, 0);
                if (bytes > 0) {
                    buf[bytes] = '\0';
                    printf("Received: %s\n", buf);
                }
            }
            if (netEvents.lNetworkEvents & FD_CLOSE) {
                printf("Connection closed by remote host.\n");
                break;
            }
        }
    }

    closesocket(s);
    WSACloseEvent(hEvent);
    WSACleanup();
    return 0;
}

See Also