Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First of all i download a three files from different ftp servers while downloading one server is shutdown but i didn't get Exception
MIDL
try
{
Stream strLocal;
                WebProxy wp = new WebProxy();
                wp.UseDefaultCredentials = true;//Create FTP request 
                FtpWebRequest request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
                request.Proxy = wp;
                request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Proxy = wp;
                request.ServicePoint.ConnectionLimit = 10;
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false; //close the connection when done
                request.ContentOffset = StartPoint;
                //Stream reader = request.GetRequestStreamAsync();
                Stream reader = request.GetResponse().GetResponseStream();
                byte[] buffer = new byte[1048576]; //downloads in chuncks
                if (!File.Exists(filename))
                {
                    strLocal = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                }
                else
                {
                    File.Delete(filename);
                    strLocal = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);

                }
                int bytesSize = 0;
                while ((bytesSize = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                     
                    if ((strLocal.Length + bytesSize) < length)
                    {
                        strLocal.Write(buffer, 0, bytesSize);
                        this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, length, i, Partfilename, bytesSize });
                    }
                    else
                    {
                        long Valtowrite = (strLocal.Length + bytesSize) - length;
                        Valtowrite = bytesSize - Valtowrite;
                        strLocal.Write(buffer, 0, (int)Valtowrite);
                        this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, length, i, Partfilename, bytesSize });
                        break;
                    }

                }

                strLocal.Close();
}
catch (System.Net.WebException ex)
          {
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message, "There was an error connecting to the FTP Server.");
          }



Thanks in Advance
kalai
Posted
Updated 14-Mar-11 4:38am
v4

1 solution

That has to do with the way FTP works over TCP/IP. In the starting days of the interwebs lines weren't very reliable and bound to be disconnected more often than being up. So TCP was designed to very robust and failsafe. If you pull the plug (the network plug of course) on your PC it will take something along 2h until TCP will finally signal a failure. So if you wait long enough eventually you'll get a timeout.
The timeout while establishing a connection is smaller, but once the connection is up and the transfer is under way you'll have to wait a little longer.

Hope that explains it for you.

Cheers!
 
Share this answer
 
v2
Comments
Espen Harlinn 17-Mar-11 10:00am    
Good reply! 5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900