Search code examples
c#telnet

C# Telnet client give none/incomplete output


I have a problem with the basic implementation of telnet client in C#. When I try to connect to the remote computer in local network via command line (telnet 192.168.0.255 23), there are no problems, but with this code, I cannot get for me any logical output
(just chars "??%?? ?? ??'??¬?? ??").

TcpClient telnetClient = new TcpClient("192.168.0.255", 23);
NetworkStream telnetStream = telnetClient.GetStream();
byte[] data = new Byte[4];
string responseData = String.Empty;
int bytes = telnetStream.Read(data, 0, data.Length);
while (true){
   responseData += Encoding.ASCII.GetString(data, 0, bytes);
   bytes = telnetStream.Read(data, 0, data.Length);
   if (!telnetStream.DataAvailable)
      break;
}
Console.WriteLine(responseData);
telnetClient.Close();

I realize when I try to get output from different public servers (for example. telehack.com), I get just in some cases first line of output. But output, which is provided via command line is much much extensive.

Telnet connection to remote pc in LAN via telnet and c#
Connection to public pc via telnet and c#

Could you please tell me what I doing terribly wrong? :) Thank you all for help.


Solution

  • The first bytes send via Telnet in the case of telehack.com are Command bytes (see https://en.wikipedia.org/wiki/Telnet#Telnet_data ). They Start with 0xFF and then the command. You can see them clearly in the example below in the debug window (numbers there are in decimal).

    I tried this with your (extended) C# script: enter image description here