Failure writing network stream

Jul 3, 2013 at 12:23 PM
I cannot resolve this issue. Downloading a 65MB text file keeps giving this error message and stops the download process. I have tried with different buffer sizes from 1K to 32K but the result is the same. At 3/4 of the download process (not always at the same point) I receive an FtpCommandException with the above mentioned message. The code used is the following.
    private int Download(string file)
        int totalRead = 0;
            _logger.Info("Trying to download remote file {0} in {1} local directory", file, _targetPath);
            using (FtpClient conn = new FtpClient())
                conn.SocketKeepAlive = true;
                conn.SocketPollInterval = 30000;
                conn.ReadTimeout = 60000;
                conn.Host = _ftpServer;
                conn.Credentials = new NetworkCredential(_ftpUsername, _ftpPassword);
                using (FileStream writeStream = new FileStream(Path.Combine(_targetPath, file), FileMode.Create))
                using (Stream downloadStream = conn.OpenRead(file))
                    int bufLen = 32768;
                    byte[] buffer = new byte[bufLen];
                    int bytesRead = downloadStream.Read(buffer, 0, bufLen);
                    while (bytesRead > 0)
                        writeStream.Write(buffer, 0, bytesRead);
                        _logger.Info("Write bytes:{0:D6}", bytesRead);
                        bytesRead = downloadStream.Read(buffer, 0, bufLen);
                        if (bytesRead == 0) break;
                        totalRead += bytesRead;
                        _logger.Info("Read: {0:D6} bytes Total:{1:D10}", bytesRead, totalRead);
                _logger.Info("File " + file + " correctly downloaded into " + _targetPath + " directory");
            return totalRead;
        catch (Exception ex)
            _logger.Error("Download Error :" + ex.Message);
            return 0;
Jul 3, 2013 at 1:40 PM
Could you please provide a full stack trace so we can see where the exception is occurring? Also a transaction log per the instructions in Examples\Debug.cs. Sounds like a network issue, connection drop maybe, that looks like a pretty generic .net error.
Jul 3, 2013 at 1:44 PM
Also worth trying to download the file using some other FTP client to see if it fails too. I think it's probably a network related issue but could be wrong.
Jul 3, 2013 at 9:40 PM
Edited Jul 3, 2013 at 9:53 PM
Thanks for your attention jptrosclair. And excuse me for the delay. Put on hold this thread because I had tried your second advice and also with an external utility (FileZilla) I have the same problem. FileZilla says that the remote server has closed the connection. So now I am asking the sysadmin to check on their side. Will keep you informed and thanks again