我怎么知道在C#.net中下载文件时FTP断开连接 [英] how i know when FTP is disconnected while downloading file in C#.net

查看:77
本文介绍了我怎么知道在C#.net中下载文件时FTP断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我从不同的ftp服务器下载了三个文件,而下载一台服务器已关闭,但我没有收到异常

First of all i download a three files from different ftp servers while downloading one server is shutdown but i didn''t get Exception

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.");
          }




在此先感谢
kalai




Thanks in Advance
kalai

推荐答案

这与FTP在TCP/IP上的工作方式有关.在开始的时候,interwebs的网络不是很可靠,与断开连接相比,断开连接的频率更高.因此,TCP被设计为非常强大且具有故障保护功能.如果您拔下PC上的插头(当然是网络插头),则大约需要2小时,直到TCP最终发出故障信号.因此,如果您等待足够长的时间,最终您将超时.
建立连接时的超时时间较小,但是一旦连接建立并且正在进行传输,您将不得不等待更长的时间.

希望能为您解释.

干杯!
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!


这篇关于我怎么知道在C#.net中下载文件时FTP断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆