Mai Hu Na

Mai Hu Na

  • NA
  • 86
  • 133.6k

underlying connection was closed An unexpected error occure

Mar 18 2013 9:24 AM
hi all,
       
        i received error: 
" The underlying connection was closed: An unexpected error occurred on a receive"
 
while uploading large files (ie 100MB) from one server to another I found file copying at destination
but at the time of below statement I gor error:

 
reqStream.Close();


and also suggest why after copying file it take long time to close request stream and give above Error plz help.
my code snipet is


  public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
  {
  try
  {
  // Get the local file name: C:\Users\Rhyous\Desktop\File1.zip
  // and get just the filename: File1.zip. This is so we can add it
  // to the full URI.
  String filename = Path.GetFileName(_FileName);
 
  // Open a request using the full URI, c/file.ext
  FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(_UploadPath);
 
  // Configure the connection request
  request.Method = WebRequestMethods.Ftp.UploadFile;
  request.Credentials = new NetworkCredential(_FTPUser, _FTPPass);
  request.UsePassive = true;
  request.UseBinary = true;
  request.KeepAlive = false;
 
  // Create a stream from the file
  FileStream stream = File.OpenRead(_FileName);
  byte[] buffer = new byte[stream.Length];
 
  // Read the file into the a local stream
  stream.Read(buffer, 0, buffer.Length);
 
  // Close the local stream
  stream.Close();
 

  // Create a stream to the FTP server
  Stream reqStream = request.GetRequestStream();
 
  // Write the local stream to the FTP stream
  // 1MB bytes at a time
  int offset = 0;
  int chunk = (buffer.Length > 1048576) ? 1048576: buffer.Length;
  while (offset < buffer.Length)
  {
  reqStream.Write(buffer, offset, chunk);
  offset += chunk;
  chunk = (buffer.Length - offset < chunk) ? (buffer.Length - offset) : chunk;
  }
  // Close the stream to the FTP server
  reqStream.Close();
  }
 
  catch (Exception ex)
  {
  Console.WriteLine(ex.Message, "Upload Error");
  }
  }





Answers (2)