Search code examples
c++socketswinapinetwork-programmingiocp

How to initialize WSAOVERLAPPED when using IOCP?


I am not sure how to initialize WSAOVERLAPPED when using IOCP. I don't think that I need to initialize it at all, I only set WSAOVERLAPPED.hEvent to NULL (not sure if this is necessary either). Is this code correct:

// Send data
char buffer[1024];

WSABUF wsaBuf;
wsaBuf.buf = buffer;
wsaBuf.len = 1024;

WSAOVERLAPPED wsaOverlapped;
wsaOverlapped.hEvent = NULL;

WSASend(s, &wsaBuf, 1, NULL, 0, &wsaOverlapped, NULL);

Also, is initializing OVERLAPPED when using WSARecv() the same as initializing WSAOVERLAPPED?


Solution

  •    memset(&wsaOverlapped, 0, sizeof(wsaOverlapped));
    

    Or explicitly set all values to zero.