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?

user797717
  • 758
  • 1
  • 6
  • 18

2 Answers2

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;
Jamleck
  • 1,017
  • 7
  • 12
  • 2
    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! – Frederick Eskens Jun 17 '20 at 09:07
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();
        }
Nattrass
  • 1,283
  • 16
  • 27
  • 1
    AFAIK FtpWebResponse doesn't implement IDisposable interface. I will try add a finally block and close the streams in there. – user797717 Jun 05 '14 at 15:41
  • Are you sure, MSDN seems to say it does. http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse(v=vs.110).aspx – Nattrass Jun 05 '14 at 17:56
  • Yes, you're right. I was trying to wrap the FtpWebRequest and not the FtpWebResponse. – user797717 Jun 06 '14 at 12:51
  • 1
    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. – Max Barraclough Mar 15 '18 at 14:43