提问人:phibac 提问时间:11/9/2023 最后编辑:Martin Prikrylphibac 更新时间:11/14/2023 访问量:62
在 C# 中使用 WinSCP .NET 程序集检查 SFTP 连接 while 循环
Check SFTP connection using WinSCP .NET assembly in C# while loop
问:
我想在我的控制台应用程序中显示实时连接状态。问题是我陷入了循环,我不知道如何在循环时正确检查连接状态。当连接丢失时(在大多数情况下大约~4分钟),我想等到连接再次恢复。在线和离线时间应始终显示在控制台中。
感谢您的任何想法或帮助!
using WinSCP;
using (Session session = new Session())
{
try
{
int i = 0;
// Session Config
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx.xx.xx.xx",
UserName = "xxxxxx",
Password = "xxxxxxxxx",
SshHostKeyFingerprint = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
};
// Connect
session.Open(sessionOptions);
while (true)
{
if (session.Opened == true) //this is displayed all the time
{
Console.WriteLine(i++ + " " + DateTime.Now + " " + "Online");
// check again here if session.Opened still true?
}
else if (session.Opened == false)
{
Console.WriteLine(i++ + " " + DateTime.Now + " " + "Offline");
}
}
}
catch (Exception)
{
}
}
使用 -loop:for
using WinSCP;
for (int k = 0; k < 100; k++)
{
using (Session session = new Session())
{
try
{
int i = 0;
// Session Config
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "",
UserName = "",
Password = "",
SshHostKeyFingerprint = "",
};
session.Open(sessionOptions);
if (session.Opened == true)
{
Console.WriteLine(k + " " + DateTime.Now + " " + "Online");
System.Threading.Thread.Sleep(1000);
session.Close();
}
else if (session.Opened == false)
{
Console.WriteLine("Timeout");
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception)
{
Console.WriteLine(k + " " + DateTime.Now + " " + "Offline");
}
}
}
答:
1赞
Martin Prikryl
11/9/2023
#1
该类不会主动检查连接。如果您在循环中不执行任何操作,它永远不会发现连接丢失。此外,如果连接丢失,它不会自动重新连接。Session
所以你必须
- 尝试对连接执行某些操作,以确保它仍然处于活动状态
- 当连接丢失时,请继续尝试重新连接
像这样的东西:
DateTime lastChange = DateTime.Now;
Session session = null;
while (true)
{
if (session == null)
{
session = new Session();
try
{
session.Open(sessionOptions);
lastChange = DateTime.Now;
}
catch (SessionException)
{
session.Dispose();
session = null;
}
}
else
{
try
{
session.FileExists(session.HomePath);
}
catch (SessionException)
{
lastChange = DateTime.Now;
session.Dispose();
session = null;
}
}
var status = (session != null ? "Online" : "Offline");
Console.WriteLine($"{status} for {DateTime.Now - lastChange}");
Thread.Sleep(TimeSpan.FromSeconds(5));
}
输出示例:
Online for 00:00:00.0000067
Online for 00:00:05.0721274
Online for 00:00:10.1424717
Online for 00:00:15.2207413
Offline for 00:00:00.1085855
Offline for 00:00:10.2624759
Offline for 00:00:20.3726673
Online for 00:00:00.0000051
Online for 00:00:05.0674031
0赞
Georgi Georgiev
11/9/2023
#2
我认为这是你要搜索的:
using WinSCP;
using (Session session = new Session())
{
try
{
int i = 0;
// Session Config
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx.xx.xx.xx",
UserName = "xxxxxx",
Password = "xxxxxxxxx",
SshHostKeyFingerprint = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
};
// Connect
session.Open(sessionOptions);
while (true)
{
CheckConnectionStatus();
//OPTIONAL:Thread.Sleep(5000)
}
}
catch (Exception)
{
}
}
void CheckConnectionStatus()
{
if (session.Opened)
{
Console.WriteLine(i++ + " " + DateTime.Now + " " + "Online");
}
else
{
Console.WriteLine(i++ + " " + DateTime.Now + " " + "Offline");
try
{
session.Open(sessionOptions);
}
catch { }
}
}
评论