Hi All
I have written a program that only downloads a part of the file using SFTP and adds it to the previously downloaded file. Basically resumes download.
Program works perfectly except when I am testing varies conditions for example losing connection ect.
This code is running in a timer.
To reproduce the error:
1. Put a break on "remote.CopyTo(fs, (int)sftp.BufferSize);"
2. Put a break on "catch (System.Net.Sockets.SocketException)"
3. Start timer to run code.
4. As the copy process starts remove network cable (trying to simulate losing connection).
5. Exception will be thrown "System.Net.Sockets.SocketException" after awhile.
6. Plug network cable in.
7. The code will try run again when the timer is triggered and after a random time the exception "System.NullReferenceException" will be thrown(Doesnt take long).
Full Error:
An unhandled exception of type 'System.NullReferenceException' occurred in Renci.SshNet.dll
Additional information: Object reference not set to an instance of an object.
If I break then I see these lines:
Call stack with external code
Renci.SshNet.dll!Renci.SshNet.Sftp.SubsystemSession.SendData(byte[] data)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.SendMessage(Renci.SshNet.Sftp.SftpMessage sftpMessage)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.SendRequest(Renci.SshNet.Sftp.Requests.SftpRequest request)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.RequestClose(byte[] handle)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpFileStream.Dispose(bool disposing)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpFileStream.Finalize()
[Native to Managed Transition]
My Code
```
private int Download(string IP, string UserName, string Password, string FileSource, string Num)
{
int fault = 0;
SftpClient sftp = null;
try
{
using (sftp = new SftpClient(IP, UserName, Password))
{
Logging.Log_To_file("SFTP connecting");
sftp.Connect();
Logging.Log_To_file("SFTP ok");
Int64 length;
using (FileStream check = File.OpenWrite(root + num+ @"\" + OldFile))
{
length = check.Length;
}
using (FileStream fs = File.Create(root + num+ @"\" + PartFile))
{
using (SftpFileStream remote = sftp.OpenRead(FileSource))
{
Int64 length2 = remote.Length;
if (length > length2)
{
remote.CopyTo(fs, (int)sftp.BufferSize);
}
else
{
remote.Seek(length, SeekOrigin.Begin);
remote.CopyTo(fs, (int)sftp.BufferSize);
}
}
}
Logging.Log_To_file("SFTP disconnecting...");
sftp.Disconnect();
Logging.Log_To_file("SFTP Done");
sftp.Dispose();
Logging.Log_To_file("SFTP Disposed");
}
}
catch (SshAuthenticationException)
{
Logging.Log_To_file("Permission denied");
fault = 1;
}
catch (SftpPathNotFoundException)
{
Logging.Log_To_file("No such file");
fault = 1;
}
catch (System.Net.Sockets.SocketException)
{
Logging.Log_To_file("Connection timed out");
fault = 1;
}
catch (Exception e)
{
Logging.Log_To_file("Unknown error occurred with SFTP");
fault = 1;
}
return fault;
}
```
Any help with this problem would be much appreciated, thanks.
I have written a program that only downloads a part of the file using SFTP and adds it to the previously downloaded file. Basically resumes download.
Program works perfectly except when I am testing varies conditions for example losing connection ect.
This code is running in a timer.
To reproduce the error:
1. Put a break on "remote.CopyTo(fs, (int)sftp.BufferSize);"
2. Put a break on "catch (System.Net.Sockets.SocketException)"
3. Start timer to run code.
4. As the copy process starts remove network cable (trying to simulate losing connection).
5. Exception will be thrown "System.Net.Sockets.SocketException" after awhile.
6. Plug network cable in.
7. The code will try run again when the timer is triggered and after a random time the exception "System.NullReferenceException" will be thrown(Doesnt take long).
Full Error:
An unhandled exception of type 'System.NullReferenceException' occurred in Renci.SshNet.dll
Additional information: Object reference not set to an instance of an object.
If I break then I see these lines:
Call stack with external code
Renci.SshNet.dll!Renci.SshNet.Sftp.SubsystemSession.SendData(byte[] data)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.SendMessage(Renci.SshNet.Sftp.SftpMessage sftpMessage)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.SendRequest(Renci.SshNet.Sftp.Requests.SftpRequest request)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.RequestClose(byte[] handle)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpFileStream.Dispose(bool disposing)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpFileStream.Finalize()
[Native to Managed Transition]
My Code
```
private int Download(string IP, string UserName, string Password, string FileSource, string Num)
{
int fault = 0;
SftpClient sftp = null;
try
{
using (sftp = new SftpClient(IP, UserName, Password))
{
Logging.Log_To_file("SFTP connecting");
sftp.Connect();
Logging.Log_To_file("SFTP ok");
Int64 length;
using (FileStream check = File.OpenWrite(root + num+ @"\" + OldFile))
{
length = check.Length;
}
using (FileStream fs = File.Create(root + num+ @"\" + PartFile))
{
using (SftpFileStream remote = sftp.OpenRead(FileSource))
{
Int64 length2 = remote.Length;
if (length > length2)
{
remote.CopyTo(fs, (int)sftp.BufferSize);
}
else
{
remote.Seek(length, SeekOrigin.Begin);
remote.CopyTo(fs, (int)sftp.BufferSize);
}
}
}
Logging.Log_To_file("SFTP disconnecting...");
sftp.Disconnect();
Logging.Log_To_file("SFTP Done");
sftp.Dispose();
Logging.Log_To_file("SFTP Disposed");
}
}
catch (SshAuthenticationException)
{
Logging.Log_To_file("Permission denied");
fault = 1;
}
catch (SftpPathNotFoundException)
{
Logging.Log_To_file("No such file");
fault = 1;
}
catch (System.Net.Sockets.SocketException)
{
Logging.Log_To_file("Connection timed out");
fault = 1;
}
catch (Exception e)
{
Logging.Log_To_file("Unknown error occurred with SFTP");
fault = 1;
}
return fault;
}
```
Any help with this problem would be much appreciated, thanks.