|
|
Title |
A class to communicate via Telnet.
|
Summary |
This note provides a class that can be used to establish and communicate with a Telnet server. |
Contributor |
Tim Slattery
|
Published |
1-Mar-2001 |
Last updated |
1-Mar-2001 |
|
|
Download Telnet files - 9 Kb
Introduction.
CHookup allows a C++ program to initiate a Telnet connection to another
computer. The program can then send commands and retrieve the results of
those commands. CHookup works well with the Sun Solaris system. I've had
it connected to a Linux system, but since I no longer have access to a
Linux system, I make no guarantees. I've had reports of problems with
other systems, but I haven't had time or resources to track them down.
Any such problems probably centre in the negotiation phase
(CHookup::TelNetOpts ). When the connection starts the two computers
negotiate to decide which will do what (will I echo, will you echo, what
terminal type are you, etc). Different systems do this somewhat
differently. Since I've had access to Solaris machines, I've made the
negotiations work with them. Other systems may require tweaks in
TelNetOpts .
Logging
When CHookup's constructor runs, it looks for a file named hookup.ini .
This file can be used to specify a logging file. If it exists, it looks
like this:
[hookup]
LogFile=c:\hookup.log
You can use any filename in place of c:\hookup.log , of course. If the
constructor finds this file, all data sent to and received from the
remove computer will be written to this file. If the file already
exists, the new data will be appended to the end. The log file should be
used only for debugging! If you create hookup.ini and forget about it,
the log file will continually grow, eating disk space and slowing
execution of CHookup functions.
Implemenation note
This class does not use CSocket, instead it calls wsock32.dll functions.
That means that you will have to add wsock32.lib to your project to
compile it.
I don't have anything against CSocket. This class was originally a DLL.
The DLL was written in C (not C++), and therefore did not use CSocket.
Rather than implement CSocket and rewrite all the wsock32 calls, I took
the easy way and left those calls alone.
Methode:
CHookup::CHookup(bool bMessages)
The constructor takes a boolean argument. If "true", then error
messages will be displayed in message boxes. If "false" messages will
not be displayed. (I used CHookup in an ISAPI DLL. In that environment,
trying to display a message box causes the server to hang. That's the
reason for this argument.)
bool CHookup::Login(CString sHost, CString sUser, CString sPassword)
Login establishes a connection to a host. The return value is "true" if
a connection was made, "false" if it failed.
CString CHookup::DoCommand(CString sCommand)
sCommand is sent to the remote host, and the result is returned.
sCommand must be terminated with "\n" (newline character).
There is no "logoff" method. The class destructor ends the connection.
Usage normally looks something like this:
CHookup TN;
CString sResult;
TN.Login("host", "user", "password"); /* supply the right values, of
course */
sResult = TN.DoCommand("ls\n"); /* sResult now has the result of the
"ls" command, v=followed by a
prompt. Note that the command is
terminated by a newline. */
|