Search code examples
c++socketswinapinetwork-programmingiocp

IO Completion Ports and socket WSARecv()


I am trying to understand how IOCP works with sockets. I need to understand if this is how they work:

I create a completion port, which is nothing but a queue that will receive notifications when some operation completes, and then I associate my socket with it, and then I process incoming notifications.

Now I want to know how this relates to receiving of data from a socket, so when I call WSARecv() what exactly happens, does WSARecv() returns immediately when I call it (does not block) and then later on when data arrives to WSARecv() I get a notification that data were received?


Solution

  • Yes, this is what happens.

    When you call WSARecv(), the function will return immediately (note that you must pass it a buffer to store the received data). Now the system will read the data received from the other end and store it in the supplied buffer. When the system does that, it will place a notification in the completion port to inform you that the read operation has been completed.