Search code examples
c#streamarraystelnet

Telnet connection to devices for c#


I want to telnet program for some cisco devices in our company. I'm using this code:

String str = Console.ReadLine();
Stream stm = tcpClient.GetStream();

ASCIIEncoding asen = new ASCIIEncoding();

byte[] bb = new byte[100];

int k = stm.Read(bb, 0,100);

for(int i=0;i<k;i++)
{ 
   Console.WriteLine(Convert.ToString(bb[i])); 
}

but it returns just some numbers can you say what is wrong or different approach?


Solution

  • you print byte value, it you want to print the string from those byte try using the below code (instead of the for loop):

                byte[] bb = new byte[100];
    
                int k = stm.Read(bb, 0,100);
                Console.WriteLine(System.Text.Encoding.Default.GetString(bb));
    

    You can change the Encoding also, read the String.Text.Encoding_properties, for example the Ascii encoding

      // Create an ASCII encoding.
      Encoding ascii = Encoding.ASCII;
    
      String decodedString = ascii.GetString(bb);