使用sshnet库上传SFTP - 处理连接问题

Tom*_*n32 4 .net c# ssh sftp ssh.net

使用Renci的sshnet库,我无法在上传文件时处理连接问题.

如果我在UploadFile上设置了一个断点,禁用我的连接,让它运行,它只是挂在那一行上.这些是慢速连接上的潜在大文件,因此添加OperationTimeout将很困难.

此外,这在一致的连接上一遍又一遍地工作.有任何想法吗?谢谢!

AuthenticationMethod[] methods = new AuthenticationMethod[1];
methods[0] = new PasswordAuthenticationMethod(username, pwd);

var con = new ConnectionInfo(Config.RemoteHostName, username, methods);
con.Timeout = new TimeSpan(0, 0, 0, 30);
var client = new SftpClient(con);

client.Connect();

string fullFilePath = string.Format("{0}{1}", Config.RemoteFilePath, filename);
client.BufferSize = 15000;
client.UploadFile(new MemoryStream(buffer), fullFilePath);
client.Disconnect();
Run Code Online (Sandbox Code Playgroud)

Mar*_*ryl 5

传输文件时,SftpClient.OperationTimeout确定库等待单个缓冲区读/写操作的时间(默认情况下最大为64 KB,请参阅参考资料SftpClient.BufferSize).没有等待整个转移完成的时间.

因此,您可以安全地将其从默认的"无限"更改为某个有意义的值.