C# Telnet 实现与 TCPClient,用户名/密码错误

标签 c# visual-studio-2015 telnet tcpclient windows-10-iot-core

当我通过 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/

相关文章:

c# - Catch block 的顺序重要吗?

c# - 我怎样才能正确地模拟我的 controllercontext 来测试 ViewResult.ExecuteResult()?

bash - shell脚本中的竖线?

command-line - Win XP 中命令行实用程序的代理

c# - 从 C# 中的托管 Windows 窗体设计器获取项目

c# - skydrive System.Dynamic.DynamicObject

javascript - Visual Studio 的 Javascript Intellisense 能否给出有关 'this' 关键字的提示?

c - Hpux telnet 使用套接字

typescript - 如何在 VS2015 项目中提供模块标志?

c++ - "Type is incomplete"(但不是)和代码编译