connecting to Telnet through C#


  • I am trying to create a Telnet session from a C# application and be able to send commands and receive output back.  Is this possible with just a regular TCPClient connecting to a host on port 23 and then reading from/writing to the stream associated with that client?  This is what I have been trying and can't figure out if my username/password stuff works right, thats why I didn't post any code because i really dont think I have anything that works.

    Any help on this would be greatly appreciated. 

    Thanks in advance
    Monday, January 15, 2007 6:59 PM

All replies

  • You are exactly right. Telnet is one of those ridiculously simply things that for the most part is just an open port... the authentication though is something else completely as that is up to the shell that you are accessing, not so much the telnet protocol itself.

    Are you receiving any data in your client from the server?

    Monday, January 15, 2007 7:07 PM
  • I just have a very simple setup right now as follows...

    TcpClient telnet = new TcpClient("", 23);
    NetworkStream telnetStream = telnet.GetStream();
    StreamWriter output = new StreamWriter(telnetStream);
    StreamReader input = new StreamReader(telnetStream);

    And I try to read from the stream and it gives me: #'$

    when I just do a Console.Write to what is read into my buffer in the input.Read() call.

    On a side note, do you have any idea how to do the authentication? or how to figure out a way to do it?  Because since you said that I am guessing that sending in a username/password over the Socket doesnt authenticate me at all and thus I am really not even connected to anything. 

    I don't know if any of what I said made much sense, sorry if I just confuse you more.

    I appreciate the help though
    Monday, January 15, 2007 7:24 PM
  • I've been working on getting a telnet session to work from C# using the TCPClient connecting to a Windows Telnet Server.  By default the Telnet server in Windows uses NTLM authentication only.  You can disable this in the Telnet Server Administration tool (Display/Change registry settings -> NTLM -> set the value to 0 then restart the telnet service).  You will then need to create TelnetClients group and add a user account you will used to authenticate with.  Using ASCII encoding you connect, read bytes, then write bytes for username and password (make sure to include the Environment.NewLine after each).
    I can get past the login and password, but then when I try to execute any additional commands my app hangs.  I'm expecting a synchronous response, and because I'm using a aspx page I need to keep it synchronous.

    Side note:
    When I first attempted to connect without disabling NTLM I received ??%?????????? from the server, which I am guessing is the authentication challenge - with NTLM disabled that problem went away.

    Any ideas on why it would not respond?  I tried executing a simple dir command.

    The page calls the checkSearch method using a button and it passes the IP and port, and the results are written to a textbox called txtResults.

    protected void checkSearch(string destServerIp, int port)


    TcpClient oClient = new TcpClient();



    oClient.Connect(destServerIp, port);

    NetworkStream ns = oClient.GetStream();

    txtResults.Text = read(ns);


    txtResults.Text += Environment.NewLine + read(ns);

    write(ns, "telnetPassword");

    txtResults.Text += Environment.NewLine + read(ns);

    write(ns, Environment.NewLine);

    txtResults.Text += Environment.NewLine + read(ns);




    catch (Exception ex)


    txtResults.Text += Environment.NewLine + ex.Message;

    if (oClient.Connected)




    private string read(NetworkStream ns)


    StringBuilder sb = new StringBuilder();

    if (ns.CanRead)


    byte[] readBuffer = new byte[1024];

    int numBytesRead = 0;



    numBytesRead = ns.Read(readBuffer, 0, readBuffer.Length);


    sb.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numBytesRead));

    sb.Replace(Convert.ToChar(24),' ');

    sb.Replace(Convert.ToChar(255), ' ');

    sb.Replace('?', ' ');


    while (ns.DataAvailable);


    return sb.ToString();


    private void write(NetworkStream ns, string message)


    byte[] msg = Encoding.ASCII.GetBytes(message + Environment.NewLine);

    ns.Write(msg, 0, msg.Length);


    Wednesday, August 20, 2008 7:50 PM
  • I spent the rest of the afternoon yesterday working on this and in my research I discovered the problems I was having are related to the Telnet protocol.  In Telnet you must implement a Network Virtual Terminal (NVT) and when you open a connection to a telnet server, you must handle the negotiation of things like terminal type.  If you need to make this work, http://www.hw-group.com/support/nvt/index_en.html will be a helpful resource.  I ended up choosing to skip telnet altogether to tackle my problem - I don't really have the time or patience to write a telnet client right now.

    If you do choose to work through getting telnet to work, I recommend logging or putting a watch on the bytes in the byte[] coming from the server every time you do a read.  Whenever you see 255 that is telling you that the following byte is to be interpretted as a command (IAC), and it is usally followed by codes for commands or operations (see NVT for more detail).  The server will wait for an appropriate response to the commands it sent you and won't respond until it gets them.

    If you end up having need for this and get it working please post a follow-up with sample code.

    Thursday, August 21, 2008 1:58 PM
  • Hi,

    In my application I can't get full buffered data whereas I receive a portion of data. How can be the full stream be received

    Anyone please help me.

    Tuesday, June 28, 2011 7:16 AM
  • http://www.faqs.org/rfcs/rfc1143.html  TELNET Option Negotiation

    This http://srand2.blogspot.com/2009/02/telnet-programming-with-c.html looks like it may have some useful information.
    Serial Port      Random      Microsoft® Community Contributor 2011
    Tuesday, June 28, 2011 11:44 AM
  • Bit Didler was on right track, and then learned that there's a bit more to a Telnet client than one may thing.  However, I believe that it actually could be easier to bypass the Telnet protocol and control all input via a Console session.  If you can redirect standard in and standard out through the Process.Startinfo methods, then you could just use input and output from the dos window!  All the OS to netgoiate and run the Telnet session.

    JP Cowboy Coders Unite!

    Wednesday, April 25, 2012 12:59 PM