1

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?

user4582812
  • 591
  • 1
  • 4
  • 16

2 Answers2

1

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.

John
  • 1,049
  • 1
  • 14
  • 34
0

If want to know more about IOCP, WSARecv() and Sockets, please see my sample project I made to educate myself about the subject.

Click here to navigate to Socket Completion Server code

You will have to get familiar with GetQueuedCompletionStatus() which sits inside a thread loop waiting for an event to return. Because it goes into block mode when there is nothing in the queue, the thread will also behave like it is in wait.

Once GetQueuedCompletionStatus() returns it is now unblocked, the process moves forward evaluating the content of the buffer. Simply fill-in with your code how you want to process the buffer.

With respect to WSARecv(), calling the function will trigger the socket API to fill the buffer with data. Since it is Overlapped, WSARecv() will not block and will return right away. There is a way of knowing when the completion event is raised by simply calling GetQueuedCompletionStatus().

benG
  • 82
  • 9