Click here to Skip to main content
Click here to Skip to main content

Telnet server classes

, 12 Jun 2001
Rate this:
Please Sign up or sign in to vote.
Some classes and sample code on how to control/access your app remotely, using a telnet client.

Introduction

This article presents two classes: CTelnet and CTelnetSocket, and one global function: TelnetThread. Using the CTelnet class, you can access/control your application remotely using a Telnet client. The download contains the classes to use, and a program to demonstrate the use of the CTelnet class.

CTelnetSocket is a class that is used internally (using Winsock calls). The global TelnetThread function is started as a separate thread. This way your application can run and the TelnetThread does all the Telnet activity. You can register some callback functions which are called by the TelnetThread. Usage is very easy:

Instantiate the CTelnet class:

CTelnet telnet( "Welcome at my telnetserver\r\n"    // intro screen
          , "scott"                               // userid
          , "Enter userid:"                     // userid prompt
          , "tiger"                           // password
          , "Enter password: "              // password prompt
          , "Command >"                   // prompt to use
          , 23 );                       // port to run on

Say, you want a function to be called whenever a user types help. Create a help function like this:

CString help( const CString& sArg );        // help function prototype

// implementation of the help function
CString help( const CString& sArg )
{
    return "\r\nCurrently no commands are implemented\r\n";
}

So this function gets called whenever the user types help (Not case sensitive). It will send data to the client "Currently no commands are implemented". When the user types "help command", sArg will contain the value " command". The next step is to register this (callback) function:

telnet.registerFunction( help , "help" );

You can create as much callback functions as you like. It is also possible to write a callback function that is called whenever the user just presses "enter". Register the function, and leave the second argument empty ("").

The next step is to start the service.

telnet.start();

Now, a user can connect to your application, that's it... Please note that a separate thread is running your Telnet service. So your application can do whatever you want after the start method is called.

Stopping the Telnet server is done by the destructor, or by calling the method stop:

telnet.stop();

When the application is running, you have two methods available: write() and setPrompt(). With the write method, you can write data to the client connected. E.g. it can be used for sending trace data.

telnet.write( "this will be sent to the client connected" );

Please note that this method cannot be used by any of the callback functions (It is not needed anyway, see implementation of callback help function).

Another method that can be called when the Telnet server is running is: setPrompt, which sets the prompt which is presented to the client:

telnet.setPrompt( "Another prompt>" );

Please note that this method cannot be used by any of the callback functions.

Final remarks:

  • For every instantiation of the CTelnet class, only one client can connect, you can however instantiate as many instances of CTelnet as you like.
  • When sharing data between the application and the callback functions (e.g. global variables), make sure you do that thread safe (e.g. using Critical Sections). The sample program which comes with the project files should give an idea on how to do this.
  • Most methods give back a boolean, true means it succeeded, false means it did not succeed. Call getLastError() to get the error that occurred.
  • The Telnet client connected should not do a local echo because the server does that itself.
  • Only backspace is implemented as a text editor command.

Have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

J.Hogendoorn

Netherlands Netherlands
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAric Green22-Sep-11 19:50 
GeneralMy vote of 5 Pinmember(BlackBox) Ethical Hacker31-Oct-10 6:35 
GeneralDetected memory leaks Pinmembernflx5-Mar-06 20:37 
QuestionUNIX? Pinmemberjontemannen2-Feb-06 5:32 
GeneralVery Good PinmemberMasterGohan17-Jan-06 8:30 
QuestionRe: Very Good Pinmembergmt20016-Feb-07 10:20 
AnswerRe: Very Good PinmemberIantLetoxx24-Mar-10 12:23 
GeneralTalking to Unix Pinmemberpetetastic26-Jan-04 16:50 
AnswerRe: Talking to Unix PinmemberMerlinblack20-Feb-06 19:53 
QuestionMFC implementation? PinmemberJan7513-Dec-02 1:02 
AnswerRe: MFC implementation? PinmemberMasterGohan17-Jan-06 8:42 
QuestionHow can we capture the data into a buffer in response to a command send to a test system using this Telnet Class PinmemberThirumalesh Thirumala13-Dec-01 12:30 
GeneralBug? - Cannot disconnect then reconnect PinmemberJeff Hansen18-Nov-01 19:17 
QuestionTelnet Client? Pinmember-= Matt Newman =-17-Nov-01 14:28 
AnswerRe: Telnet Client? PinmemberCyprus27-Nov-01 10:46 
GeneralRe: Telnet Client? PinmemberMatt Newman28-Nov-01 12:29 
GeneralRe: Telnet Client? PinmemberHanphy18-May-03 23:13 
GeneralRe: Telnet Client? Pinmembermohammad arif22-Oct-03 19:21 
GeneralRe: Telnet Client? PinsussAnonymous14-Mar-04 11:32 
GeneralRe: Telnet Client? Pinmembermarfemag30-May-04 13:43 
GeneralRe: Telnet Client? PinmemberMerlinblack20-Feb-06 19:57 
GeneralRe: Telnet Client? Pinmemberrushivyas17-Aug-10 9:27 
GeneralRe: Telnet Client? Pinmemberrushivyas17-Aug-10 9:30 
AnswerRe: Telnet Client? Pinmemberliwenhaosuper13-Feb-11 17:07 
GeneralRe: Where to put your server name? Pinmember-= Matt Newman =-17-Nov-01 14:21 
GeneralStraight MFC telnet server implementation PinmemberAnonymous16-Aug-01 5:28 
QuestionEducated ? Pinmemberkoos k20-Jun-01 20:05 
AnswerRe: Educated ? PinmemberHogendoorn23-Jun-01 12:16 
Hmm, guess you got a point with that...
Cry | :((
Plse note that not all techniques were learned during my stay, check it out: "source code comments" Cool | :cool:
 


GeneralRe: Educated ? PinmemberAnonymous11-Aug-01 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 13 Jun 2001
Article Copyright 2001 by J.Hogendoorn
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid