Ever wondered how some of the popular download tools and FTP clients have capability to break download in middle, disconnect from net, later reconnect and restart file transfer from exact point they left earlier. I wanted to do the same for a FTP client I wrote (check this out) but had no clue how??? There is no direct API support from Microsoft. I found some third party support but that costs money. So I have searched here and there and wrote my own. My way may not represents a standard, but this one work with almost every FTP server.
I have been reading stuff from this website in past and thought it is time to pay back. So I decided to put things together and write my first article. Feel free to ask me questions in case I could not do my job well . Also I prepared a working C++ sample to demonstrate my idea. You can write it in any language of your own choice as long you have some way to send & receive FTP commands to server. Feel free to use or change this sample in any way you like a credit is appreciated but not required if you reuse the code.
Most of the FTP server around these days supports RESTART (REST) command. (Check this W3C link to learn more about other FTP commands). Purpose of REST command is to basically tell FTP server to set its marker at which the file transfer has to be restarted.
Let say for example you are downloading a file: MyHugeFooFile.zip.
- Typically send a RETRIEVE (RETR) command for file MyHugeFooFile.zip to the FTP server to start download. Store data received from server to a temp file for example Temp_MyHugeFooFile.zip. So any time download is aborted, size of temp file is the actual useful data transferred so far of MyHugeFooFile.zip.
- Let say all of a sudden ISP connection is broken or FTP server is down or your FTP client crashed or you manually stopped the download.
- Now Next time when you want to restart download, read the size of temp file (i.e. useful data downloaded so far), and seek to the end of the temp file so further download will append the data from end.
- Now send a REST command to FTP server and pass the file size of temp file as parameter. (a typical syntax would be REST 250 if size of temp file was 250). FTP server will send a response stating something like "Restarting from byte 250..".
- Now you can send RETRIEVE (RETR) again for file MyHugeFooFile.zip and FTP server will start sending data from byte offset 251 onwards.
- Now it is your responsibility to append the data coming from server to the incomplete file Temp_MyHugeFooFile.zip and you are all set.
- Once the transfer is complete rename the temp file to the actual file name on WINDOWS using C++ it should typically look like
For upload scenario, conceptually you do something similar but different points are..
- Send a STORE command first.
- Connection breaks.
- Read the size of incomplete temp file at FTP server by either sending SIZE or use your favorite way there could be many.
- Seek to the local file up to the size received from server, so you will start reading and sending data from the location just after.
- Send a REST command. So FTP server will set the marker to start appending data from the location passed in REST command.
- Send STORE again.
- Once done rename the file at FTP server a typical winInet funcion would be
FtpRenameFile(hConnect, Temp_MyHugeFooFile.zip, MyHugeFooFile.zip), or use your favorite way like directly sending RENAME FROM (RNFR) & RENAME TO (RNTO) which are obviously faster, then WinInet.
Three simple steps
- Once connected to FTP server try sending a REST command with 0 ( zero ) as argument.
- If commands fail or server return something like command is not supported. That means this server doesn't support Pause and Resume.
- If FTP server return a response stating something like "Restarting from byte 0" that means server supports Pause & Resume
Now if you just started writing code you may have question, how to send these commands the FTP server. Answer is so many ways. Raw socket is one, WinInet is another. In my sample code I have utilized WinInet's API FtpCommand to send direct commands to FTP server.
I have written this sample in VC++, to demonstrate how we can use WinInet to implement pause and resume, don't look at my coding style for purpose of code review just take it as a sample which I wrote in less then an hour. The code is barely reusable only the idea is reusable, so I kept no restriction on it's usage. First build and run the sample don't forget to link it against the wininet.lib zipped with sample.
This sample demonstrate the download pause and restart download scenario. When you run the sample click the download button to start the download and note the progress bar showing the progress. Stop in between and restart again. Note the download will restart from the location it left off last time. Click Start and as many times as you want until the download is finished. Note that download is hard coded from ftp://ftp.gnu.org/, and file is always find-ls.txt.gz from root directory.
Sample is self explanatory but still if you have any question ask me I will answer as per my best knowledge.