How to properly disconnect from FTP server with FtpWebRequest
Asked Answered
Y

2

9

I've created a ftp client that connects several times during the day to retrieve log files from a FTP server.

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.

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;

How can I close/disconnect the connection properly? Did I forget anything?

Yevetteyew answered 5/6, 2014 at 1:53 Comment(0)
C
14

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;
Cruet answered 5/6, 2014 at 2:39 Comment(1)
When setting the KeepAlive to false, the connection is closed when you call the Close method on the response. So be sure to always call ftpWebResponse.Close() afterwards!Gan
S
1

Have you tried wrapping your response in a using statement?

using (FtpWebResponse response = request.GetResponse() as FtpWebResponse)
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader streamReader = new StreamReader(responseStream))
                {
                    string responseString = streamReader.ReadToEnd();

                    Byte[] buffer = Encoding.UTF8.GetBytes(responseString);
                    memoryStream = new MemoryStream(buffer);
                }

                responseStream.Close();
            }
            response.Close();
        }
Songsongbird answered 5/6, 2014 at 7:7 Comment(4)
AFAIK FtpWebResponse doesn't implement IDisposable interface. I will try add a finally block and close the streams in there.Yevetteyew
Are you sure, MSDN seems to say it does. msdn.microsoft.com/en-us/library/…Songsongbird
Yes, you're right. I was trying to wrap the FtpWebRequest and not the FtpWebResponse.Yevetteyew
I've just tried this, and neither of the Close() calls has the desired effect; my FTP server log shows the connection closing only when my application terminates. Jamleck's answer works though.Caritta

© 2022 - 2024 — McMap. All rights reserved.