Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
5.2k views
in .NET FTP by (1.9k points)
Hi,

I am having many errors lately with the following exception description:


System.IO.IOException was caught
Message="Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
Source="System"
StackTrace:
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
at EnterpriseDT.Net.Ftp.FTPControlSocket.ReadReply()
at EnterpriseDT.Net.Ftp.FTPClient.ValidateTransfer()
at EnterpriseDT.Net.Ftp.FTPClient.ValidateTransferOnError()
at EnterpriseDT.Net.Ftp.FTPClient.Put(String localPath, String remoteFile, Boolean append)
at EnterpriseDT.Net.Ftp.FTPConnection.UploadFile(String localPath, String remoteFile, Boolean append)
at EnterpriseDT.Net.Ftp.FTPConnection.UploadFile(String localPath, String remoteFile)
at FTPc.frmMain.UploadFile(Object paths)



The ftp servers are fine and I can ftp to any of them. It seemed to me at first that this is a time-out error. But the servers are responding properly with the a normal ftp client.

Now to also help below is an extract from FTPCoonection log file. I am setting log level to ERROR.


ERROR [FTPClient] 17 Nov 2007 22:27:17.671 : Caught exception : System.IO.IOException: Unable to write data to the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
System.IO.IOException: Unable to write data to the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.BufferedStream.Write(Byte[] array, Int32 offset, Int32 count)
at EnterpriseDT.Net.Ftp.FTPClient.a(Stream A_0, String A_1, Boolean A_2, Boolean A_3)
CAUSED BY:
System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)

9 Answers

0 votes
by (162k points)
This can occur if you are doing many transfers from the one application within a short period of time. You end up running out of network resources (i.e. sockets) and can't do much until the OS reclaims the used ones (each transfer or listing consumes one socket). Applications have a limited number of sockets. This is a limitation of the FTP protocol. There are various ways to deal with this - perhaps give us an idea of what you are trying to do.

If you are not doing many transfers, there is probably another explanation.
0 votes
by (1.9k points)
Hi Bruce,

Yes you are right. This is what my application does. It basically sends 30 files to 40 ftp servers simultaneously. My code loops through a collection of files and in each iteration it uploads 30 files. The thing is, it used to work and never gave me an error. It is strange that this is happening now.

For each Svr as string in MyServers()

  For each f as string in MyFiles()

   Dim FtpConn as new Net.Ftp.ExFTPConnection
   FtpConn.UserName="user"
   FtpConn.Password="pass"
   FtpConn.Serveraddress=Svr
   FtpConn.Connect
   FtpConn.UploadFile(f,IO.Path.GetFileName(f))
   FtpConn.Close

  Next f

Next Svr


Is this wrong? If so, how can I get around that?

Thanks
0 votes
by (162k points)
Move FtpConn.Connect to the outer loops, and also FtpConn.Close

That way you only connect to each server once instead of 30 times.
0 votes
by (1.9k points)
Hello,
I see what you are saying. But this will mean that I will only use one connection per server and then send all files sequentially. My target is to open a separate connection for each file so as to make things run in parallel. This way total ETA for files will significantly decrease. The remote servers are vsFTPd 2.0.5

Any ideas ?
0 votes
by (162k points)
Given that your code isn't asynchronous or using multiple threads, it is sequential no matter how you do it.

If you want to do things in parallel, allocate a thread per connection and upload simultaneously, or use a thread pool.

The main thing is use one connection per server, and upload all the files on that one connection without disconnecting and reconnecting - that should minimize any socket problems.

If you want to use asynchronous methods, consider using edtFTPnet/Express, which supports them along with directory transfers, proxies, directory synchronization (next release) etc.
0 votes
by (1.9k points)
Hello,

I edited the code to be be asynchronous:

For each Svr as string in MyServers()

  For each f as string in MyFiles()

    ThreadPool.QueueUserWorkItem(new WaitCallback(AddressOf UploadFile),f)

  Next f

Next Svr


Private Sub UploadFile (ByVal FilePath as string)

   Dim FtpConn as new Net.Ftp.ExFTPConnection
   FtpConn.UserName="user"
   FtpConn.Password="pass"
   FtpConn.Serveraddress=Svr
   FtpConn.Connect
   FtpConn.UploadFile(f,IO.Path.GetFileName(f))
   FtpConn.Close

End Sub


Now I am still having the same problem since as per your advice I am still using a connection per file. How can I adjust the above code to accept sending files over the same connection?
0 votes
by (162k points)
Change the UploadFile method to UploadFiles, and connect once, upload all files and disconnect in that method.

Kick off UploadFiles asynchronously for each server.
0 votes
by (1.9k points)
Ok, good idea.. Now in terms of time, I was also thinking of looping through files rather than servers. Then open a new connection to each server, send that file, and then disconnect. Will that go through or give the sockets error? Also will that save time or will just take the same time as sending all files to each server simultaneously as you suggested?

Thanks
0 votes
by (162k points)
Just do what I suggest in the previous post. If that works tweak as you like.

Categories

...