3

Based on my understanding so far of IOCP, when I create a completion port and associate a socket to it, a notification will be sent to the completion port when a socket is ready to be read.

But how send() is handled, I mean if I want to send data, should I just call send()? what about the completion port, does it get any notification regarding send()?

user4582812
  • 591
  • 1
  • 4
  • 16
  • 2
    With IOCP, it's usual to use overlapped I/O with WSASend/WASRecv to queue up I/O requests. Using send() does not take advantage of IOCP and cannot initiate overlapped I/O requests. – Martin James Feb 21 '15 at 12:41

1 Answers1

6

Based on my understanding so far of IOCP, when I create a completion port and associate a socket to it, a notification will be sent to the completion port when a socket is ready to be read.

NO! One advantage of the IOCP mechanism is that you can queue up read/write requests, with associated buffers, to the kernel and have a kernel threadpool perform the IO operations in kernel state. It's an I/O COMPLETION port, ie. you are notified upon operation completed.

If it's a read, you get your buffer, (pointer), back with the data already loaded - you don't have to explicitly read/copy it again.

If it's a write, the data has already gone and your returned buffer, (pointer). is free to be deleted/reused/repooled/whatever.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • But isn't this question says otherwise: http://stackoverflow.com/questions/28644276/io-completion-ports-and-socket-recv – user4582812 Feb 21 '15 at 12:55
  • I mean when I associate a socket with a completion port, and I want to read, should I call 'recv()'? – user4582812 Feb 21 '15 at 12:56
  • @user4582812 - the answer given, (and accepted), is not relevant to IOCP at all - it is not an answer. – Martin James Feb 21 '15 at 12:57
  • No. Calling recv() is not appropriate for IOCP. You do not need to read upon I/O completion notification because the read operation has already been completed when you receive notification - your buffer is already loaded with received data. – Martin James Feb 21 '15 at 12:59
  • 1
    OK, so after I associate my socket to the completion port, I call WASRecv() (which I think holds my buffer?), and then when I receive a notification, the notification just indicated that the data were read into the buffer I supplied? – user4582812 Feb 21 '15 at 13:02
  • 1
    @user4582812 - OK, now you're getting it:) – Martin James Feb 21 '15 at 13:07