473,946 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FTP Connection Closed Error

I have written a Windows service that periodically lists the contents of an
FTP directory until a new file becomes available. Then, it gets the new file
and writes it to the local server. Most of the time, this works fine, but
occasionally I get the following exception while getting the directory
listing:

Message: The underlying connection was closed: An unexpected error occurred
on a receive.
Source: System, Void SyncRequestCall back(System.Obj ect)
Inner Exception:
Stack Trace:
at System.Net.FtpW ebRequest.SyncR equestCallback( Object obj)
at System.Net.FtpW ebRequest.Reque stCallback(Obje ct obj)
at System.Net.Comm andStream.Abort (Exception e)
at System.Net.FtpW ebRequest.Finis hRequestStage(R equestStage stage)
at System.Net.FtpW ebRequest.GetRe sponse()
at RiverFlowBackup .FileTransfer.L istFiles()
at RiverFlowBackup .FileTransfer.G etNameOfLastest File()
at RiverFlowBackup .FileTransfer.W aitForNewFile()
at RiverFlowBackup .RFBService.tim erExecute_Elaps ed(Object sender,
ElapsedEventArg s e)

I'm beginning to think that when I get this error, the request for the
directory list is taking longer than usual and some timeout period of around
15 seconds is causing the connection to close.

I have the KeepAlive property of the FTP object set to FALSE and the TimeOut
property is set to 3 minutes (180000ms). Below is the relevent code:

private ArrayList ListFiles()
{
ArrayList fileNames = new ArrayList();
// Build the FTP request to get the directory listing
FtpWebRequest request =
BuildRequest(We bRequestMethods .Ftp.ListDirect ory);
// Execute the request and get the response from the FTP server
FtpWebResponse response = null;
Stream responseStream = null;
StreamReader reader = null;

try
{
// Get the response from the FTP server
response = (FtpWebResponse )request.GetRes ponse();

// Access the contents of the response
responseStream = response.GetRes ponseStream();
reader = new StreamReader(re sponseStream);

// Read each line of the response contents. Each line
contains one file name.
while (!reader.EndOfS tream)
{
string fileName = reader.ReadLine ();

// Verify that the file name matches the format defined
in the application
// configuration settings. If it does, add it to the
file name array that
// is returned by this function.
Regex ex = new Regex(ftpFileFi lter);
if (ex.IsMatch(fil eName))
fileNames.Add(f ileName);
}
}
finally
{
// Release resources
if (reader != null)
reader.Close();

if (responseStream != null)
responseStream. Close();

if (response != null)
response.Close( );
}

return fileNames;
}
private FtpWebRequest BuildRequest(st ring method)
{
return this.BuildReque st(method, String.Empty);
}
private FtpWebRequest BuildRequest(st ring method, string fileName)
{
// Build the FTP request object
string filePath = ftpPath + fileName;
FtpWebRequest request =
(FtpWebRequest) FtpWebRequest.C reate(filePath) ;

request.Method = method;
request.Credent ials = new NetworkCredenti al(ftpUserName,
ftpPassword);
request.Timeout = ftpTimeout;
request.CachePo licy = new
System.Net.Cach e.RequestCacheP olicy(System.Ne t.Cache.Request CacheLevel.Bypa ssCache);
request.KeepAli ve = false;
request.UseBina ry = (ftpTransferTyp e == TransferTypeEnu m.Binary);
request.Proxy = null;

return request;
}
Thank you in advance for your help.
Aug 6 '08 #1
0 1355

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1912
by: Larry | last post by:
Hi I have asp.net programs. I used a very simple data transfer method by using URLs ¡°First.aspx¡± contents a line to send data to the ¡°second.aspx¡± page, for
1
3239
by: Sean Nolan | last post by:
We have implemented unhandled error trapping at the application level and log these errors to our database. One error, however, the does not get trapped is when the connection pool has exceeded the max number of connections. Obviously, we need to find the place(s) in our code where connections are not closed correctly (espcially in loops), but I'm wondering if it's possible to trap this error and to find out which part of our code (i.e....
0
18472
by: BGS | last post by:
I have a web site (www.on-the-matrix.com) that displays photos in a "slide show" format on ASPX pages that run in an inline frame on an ASP page (or in a separate pop-up window). The ASPX pages use a hidden form to transmit information back to the server so it will know which is the next photo to load. All of this works as intended in modern versions of Internet Explorer, Netscape and Mozilla. With Opera, however, users receive a...
18
3266
by: Rob Nicholson | last post by:
We're getting an occasional occurrence of the following error when two users try and open the same record in our ASP.NET app: "There is already an open DataReader associated with this Connection which must be closed first." As suggested, I was closing the connection in the Finally part of the outer Try but I wasn't closing the data reader as well so I assume that if the following happens, the above error could occur
15
8303
by: Rob Nicholson | last post by:
I'm starting to worry a bit now. We're getting the above error when two users hit the same database/page on an ASP.NET application using ADO.NET, talking to a SQL 7 server. The error is perfectly repeatable :-( But this should help! The error is occurring inside ExecuteReader which uses a DataReader internally. Here are some things that I'm pretty sure it's *NOT*:
26
16554
by: Rajeev Tipnis | last post by:
http://support.microsoft.com/default.aspx?scid=kb;en- us;819450 Questions: 1) Is this patch (fix) applicable to the .NET 1.1 Framework as well? That is, if we have Framework 1.1 (On Win2K3/IIS6.0), do we still need this patch? 2) Does it specifically solve only this error: The underlying connection was closed. An unexpected error
0
6913
bartonc
by: bartonc | last post by:
This is a work in progress (current and active). There are some issues to resolve in supporting multiple connection types and I plan to add PySQLite to the mix. The this update is due to my suggesting the accompanying helper functions in a recent post and noticing that the ones posted here are a bit out of date. I'll post those helpers in a separate thread. I've also been working with the following database engines: MySQL 5.x SQLite3 JET...
4
21815
by: =?Utf-8?B?QXJuYXVkIFZpc2N1c2k=?= | last post by:
Hi all, we meet a problem since we use WCF to consume java WebServices. Often the first call works then the second raises the following error : "The underlying connection was closed : " We use a basicHttpBinding over HTTPS to connect to WebService. The configuration is the following <binding name="SoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"
4
4589
by: Sin Jeong-hun | last post by:
I don't get the message so it's hard to debug that, but some of my clients report that they get "The underlying connection was closed unexpectedly" exception. According to this site (http:// www.dotnetspider.com/resources/2596-e-underlying-connection-was-closed-A-connection.aspx), it's a bug of .NET 2.0, and the author suggests that we use KeepAlive=false until Microsoft fixes it. It seems like almost 5 years have passes since the...
0
10159
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9981
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11337
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8250
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6331
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4940
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3541
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.