PDA

View Full Version : C++ Coding: Telnet in a program, interesting issue


mero42
06-24-2005, 06:39 PM
Hi. I haven't seen many questions like this, so I don't even know if it is possible to do the following:

I am writing a program that will connect to another machine (my Direcway cable modem) and restart it without the user actually entering any text. Using
system("telnet 192.168.0.1 1953"); works just fine, but how would I get the program to enter a string of text (rr) that reboots the modem? And afterwards, how would the program revert out of telnet to display all the "you have succesfully rebooted your modem" messages?
Here is the code I have so far:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
std::cout<<"Welcome to the Direcway Restart Program" <<endl;
std::cout<<"-----------------------------------------" << endl;
char User_Choice;
std::cout<<"Would you like to restart your Direcway terminal? (Y, N)" <<endl;
std::cin>>User_Choice;
if (User_Choice =='Y') {
std::cout<<"Restarting your Direcway Terminal..." <<endl;
system("telnet 192.168.0.1 1953");
std::cout<<"You have succesfully restarted your Direcway Terminal" <<endl;
system("PAUSE");
}
else if (User_Choice =='N') {
cout<<"Ok. Exiting Program." <<endl;
system("PAUSE");

}
else if (User_Choice != 'Y' || 'N'){
std::cout<<"Fatal error. Exiting program. Please enter either \"Y\" or \"N\" next time. "<<endl;
system("PAUSE");
}
}

I hope someone can help... :thumbsup:

aman
06-24-2005, 09:46 PM
Having never created a telnet client myself before, I would offer some suggestions...

You'll need to do some research on telnet commands and how to use them.

I'm not sure the system() method you are using will do you much good without additional code... how do you plan on communicating with the new terminal you open from your program?

You may be better off learning a bit of telnet protocol and creating your own basic telnet client yourself.

mero42
06-24-2005, 09:58 PM
Hmm. When restarting the modem manually, I use the command prompt to access it. When you are connected, there are different two-letter commands you can use to do various things...
I would have no idea where to begin if I were to create my own telnet client, but how would I integrate that into my program?
Edit: Maybe "terminal" isn't the right term to use in this program...

aman
06-25-2005, 10:18 PM
Telnet client is pretty simple, it consists of just opening a connection (sockets) and sending/receiving the proper commands.

I just tried it another way and it works fine. All I did was create a new command prompt via CreateProcess(), get my new window's HWND, and send all the messages I want to it. I can easily start a telnet session and send commands this way, and it's probably easier than learning the telnet protocol :)