【发布时间】:2014-07-25 20:49:11
【问题描述】:
我创建了一个 ftp 客户端,它在一天中连接数次以从 FTP 服务器检索日志文件。
问题是几个小时后,我从 FTP 服务器收到一条错误消息(已达到 -421 会话限制..)。当我使用 netstat 检查连接时,我可以看到到服务器的多个“已建立”连接,即使我已经“关闭”了连接。
当我尝试通过命令行或 FileZilla 执行相同操作时,连接已正确关闭。
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
如何正确关闭/断开连接?我是不是忘了什么?
【问题讨论】:
标签: c# ftp ftp-client ftpwebrequest