telnet server.cpp
上传用户:zhangwei01
上传日期:2007-01-09
资源大小:25k
文件大小:3k
- #include "stdafx.h"
- #include "telnet.h"
- // prototypes of callback functions
- CString displayHelp( const CString& sArg );
- CString trace( const CString& sArg );
- CString stop( const CString& sArg );
- CString nomatch( const CString& sArg );
- // a critical section is needed, bcause
- // bStoppping and iCounter are used by the main- and the telnetThread
- CRITICAL_SECTION critSection;
- bool bStopping = false;
- int iCounter = 0;
- bool bStop = false;
- int main(int argc, char* argv[])
- {
- InitializeCriticalSection( &critSection );
- CTelnet telnet( "Welcome at my telnetserverrn" // intro screen
- , "scott" // userid
- , "Enter userid:" // userid prompt
- , "tiger" // userid
- , "Enter password: " // password prompt
- , "Command >" // prompt to use
- , 23 ); // port to run on
- // register callback functions
- telnet.registerFunction( trace , "trace" ); // gets called when user typed 'trace'
- telnet.registerFunction( displayHelp , "help" ); // gets called when user typed 'help'
- telnet.registerFunction( stop , "stop" ); // gets called when user typed 'stop'
- telnet.registerFunction( nomatch , "" ); // gets called when user typed ENTER
- // start the server
- if ( !telnet.start() )
- {
- printf( "error occured %s n", telnet.getLastError() );
- return 0;
- }
-
- // very simple..... just increment counter and sleep for one second
- while( !bStopping )
- {
- iCounter ++;
- Sleep(1000);
- // 30 seconds passed, change prompt
- EnterCriticalSection( &critSection );
- if ( iCounter == 30 )
- {
- telnet.write( "rn30 seconds passed, changing prompt!rn" );
- telnet.setPrompt( "New prompt >" );
- }
- bStopping = bStop;
- LeaveCriticalSection( &critSection );
- }
- telnet.stop();
- return 0;
- }
- ///////////////////////////////////////////////////////////////////
- // gets called when user presses ENTER
- CString nomatch( const CString& sArg )
- {
- return "Type help for helprn";
- }
- //////////////////////////////////////////////////////////////////
- // gets called when user issued 'help'
- CString displayHelp( const CString& sArg )
- {
- CString sHlp;
- sHlp = "rnCommands currently supported:rnrn";
- sHlp += "trace displays current value of the counterrn";
- sHlp += "stop quits the programrn";
- sHlp += "stop 10 quits the program over 10 secondsrnrn";
- return sHlp;
- }
- //////////////////////////////////////////////////////////////////
- // gets called when user issued 'trace'
- CString trace( const CString& sArg )
- {
- CString sValBack;
- EnterCriticalSection( &critSection );
- sValBack.Format( "rnCurrent value of iCounter=%irn", iCounter );
- LeaveCriticalSection( &critSection );
- return sValBack;
- }
- //////////////////////////////////////////////////////////////////
- // gets called when user issued 'stop'
- CString stop( const CString& sArg )
- {
- if ( sArg == " 10" )
- {
- Sleep(10000);
- }
- EnterCriticalSection( &critSection );
- bStop = true;
- LeaveCriticalSection( &critSection );
- return "Stopping the programrn";
- }