Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
4.64/5 (19 votes)
9 Oct 20025 min read 195.9K   5.5K   82   43
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 :). 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


Written By
Software Developer (Senior)
United States United States
To know more about Narendra's professional work one can check his profile below:

http://www.linkedin.com/in/narendrachandel

Comments and Discussions

 
QuestionHow to make the Code work on a HTTP Server Pin
sureskoleti24-Jul-09 2:57
sureskoleti24-Jul-09 2:57 
Questionhow about http Pin
monsieur_jj30-Apr-08 19:54
monsieur_jj30-Apr-08 19:54 
GeneralIt works for me Pin
1thing26-Sep-07 7:11
1thing26-Sep-07 7:11 
QuestionDoesn't support IIS Pin
msrameshkumar18-Apr-07 0:39
msrameshkumar18-Apr-07 0:39 
Questionwhy not? Pin
elfederico4-Feb-07 6:06
elfederico4-Feb-07 6:06 
QuestionHow to make the Code work on a HTTP Server Pin
prabu.s2-Feb-06 22:38
prabu.s2-Feb-06 22:38 
AnswerRe: How to make the Code work on a HTTP Server Pin
Yuvi Panda23-Apr-06 22:42
Yuvi Panda23-Apr-06 22:42 
GeneralRe: How to make the Code work on a HTTP Server Pin
KhinLLK18-Mar-07 16:24
KhinLLK18-Mar-07 16:24 
Questionsource code in vb6? Pin
SewH25-Jan-06 20:06
SewH25-Jan-06 20:06 
QuestionDownload bedug?? Pin
wang-yuan-sheng19-Oct-05 0:44
wang-yuan-sheng19-Oct-05 0:44 
Questionit doesn't work, why? Pin
liuty200626-May-05 18:22
liuty200626-May-05 18:22 
QuestionMy question: Unknow error ??? Pin
thangnvhl28-Apr-05 15:29
thangnvhl28-Apr-05 15:29 
AnswerRe: My question: Unknow error ??? Pin
thangnvhl17-May-05 16:41
thangnvhl17-May-05 16:41 
AnswerRe: My question: Unknow error ??? Pin
thangnvhl17-May-05 16:41
thangnvhl17-May-05 16:41 
GeneralUpload Pin
Krouer23-May-04 21:26
Krouer23-May-04 21:26 
GeneralServ-U Upload Resume Pin
seymen13-Mar-04 19:49
seymen13-Mar-04 19:49 
Generalask rest command?why not Pin
Member 32899313-Apr-03 22:23
Member 32899313-Apr-03 22:23 
Generalask rest command?why not Pin
Member 32899313-Apr-03 22:21
Member 32899313-Apr-03 22:21 
GeneralIt does not work on standart FTP server Pin
Dmitry Rudnitsky8-Mar-03 3:39
Dmitry Rudnitsky8-Mar-03 3:39 
GeneralRe: It does not work on standart FTP server Pin
dandinpantelimon9-Apr-03 4:07
dandinpantelimon9-Apr-03 4:07 
GeneralCan´t get it to work on other servers Pin
Jonatan Dahl11-Feb-03 7:55
Jonatan Dahl11-Feb-03 7:55 
GeneralRe: Can´t get it to work on other servers Pin
Narendra Chandel18-Feb-03 19:19
Narendra Chandel18-Feb-03 19:19 
Hi, sorry for the delay in reply.
Well I have tried with a lot of servers and it seems to be working in most of the cases I have not tried particularly at ftp.microsoft.com but you have to the source code. Why don't you try to debug the code and find out if there is something wrong with the code. I am sure there is nothing wrong with the logic as it is working with most of other servers, there could be differences in responses at Microsoft server itself which will be interesting to know.
Try debugging it.
Thanks!!
Narendra Chandel


__NarendraC
GeneralRe: Can´t get it to work on other servers Pin
Jonatan Dahl20-Feb-03 11:50
Jonatan Dahl20-Feb-03 11:50 
GeneralRe: Can´t get it to work on other servers Pin
Slackbot2-Jun-03 11:13
Slackbot2-Jun-03 11:13 
GeneralRe: Can´t get it to work on other servers Pin
rwilchek@att.net15-Nov-03 3:58
rwilchek@att.net15-Nov-03 3:58 

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

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