tags:

views:

194

answers:

4

hi

im doing projects in ftp and remoting,i transfer files very often ,while sending file if network failed the program must wait untill the connection got resumed ,and after resuming,it must transfer the file from the place where the last byte was written.how to do it any ideas.

so what should i do i will go n check the file and get the size subtract it from the total size and starts copying frm the result size. will it works

A: 

You should use a method like wget which is able to resume interrupted transfers.

Is the tag c# really a requirement here?

Sorry, can't help with c#

EDIT: and yes, it's true what Jon said.

The resumed transfer won't actually start with the next byte. There will be a quick check that the previous part was correctly transferred.

pavium
Thanks Jon ... could you do ALL my links? ;-D
pavium
Sure thing - my rates are very reasonable ;-)
Jon Cage
A: 

It's not a C# solution, but Getright does this very well.

Programmatically, for writing files, I would think that to resume a transfer you'd need to connect in, read the file (or in some other way, identify how big it is) and then open it and append the remaining data. It might be worth checking that the last N bytes in the existing file match those in your local file. Reading would be similar - skip to a specific part of the file and start reading back.

[Edit] If you've managed to establish an FTP connection, the command you need is REST:

RESTART (REST)

The argument field represents the server marker at which file transfer is to be restarted. This command does not cause file transfer but skips over the file to the specified data checkpoint. This command shall be immediately followed by the appropriate FTP service command which shall cause file transfer to resume.

I'm sure there are a few simple C# ftp client libraries you could use if you've not already found/written one.

Jon Cage
im a windows programmer shall i try it in c#
karthik
A: 
FileInfo finfo = new FileInfo("E:\\butterfly.mpg");
                FileInfo sourceinfo = new FileInfo("H:\\butterfly.mpg");
                int sourceLength = 0;
                sourceLength = (int)sourceinfo.Length;
                int totalLength = (int)finfo.Length;
                int RemaingDatalength = 0;
                RemaingDatalength = sourceLength - totalLength;
                int appLength = 0;
                int Remaining = 0;
                int bufferLength = 12488;
                byte[] buffer = new byte[bufferLength];
                int pointer = 1;
                FileStream fsa = new FileStream("E:\\butterfly.mpg", FileMode.Append);
                fss = new FileStream("H:\\butterfly.mpg", FileMode.Open);
                while (pointer != 0)
                {
                    fss.Position = (long)totalLength;
                    fss.Read(buffer, 0, bufferLength);
                    totalLength += bufferLength;
                    appLength += bufferLength;
                    fsa.Write(buffer, 0, bufferLength);
                    Remaining = RemaingDatalength - appLength;
                    if (Remaining < bufferLength)
                    {
                        byte[] buff = new byte[Remaining];
                        fss.Read(buff, 0, Remaining);
                        fsa.Write(buff, 0, Remaining);
                        break;
                    }
                }
                fss.Close();
                fsa.Close();
                MessageBox.Show("Filetransfer Completed");

this is the method i used while im sending file through remoting.now im trying this in FTP

karthik
You should be careful with your fss.Read(buffer,0,bufferLength). If the amount you read is not exactly bufferLength (which it could be if the file is small, or you start near the end) then you write a whole buffer, but only have read in some of the buffer. fss.Read returns the actual number of bytes read, so you should use that in the length calculations afterwards.
Matt
A: 

There's an open source C# FTP library here.

The API states that the Put method "Allows appending if current file exists." via a boolean parameter. This is probably what you want. All you'll need to do is retry the upload until you get a successful transfer event.

Catchwa