简体   繁体   中英

How to send a file using IOCP?

When using blocking sockets, all I had to do to send a file was to open the file and loop through it and send it in chunks.

But I find sending a file using overlapped sockets to be more challenging. I can think of the following approach to do it:

  1. I open the file and send the first chunk, and I keep track of the file handle and file position (I store these data somewhere in memory).
  2. Now when I get a completion packet indicating that some data has been sent, I retrieve the file handle and file position and send the next chunk.
  3. I repeat step 2 until I reach the last chunk in the file, and then I close the file.

Is this approach correct?

Note: I don't want to use TransmitFile().


Edit: I have updated my question.

Easiest way: look up 'TransmitFile' on MSDN. This functionality is so common, (eg. serving up web pages), that there is a specific API for it.

If you don't want to use TransmitFile() then you can use overlapped file I/O using IOCP where the completion of a file read is used to trigger a socket write and the completion of a socket write is used to trigger a file read. You then decide how much data you want in transit and issue that many file reads and wait for EOF...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM