0

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.

John
  • 1,049
  • 1
  • 14
  • 34

2 Answers2

1

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.

Martin James
  • 24,453
  • 3
  • 36
  • 60
1

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...

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
  • Why the downvote? Why not leave a comment when you downvote so that I can either improve the answer or you can at least explain what you think is wrong. A silent down vote does nothing to improve the quality of the answers. – Len Holgate Apr 30 '15 at 08:01
  • Hi. I asked this question 2 months ago and your answer is to that question. However, I recently updated my question to explain more what I mean (please see it if you have not done that already). I think that your answer is the correct answer to my updated question, but I'm not sure what you mean by: "the completion of a file read", are you assuming that I am using Overlapped IO to read data from a file? (I did not downvoted you BTW :-) – John Apr 30 '15 at 15:55
  • Yes, you can use overlapped I/O to read the file and overlapped I/O to send the data to the network. You can either work on one read at a time (issue a read to the file, when that completes, issue the write to the network (using the same data buffer), when that completes, issue another read to the file (using the same buffer). It's better to use multiple concurrent reads though (but it requires sequencing to ensure that you keep the file read completions in the correct order when sending - the reads will complete in order but if you have more than one thread servicing your thread pool ... – Len Holgate Apr 30 '15 at 16:01