問題描述
我創(chuàng)建了一個 ftp 客戶端,它在一天中連接數(shù)次以從 FTP 服務(wù)器檢索日志文件.
I've created a ftp client that connects several times during the day to retrieve log files from a FTP server.
問題是幾個小時后,我從 FTP 服務(wù)器收到一條錯誤消息(已達到 -421 會話限制..).當我使用 netstat 檢查連接時,我可以看到到服務(wù)器的多個已建立"連接,即使我已經(jīng)關(guān)閉"了連接.
The Problem is that after a few hours I am getting an error message from the FTP server (-421 session limit reached..). When I check the connections with netstat, I can see several 'ESTABLISHED' connections to the server even though I've "closed" the connection.
當我嘗試通過命令行或 FileZilla 執(zhí)行相同操作時,連接已正確關(guān)閉.
When I try to do the same over the command line or FileZilla, the connections are properly closed.
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;
如何正確關(guān)閉/斷開連接?我是不是忘了什么?
How can I close/disconnect the connection properly? Did I forget anything?
推薦答案
嘗試設(shè)置 FtpWebRequest.KeepAlive 屬性為 false.如果 KeepAlive
設(shè)置為 false,則在請求完成時將關(guān)閉與服務(wù)器的控制連接.
Try and set the FtpWebRequest.KeepAlive property to false. If KeepAlive
is set to false, then the control connection to the server will be closed when the request completes.
ftpWebRequest.KeepAlive = false;
這篇關(guān)于如何使用 FtpWebRequest 正確斷開與 FTP 服務(wù)器的連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!