当我通过 telnet 连接到服务器(Windows 10 IOT 设备)时,说“打开 127.0.0.1 1023”它会连接我,要求输入用户名。在设备上我没有用户名,所以我可以通过回车连接,它不会要求我输入密码,而是直接将我带到命令提示符
当我尝试使用 TCP 客户端在 C# 中实现此功能时,我被要求输入用户名,我用回车回复,然后我被要求输入密码,我用回车回复,然后它返回无效的用户名或密码,并断开我的连接。
这是为什么?我需要添加用户名,还是 TCPClient 与 telnet 不同?
谢谢
代码:
var tcpClient = new TcpClient("127.0.0.1", 1023);
var ns = tcpClient.GetStream();
Byte[] output = new Byte[1024];
String response = String.Empty;
Byte[] cmd = System.Text.Encoding.ASCII.GetBytes("\n");
ns.Write(cmd, 0, cmd.Length);
Thread.Sleep(100);
Int32 bytes = ns.Read(output, 0, output.Length);
response = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write(response);
Regex objToMatch = new Regex("User name");
if (objToMatch.IsMatch(response))
{
cmd = System.Text.Encoding.ASCII.GetBytes("" + "\r");
ns.Write(cmd, 0, cmd.Length);
}
Thread.Sleep(100);
bytes = ns.Read(output, 0, output.Length);
response = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write(response);
objToMatch = new Regex("Password");
if (objToMatch.IsMatch(response)) //Both Regex match
{
cmd = System.Text.Encoding.ASCII.GetBytes("" + "\r");
ns.Write(cmd, 0, cmd.Length);
}
Thread.Sleep(100);
bytes = ns.Read(output, 0, output.Length);
response = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write(response); //Invalid Username/Password response
cmd = System.Text.Encoding.ASCII.GetBytes("\r");
ns.Write(cmd, 0, cmd.Length); //Cannot send because connection is closed
tcpClient.Close();
最佳答案
我确定这是设备问题,而不是 C# 或 Telnet 问题。我找到了一种在设备上执行我的命令的不同方法,所以我可以关闭这个问题,而且我很确定没有其他人会遇到这个问题
关于C# Telnet 实现与 TCPClient,用户名/密码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47702804/