Click here to Skip to main content
Click here to Skip to main content
Go to top

Pause & Resume at Protocol level. (You can implement it in any language)

, 9 Oct 2002
Rate this:
Please Sign up or sign in to vote.
Ever wondered how some of the popular download tools have capability to break download in middle, disconnect from net, later reconnect and restart file transfer from exact point they left earlier.

Sample Image - pauseresume.jpg

Introduction

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 Smile | :) . 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.

Contents

Some theory to warm up

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.

Programmatically starting download pausing and then restarting again

Let say for example you are downloading a file: MyHugeFooFile.zip.

  1. 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.
  2. 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.
  3. 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.
  4. 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..".
  5. Now you can send RETRIEVE (RETR) again for file MyHugeFooFile.zip and FTP server will start sending data from byte offset 251 onwards.
  6. Now it is your responsibility to append the data coming from server to the incomplete file Temp_MyHugeFooFile.zip and you are all set.
  7. Once the transfer is complete rename the temp file to the actual file name on WINDOWS using C++ it should typically look like RenameFile(Temp_MyHugeFooFile.zip, MyHugeFooFile.zip); Done.

Programmatically starting upload pausing and then restarting again

For upload scenario, conceptually you do something similar but different points are..

  1. Send a STORE command first.
  2. Connection breaks.
  3. Read the size of incomplete temp file at FTP server by either sending SIZE or use your favorite way there could be many.
  4. 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.
  5. Send a REST command. So FTP server will set the marker to start appending data from the location passed in REST command.
  6. Send STORE again.
  7. 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.
  8. Done.

Programmatically determining if a particular FTP server supports Pause & Resume

Three simple steps

  1. Once connected to FTP server try sending a REST command with 0 ( zero ) as argument.
  2. If commands fail or server return something like command is not supported. That means this server doesn't support Pause and Resume.
  3. 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.

About the C+ Sample

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Narendra Chandel
Web Developer
United States United States
To know more about Narendra's professional work one can check his profile below:
 
http://www.linkedin.com/in/narendrachandel
Follow on   LinkedIn

Comments and Discussions

 
QuestionHow to make the Code work on a HTTP Server Pinmembersureskoleti24-Jul-09 2:57 
Questionhow about http Pinmembermonsieur_jj30-Apr-08 19:54 
GeneralIt works for me Pinmember1thing26-Sep-07 7:11 
Do not use FtpOpenFile, instead use FtpCommand:
string strSeekOffsetCMD = "TYPE I\r\nREST " + (string)pszOffset + "\r\nRETR " + strFileNameAtServer + "\r\n";
 
bRet = ::FtpCommand(hConnect, // WinInet Connection handle
TRUE, // Yes, I expect a response
FTP_TRANSFER_TYPE_BINARY, // I'm receiving image
strSeekOffsetCMD.c_str(), // This is the FTP command I am passing
0, // No context needed
&hOpenFile); // The handle to read the response

 

QuestionDoesn't support IIS Pinmembermsrameshkumar18-Apr-07 0:39 
Questionwhy not? Pinmemberelfederico4-Feb-07 6:06 
QuestionHow to make the Code work on a HTTP Server Pinmemberprabu.s2-Feb-06 22:38 
AnswerRe: How to make the Code work on a HTTP Server PinmemberYuvi Panda23-Apr-06 22:42 
GeneralRe: How to make the Code work on a HTTP Server PinmemberKhinLLK18-Mar-07 16:24 
Questionsource code in vb6? PinmemberSewH25-Jan-06 20:06 
QuestionDownload bedug?? Pinmemberwang-yuan-sheng19-Oct-05 0:44 
Questionit doesn't work, why? Pinmemberliuty200626-May-05 18:22 
QuestionMy question: Unknow error ??? Pinmemberthangnvhl28-Apr-05 15:29 
AnswerRe: My question: Unknow error ??? Pinsussthangnvhl17-May-05 16:41 
AnswerRe: My question: Unknow error ??? Pinsussthangnvhl17-May-05 16:41 
GeneralUpload PinmemberKrouer23-May-04 21:26 
GeneralServ-U Upload Resume Pinmemberseymen13-Mar-04 19:49 
Generalask rest command?why not Pinmembersmalldragon7713-Apr-03 22:23 
Generalask rest command?why not Pinmemberffffffffffffffffffffffffffff13-Apr-03 22:21 
GeneralIt does not work on standart FTP server PinmemberDmitry Rudnitsky8-Mar-03 3:39 
GeneralRe: It does not work on standart FTP server Pinmembervasilica9-Apr-03 4:07 
GeneralCan´t get it to work on other servers PinmemberJonatan Dahl11-Feb-03 7:55 
GeneralRe: Can´t get it to work on other servers PinmemberNarendra Chandel18-Feb-03 19:19 
GeneralRe: Can´t get it to work on other servers PinmemberJonatan Dahl20-Feb-03 11:50 
GeneralRe: Can´t get it to work on other servers PinmemberSlackbot2-Jun-03 11:13 
GeneralRe: Can´t get it to work on other servers Pinmemberrwilchek@att.net15-Nov-03 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 10 Oct 2002
Article Copyright 2002 by Narendra Chandel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid