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

Telnet Console Application

By , 31 Jul 2001
 

Introduction

This is a telnet console application that implements the Telnet client protocol in rfc854. I have run it on VC6, NT4.0 SP3. I did not test it on 95 and VC5.

Telnet

The purpose of the TELNET Protocol is to provide a fairly general, bi-directional, eight-bit byte oriented communications facility. Its primary goal is to allow a standard method of interfacing terminal devices and terminal-oriented processes to each other. It is envisioned that the protocol may also be used for terminal-terminal communication ("linking") and process-process communication (distributed computation).

General Considerations

A TELNET connection is a Transmission Control Protocol (TCP) connection used to transmit data with interspersed TELNET control information. The TELNET protocol is built upon three main ideas: first, the concept of a "Network Virtual Terminal"; second, the principle of negotiated options; and third, a symmetric view of terminals and processes.

When a TELNET connection is first established, each end is assumed to originate and terminate at a "Network Virtual Terminal", or NVT. An NVT is an imaginary device which provides a standard, network-wide, intermediate representation of a canonical terminal. This eliminates the need for "server" and "user" hosts to keep information about the characteristics of each other's terminals and terminal handling conventions. All hosts, both user and server, map their local device characteristics and conventions so as to appear to be dealing with an NVT over the network, and each can assume a similar mapping by the other party. The NVT is intended to strike a balance between being overly restricted (not providing hosts a rich enough vocabulary for mapping into their local character sets), and being overly inclusive (penalizing users with modest terminals).

This project has five main classes.

CSocketDx

CSocketDx class is used to establish TCP connection. It has 3 functions:

CSocketDx(char *,int);
~CSocketDx();
int Create();
int Connect();
SOCKET TelnetConnect();

CSocketRx

CSocketRx class is used to receive data. It has only one thread.

CSocketRx();
CSocketRx(SOCKET,HANDLE&);
virtual ~CSocketRx();
static DWORD RdTh(CSocketRx *);

CSocketTx

CSocketTx class is used to send data. It has only one thread.

CSocketTx(SOCKET,HANDLE&);
virtual ~CSocketTx();
static DWORD SendTh(CSocketTx*);

CProtocolRx

CProtocolRx class is used to implement the Telnet protocol.

inline void yesreply(SOCKET server, _verb verb,_option option);
inline void noreply(SOCKET server, _verb verb,_option option);
inline void askfor(SOCKET server, _verb verb,_option option);
void TelentProtcol(SOCKET server,unsigned char code);

Platforms

I have run it on VC6, NT4.0 SP3, I did not test it on 95 and VC5.

Limitations

Cannot handle arrow keys.

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

About the Author

Ibrar Ahmad
Web Developer
Pakistan Pakistan
Member
Master of Computer Science
Experince
1). C++
2). C
3). Network Programming...
Socket,Pipe,Protocol Implemntation
4). hardware interfaceing
5). Emabeded programming in C51...Etc
(for 8051,PIC,C166 Processores)
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionerror while building applicationmembersmaranika8 Mar '11 - 19:32 
I am getting following errors.
 

fatal error C1189: #error : MFC requires use of Winsock2.h c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwin.h
 

error BK1506 : cannot open file '.\Debug\SocketDx.sbr': No such file or directory
 

 
Please help me
GeneralMy vote of 5memberdkgversion6 Dec '10 - 18:34 
Great! It's helpful for me. Thanks.
NewsIt works with win 7memberDr.Luiji18 Feb '10 - 4:29 
Just for fun I try it under Windows 7, compiled with VC6 and OMG | :OMG: it works after 10 years!!
Cheers
Dr.Luiji
 
Trust and you'll be trusted.
 
My Blog : The Code Is Art

Questionopen source software ? license?memberjohnlol18 Jan '10 - 9:43 
Hi there,
 
Wondering if this project is licensed under any OSS license or proprietary.
 
Thanks for your help!
GeneralThanksmemberauralius28 Oct '08 - 22:43 
This is great...
It helps me...
 
From Indonesia with love..!!

Questionhow to change this program to send commands via file handle and store output into a file???memberS.V.RAJKUMAR21 Apr '08 - 1:42 
how to change this program to send commands via file handle and store output into a file???
 
S.V.Rajkumar

QuestionHow to off ECHO on client window?memberkumardeepak4 Feb '07 - 22:15 
Hello,
 
I am trying to build a telnet library for my client application. I have use this code piece. When I am writing any command like "ls\r\n" server is echoing the thing back. As a result my code is receiving "ls\r\n" apart from the normal output of command and login prompt of linux shell.
 
So, I want to switch off "ls\r\n" so that I only receive linux login prompt and "ls" output.
Please help me out of this difficulty.
 
Nothing

AnswerRe: How to off ECHO on client window?member12kaunas14 Mar '07 - 16:30 
I am totally new in telnet programming. I was looking for some Telnet client code in C++ which I can implement in my application. I have found this project and since you are using it maybe you help me. I have my special Telnet server which runs on port 1024. When I open this project and enter my IP and port, my server responses properly. But after that I can't type anything on the screen. Am I missing something ?
 
Thanks for any advice.
Alex
QuestionHow could I send arrow keys?membereghost30 Jan '07 - 22:53 
I try to send arrow keys( included other 2-byte keys), but it doesn't work. Do you have any idea about it?
 
by Eghost
AnswerRe: How could I send arrow keys?membereghost31 Jan '07 - 14:03 
I found it.
http://www.nw.com/nw/WWW/products/wizcon/vt100.html[^]
 
By Eghost
GeneralRe: How could I send arrow keys?membereghost1 Feb '07 - 21:16 
Partial code should be modified as: (fixed screen attribute issue)
 
void ansi_set_screen_attribute(char* buffer)
{
while(*buffer)
{
switch(*buffer++)
{
case '0': //Normal
sa = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case '1': //Hign Intensity
sa |= FOREGROUND_INTENSITY;
break;
//case '4': //Underscore /** Modify here **/
//break;
case '5': //Blink.
sa |= BACKGROUND_INTENSITY;
break;
case '7':
sa = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
break;
case '8':
sa = 0;
break;
case '3': /** Modify here **/
sa &= (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY | ((*buffer & 1)?FOREGROUND_REDBlush | :O ) |((*buffer & 2)?FOREGROUND_GREENBlush | :O ) |((*buffer & 4)?FOREGROUND_BLUEBlush | :O ));
if(*buffer)
buffer++;
break;
case '4': /** Modify here **/
if(*buffer != ';' && *buffer)
{
sa = sa & (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY) | ((*buffer & 1)?BACKGROUND_REDBlush | :O ) |((*buffer & 2)?BACKGROUND_GREENBlush | :O ) |((*buffer & 4)?BACKGROUND_BLUEBlush | :O );
if(*buffer)
buffer++;
}
break;
}
if(*buffer && *buffer == ';')
buffer++;
}
SetConsoleTextAttribute(stdout1,sa);
}
 
By Eghost
QuestionHow to disable Telnet-Prompt?membermephistho17 Jul '06 - 22:33 
Hi,
 
is it possible to disable the telnet-prompt?
 
I just need the standard cmd-line. I want to build a text-based menu for sending commands to a network power switch. Turn special Plugs on/off and so on.
 
So I need to display the data recieved over telnet, but do not type the commands directly into a telnet-console.
 
Is the problem clear?
 
Thanks,
meph
AnswerRe: How to disable Telnet-Prompt?memberkumardeepak4 Feb '07 - 22:17 
hi Meph,
 
Did you find the solution ? If yes, please share with me as well.
 
Rgs
KD
 
Nothing

GeneralRe: How to disable Telnet-Prompt?membermephistho5 Feb '07 - 21:36 
No, sorry, I didn't continue on that program.
 
gl
meph
GeneralRe: How to disable Telnet-Prompt?memberkumardeepak6 Feb '07 - 23:52 
hi Meph,
 
Can you suggest me alternative solution for the problem. How did u solve your problem ?? Do you have any telnet library so that i use it in my code
 
Nothing

GeneralRe: How to disable Telnet-Prompt?memberwnagenbo12 Apr '07 - 4:02 
hi
socket rebuild may solution the problem
 

WEB
GeneralAbout SSHmemberJZM21 Dec '05 - 14:10 
Does your telnet program support ssh (secure telnet)?
GeneralRe: About SSHmemberbenjamin232 Apr '08 - 4:14 
Telnet and SSH are two very different protocols, take a look at PuTTy source code.
GeneralCSI control sequencesmemberssh3313 Dec '04 - 5:11 
I tried this app on telecom equipment I have handy. 80% of it wants CSI (Control Sequence Introducers) implementation.
 

CSI control sequences
 
In 7-bit mode, CSI is ESC [. In 8-bit mode, CSI is (hex)0x9B.
 
{ASCII}, Hex, Action
 

{CSI [n] @} 9B [n] 40 Insert n spaces at cursor (default = 1 space)
{CSI [n] A} 9B [n] 41 Cursor up n rows, no wrap (default = 1 row)
{CSI [n] B} 9B [n] 42 Cursor down n rows, no wrap (default = 1 row)
{CSI [n] C} 9B [n] 43 Cursor right n columns, no wrap (default = 1 column)
{CSI [n] D} 9B [n] 44 Cursor left n columns, no wrap (default = 1 column)
{CSI [n] F} 9B [n] 46 Cursor up n rows, positioned in first column (default = 1 row)
{CSI [n] G} 9B [n] 47 Move cursor to column n (default = column 1)
{CSI [r[;c]] H} 9B [r [3B c]] 48 Cursor position (default = row 1; column 1)
{CSI [n] J} 9B [n] 4A Erase 0=cur-EOS 1=HOME-cur 2=screen (default = 0 (to end of screen))
{CSI [n] K} 9B [n] 4B Erase 0=cur-EOL 1=BOL-cur 2=line (default = 0 (to end of line))
{CSI [n] L} 9B [n] 4C Insert n lines (default = 1 line)
{CSI [n] M} 9B [n] 4D Delete n lines (default = 1 line)
{CSI [n] P} 9B [n] 50 Delete n chars (default = 1 char)
{CSI [n] S} 9B [n] 53 Scroll forward n lines (default = 1 line)
{CSI [n] T} 9B [n] 54 Scroll backward n lines (default = 1 line)
{CSI [n] X} 9B [n] 58 Erase cur for n-1 chars (default = 1 (0 chars))
{CSI Z} (9B 5A) Back tab
{c CSI [n] b} c 9B [n] 62 Repeat GR or GL character c, n times. c is the last displayable character; n defaults to 1 time.
{CSI 0 c} (9B 30 63) Primary device attrib request
{CSI [n] d} 9B [n] 64 Move cursor to line n (default = line 1)
{CSI [n] g} 9B [n] 67 Tab clear 0=cursor 2=all (default = 0)
{CSI [n[;n]...] h} 9B [n[3B n]...] 68 Standard Set mode (See modes table) (default=none)
{CSI ? [n[;n]...] h} 9B 3F [n[3B n]...] 68 Private Set mode (See modes table) (default=none)
{CSI [n[;n]...] l} 9B [n[3B n]...] 6C Standard Reset mode (See modes table) (default=none)
{CSI ? [n[;n]...] l} 9B 3F [n[3B n]...] 6C Private Reset mode (See modes table) (default=none)
{CSI [n[;n]...] m} 9B [n[3B n]...] 6D Select Graphic Rendition (See below) (default = 0)
{CSI n n} 9B n 6E Device status 5=status 6=cursor/pos
{CSI [r[;c]] r} 9B [r [3B c]] 72 Set scroll region and home cursor
{CSI r} 9B 72 Disable scroll region & home cursor
{CSI s} 9B 73 Save cursor
{CSI u} 9B 75 Restore cursor
{CSI ! p} 9B 21 70 Soft reset
{CSI [n[;n]] ]} 9B [n [3B n]...] 5D Set default 1=underline 2=half-intensity 8=colorset (default=none)
{CSI = [f [;d]] B} 9B 3D [f [3B d]] 46 Set frequency(Hz) and duration (ms) for bell (default=100Hz, 250ms)
{CSI = [n] F} 9B 3D [n] 46 Set and Save foreground color
{CSI = [n] G} 9B 3D [n] 47 Set and Save background color
 

Hope it helps.

GeneralTelnet Color Formatmembercnalpine26 Dec '02 - 20:06 
Where Can I find the color format description on Telnet?
Thanks!
GeneralRe: Telnet Color FormatmemberHughJampton25 Feb '05 - 10:57 
Try this:
http://consult.stanford.edu/pub/communications/color-message[^]
 

 
Hugh
GeneralConvert it to Single DialogmemberAmjad W. Hawash18 Mar '02 - 2:28 
Hi,
can you convert your telnet form console to be a Single Dialog?
AmjadConfused | :confused:
QuestionHow to put your login and password in your code?memberrena8 Nov '01 - 6:50 
Is it possible to put your login and password info. in your code to telnet to the server. I modified your code so I hard code the servername and port in the project. can I do the same thing for login ID and password.
 
thanks.

AnswerRe: How to put your login and password in your code?membermarkw_socal30 Nov '01 - 7:22 
rena,
 
I modified _tmain() as follows, running on a Windows NT 4.0 PC, connected to a VAX system with VMS operating system.
 
markw_socal
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRet;
WSADATA wd;
int nPort;
// char strIP[256];
char * strIP = "999.99.99.999"; // Your IP address
char scr_line[80];
char strTitle[256];
SOCKET hSocket;
HANDLE hThread[2];
char FAR * loginname = "mylogin"; // Your login
char FAR * pwdname = "mypassword"; // Your password
 
nRet = AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
fputs("AfxWinInit()\n", d_dump);
 
if ( nRet == FALSE )
{
printf("\nAfxWinInit Fail..............!\n");
return 0;
}
 
nPort = IPPORT_TELNET;
SetConsoleTitle("Try To connect...");
WSAStartup(0x0101,&wd);

stdin1 = GetStdHandle(STD_INPUT_HANDLE);
stdout1 = GetStdHandle(STD_OUTPUT_HANDLE);
stderr1 = GetStdHandle(STD_ERROR_HANDLE);
hHeap1 = GetProcessHeap();
SetConsoleMode(stdin1,ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT );

// printf("\nPlease Enter IP/Domain.........:");
// scanf("%s",strIP);
// if ( strlen(strIP) < 3 )
// {
// printf("\nBad IP/Domain\n");
// getch();
// return 0;
// }
 
printf("strIP............%s :",strIP);
printf("\nPort...........%d :",nPort);

CSocketDx SocketDx(strIP,nPort);
hSocket = SocketDx.TelnetConnect();

if ( hSocket == NULL )
{
printf("\nUnable To Connect\n");
getche();
return 0;
}
 

CSocketRx SocketRx(hSocket,hThread[0]);
CSocketTx SocketTx(hSocket,hThread[1]);

wsprintf(strTitle,"Connecting to %s:%d",strIP,nPort);
SetConsoleTitle(strTitle);
 
send(hSocket, loginname, strlen(loginname) + 1, 0); // this code to log in
send(hSocket, "\r", 2 , 0); // separate "\r" so as not to
send(hSocket, pwdname, strlen(pwdname) + 1, 0); // erase the previous string
send(hSocket, "\r", 2 , 0);

// ...
}
 
mark in SoCal
Generaltelnet registry entriesmemberKannan Kalyanaraman2 Aug '01 - 0:10 
Hi,
I need to hide the OS information displayed when i telnet a server.(W2K)
Is there any registry setting in the server, which would prevent the telnet client (the one in Win2K) from giving out the OS information.
 

thanks
Kannan

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 1 Aug 2001
Article Copyright 2001 by Ibrar Ahmad
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid