Search code examples
c#visual-studio-2015telnettcpclientwindows-10-iot-core

C# Telnet implementation with TCPClient, username/password error


When I do a telnet connection to the server (windows 10 IOT device), say "open 127.0.0.1 1023" it connects me, asking for a username. On the device I dont have a username, so I can connect by hitting return, and it doesn't ask me for a password, and takes me straight to a command prompt

When I try to implement this in C# using TCP Client, I get asked for a username, which I reply with a return, then I get asked for a password, which I reply with a return, then it returns invalid username or password, and disconnects me.

Why is this? Am I required to add a username, or is TCPClient different from telnet?

Thanks

The code:

         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();

Solution

  • I determined it was a device issue, not a C# or Telnet issue. I found a different way to execute my commands on the device, so I can close this question, and Im pretty sure no-one else will have this problem