如果这是一个补救问题,我道歉。首先,C#是我学过的第一门编程语言,在过去的一个月里,我已经自学了基础知识+。在我的应用程序中,我使用minimalistic telnet库来进行telnet连接和执行命令。问题是我有大约2000个不同的客户端连接到大多数是在单元连接上。当我通过telnet进入telnet时会快速响应一些提示,在极少数情况下,只需要9秒就能给出登录提示。
我想要做的是修改minimalistic telnet代码,等待登录提示部分最多9秒。
到目前为止我所尝试的是将预定义的超时更改为9000:
string s = tc.Login("root", "pass", 9000);
将telnet类内部的超时更改为9000以及之间的每个组合:
class Telnet
{
TcpClient tcpSocket;
// default timeout 9000
int TimeOutMs = 9000;
我已经替换了原始代码:
if (prompt != "#" && prompt != "$" && prompt != ":")
throw new Exception("Connection failed");
使用我自己的代码:
while (prompt != "#" && prompt != "$" && prompt != ":" && delay != 19)
{
delay ++
System.Threading.Thread.Sleep(500);
if (delay = 12)
{
updateHistoryWindow(this, "Unit is taking a long time to respond");
}
if (delay = 19)
{
throw new Exception("Connection failed");
updateHistoryWindow(this, "Unit did not display a login prompt");
}
}
使用While命令代替原始if命令似乎根本不起作用。它不会等待while循环,它就像它首先超时一样。
我的应用目前分别设置为2500/2500超时,所有典型连接都很有效。如果我将这些超时值更改为9000,那么与典型的18秒相比,连接需要一分钟或更长时间。我希望尽可能快地保持连接速度,只要登录提示需要等待,并且在向服务器发送命令之间没有9秒的延迟。理论上,一个系统需要另外5秒才能通过登录提示进行响应,只需要23秒即可完成操作(其余响应不会像登录响应一样延迟)。
以下是我初始化连接的代码:
try
{
int delay = 0;
Telnet tc = new Telnet(MyGlobals.IP, 23);
//login with user, password, using a timeout of 2500ms, and show server output
string s = tc.Login("root", "pass", 2500);
Console.Write(s);
// Check server output to see if it responds
string prompt = s.TrimEnd();
prompt = s.Substring(prompt.Length - 1, 1);
while (prompt != "#" && prompt != "$" && prompt != ":" && delay != 19)
{
delay ++
System.Threading.Thread.Sleep(500);
if (delay = 12)
{
updateHistoryWindow(this, "Unit is taking a long time to respond");
}
if (delay = 19)
{
throw new Exception("Connection failed");
updateHistoryWindow(this, "Unit did not display a login prompt");
}
}
prompt = "";
// while connected
if (tc.IsConnected)
{ etc....
以下是来自minimalistic telnet的大量telnet.cs供参考:
namespace MYApp
{
enum Verbs
{
WILL = 251,
WONT = 252,
DO = 253,
DONT = 254,
IAC = 255
}
enum Options
{
SGA = 3
}
class Telnet
{
TcpClient tcpSocket;
// default timeout was 2500
int TimeOutMs = 2500;
public Telnet(string Hostname, int Port)
{
tcpSocket = new TcpClient(Hostname, Port);
}
public string Login(string Username, string Password, int LoginTimeOutMs)
{
int oldTimeOutMs = TimeOutMs;
TimeOutMs = LoginTimeOutMs;
string s = Read();
if (s.TrimEnd().EndsWith(" "))
throw new Exception("Failed to connect : no login prompt");
WriteLine(Username);
s += Read();
if (!s.TrimEnd().EndsWith(":"))
throw new Exception("Failed to connect : no password prompt");
WriteLine(Password);
s += Read();
TimeOutMs = oldTimeOutMs;
return s;
}
public void WriteLine(string cmd)
{
Write(cmd + "\n");
}
public void Write(string cmd)
{
if (!tcpSocket.Connected) return;
byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.Replace("\0xFF", "\0xFF\0xFF"));
tcpSocket.GetStream().Write(buf, 0, buf.Length);
}
public string Read()
{
if (!tcpSocket.Connected) return null;
StringBuilder sb = new StringBuilder();
do
{
ParseTelnet(sb);
System.Threading.Thread.Sleep(TimeOutMs);
} while (tcpSocket.Available > 0);
return sb.ToString();
}
public bool IsConnected
{
get { return tcpSocket.Connected; }
}
void ParseTelnet(StringBuilder sb)
{
while (tcpSocket.Available > 0)
{
int input = tcpSocket.GetStream().ReadByte();
switch (input)
{
case -1:
break;
case (int)Verbs.IAC:
// interpret as command
int inputverb = tcpSocket.GetStream().ReadByte();
if (inputverb == -1) break;
switch (inputverb)
{
case (int)Verbs.IAC:
//literal IAC = 255 escaped, so append char 255 to string
sb.Append(inputverb);
break;
case (int)Verbs.DO:
case (int)Verbs.DONT:
case (int)Verbs.WILL:
case (int)Verbs.WONT:
// reply to all commands with "WONT", unless it is SGA (suppres go ahead)
int inputoption = tcpSocket.GetStream().ReadByte();
if (inputoption == -1) break;
tcpSocket.GetStream().WriteByte((byte)Verbs.IAC);
if (inputoption == (int)Options.SGA)
tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WILL : (byte)Verbs.DO);
else
tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
tcpSocket.GetStream().WriteByte((byte)inputoption);
break;
default:
break;
}
break;
default:
sb.Append((char)input);
break;
}
}
}
}
}
提前感谢您对此有任何帮助!