Post subject: help writing a telnet application in c++ builder 6
Iīm writting a telnet client in c++ builder to send some commands and receive some data from a remote machine. The problem is that with a kind of machine, the program doesnīt get the data sent by the remote machine.
I used the windows telnet, and it works, i get the data on my screen. Well, i used a packet sniffer named Packet Analyzer Professional (no merchandise here) and i can see that the same packets that arrive when iīm using windows telnet, arrive when i use my program. so, obviusly, the problem is in my code. here is a sample of the method i use to receive the data. i will be gratefull for any help.
This is the method that deal with everything that comes trough
the telnet connection, the IdTelnet5 object
char *cp = new char[ Buffer.Length() + 1 ];
char *p;
char cmd[20],info[10],vel_1[5],vel_2[5];
int cont=0, cont_2=0;
p=0;
strcpy( cp, Buffer.c_str() );
/*Start logging in the station where the data will be recovered
the timers are used to syncronize with the remote machine, because
i donīt receive some responses from some commands*/
start sending commands. comp_1 to 4 are the responses expected from the remote machine, a signal
for the program to send the next command*/
if(strstr(cp,comp_1))
{
if(flag_H == 1)
{
//command is sent using "WriteLn" ...
Form1->IdTelnet5->WriteLn("exit");
}
/*this is the command to log out after receiving the data required
it is the same for the next four comparissons. i have to send a exit, wait for an
answer, send another exit, etc. I send a total of four exits, then disconnect from the remote machine*/
/*this timer function is to mark if the answer arrived or not. if the time set expires
(it is set to 2,5 seconds) the program informs the user that the machine is unacessible
I think 2,5 seconds is time enough to the arrival of the answer, but i tried up to
10 seconds, and couldnīt get the answer. So, here is my problem: the answer is comming
from the remote machine to the local machine, but the program isnīt getting it. Is the
problem here or up in the code, where i get the buffer? someone can help ?? */
Timer4->Enabled = true;
/*when the data comes, i just split it and put in the screen
unfortunately, there are two versions of the same vendor, and
they respond diferent: one uses Downstream and Upstream, and the other
*/uses Down stream and Up stream ... just to make the code longer ...
Hi,
I was looking at your code. I'm not so sure if I would be of any help or not...but I will try.
I pretty much skipped the first part of your code as I have no idea what kind of command you are expecting or how those variables are being used(log_flag..etc).
But in the second part, you've used that while loop in every conditions.
Since, I don't know the reply command structure, I'm assuming whatever you are doing is right. but I have some reservation about this while loop. I've looked up the "strstr"(not a c++ guy) function. I'm not sure what you are doing here. Because, if that "\r" does not exist in that reply, you are stuck in the loop forever and eventually your machine will probably crush due to seg fault. On the other hand, if it does find it, then the condition is false and it never excute the internal part, which makes the later part totally useless.
I hope this would be helpful. I might be wrong in analyzing this. Try it and let me know. I would be interested to know what happened.
Sorry about the mess in the code, i didnīt know about the code brackets to show codes ...
Well, here is a sample of what i get from the machine iīm doing the telnet:
Code:
ANUCTO600(config-ADSL-0/1)#show line operation 1
Current line Operating Mode : g.dmt
Channel mode : Fast
Upstream channel bit swap : Disable
Downstream channel bit swap : Disable
Down stream channel rate(Kbps) : 768
Up stream channel rate(Kbps) : 128
Down stream channel noise margin(dB) : 42
Up stream channel noise margin(dB) : 31
Down stream channel attenuation(dB) : 8.5
Up stream channel attenuation(dB) : 4.5
Down stream max attainable rate(Kbps) : 8608
Up stream max attainable rate(Kbps) : 896
Total output power(dBm) : 11
It comes in the buffer, as a long char string. The char *strstr( x , y) function searches for the char string "y" in the char string "x" and returns a pointer for the first character of the "y" string in the "x" string. As you can see in the sample answer, i have to search for specifics strings, such as "Down stream channel rate(Kbps)". Then i must walk along the string until i find the " : " character. Then I read the information. The code you talked about is the part that gets this info thatīs after the " : ". Please ignore what is after the // in the code, that was a experience i was doing. After each line of info theres a carriage return combination (\r\n) that I use as a flag to stop reading the info.
Well, i identyfied the problem in the program using telnet. I donīt know the reason, but when the data package that comes from the remote machine contains the "\n\r" character, my program doesnīt get the whole package, just the piece that is before the "\n\r" character. The problem is what is this character?? I know that "\r\n" is the carriage return character, but i never saw "\n\r". Anyone has a clue ??
Tue May 04, 04 5:02 am
Guest
Post subject: Carriage Return
Saulo,
I am not the right guy for C. But I had to run into a carriage return problem in php, few days ago. I think, the following might be relevent to your problem. But you have to find the link, if there is any.
You are using two different platforms when you face the problem right? When editing text files in cross-platform environments, you must account for different line termination conventions.
Let's begin with a look at the various line termination conventions.
On UNIX, text file line-endings are terminated with a newline (\n), also referred to as a linefeed (LF).
On Windows, line-endings are terminated with a combination of a carriage return (\r) and a newline(\n), also referred to as CR/LF.
On the Mac Classic, line-endings are terminated with a single carriage return (CR). (Mac OS X uses the UNIX convention.)
[quote]
In a text file on Windows, each line is terminated with a carriage return character followed by a line feed character. When the file is read by a C program in text mode, the C library converts each carriage return/line feed pair into a single line feed character. If the file is read in binary mode, the program will see both the carriage return and the line feed.
You may have seen this distinction when transferring files between Unix and Window systems via FTP. You need to set the FTP program into binary or text mode as appropriate for the file you want to transfer.
When transferring a binary file, the FTP program simply transfers the data unchanged. When transferring a text file, the FTP program must convert each carriage return/line feed pair into a single line feed.
When using the C standard library, a binary file is indicated by adding b after the r, w, or a in the call to fopen. When reading a text file, the program can not simply count characters and use that when computing arguments to fseek.
[unquote]
Yes, i think youīre right. the program works like this: it makes a telnet to a Sun OS 5.8 system server. From that server, it makes another telnet, to the station I need to retreive the data. There are diferent types of stations the program needs to make this second telnet: some runs Unix-like OS, others run windows. All the computers that the program will be used runs some kind of windows (2000, XP ...).
I was thinking on what you sayed, and analising some data that comes to the programīs buffer, and sometimes i got messages like:
I think that the \r\n may come from the station, but since the program makes two telnets, do you know if each telnet appends a escape character of it own? so one uses \r\n as end-of-line/new-line and the other uses \n\r for the same purpose ?
Let me explain my situation, so maybe it becomes clear why iīm doing this program. Iīm studing Computer Engineering, in Brasil. Iīm in the fourth year, and there is a kind of job here (i think it exists all over the world) where they contract students, so they apply in the real world (outtside colleges) what they learn. Itīs called "estágio" here, in portuguese, and i donīt know the equivalent in english.
In this job, Iīm coninuing a program that a guy started, thatīs why iīm using c++ Builder 6. Anyway, iīm making this program alone, and I need a tool like the Builder, or the program wonīt be ready in usefull time. So, i found myself with two options: I resolve this problem using the IdTelnet component of C++ builder, or i write a socket class and implement it in the builder program. Iīve saw some programs written in c++ using Visual c++ that works with the stations Iīm having problems, and Iīm thinking about using it in the c++ builder. So, probably you people will see questions from me about writing socket programs in c++ ...
Thanks for the repply.
Wed May 05, 04 6:37 am
quantum Site Admin
Joined: 07 Mar 2004 Posts: 1048
Location: Dhaka, Bangladesh
Post subject:
I think that the \r\n may come from the station, but since the program makes two telnets, do you know if each telnet appends a escape character of it own? so one uses \r\n as end-of-line/new-line and the other uses \n\r for the same purpose ?
I do not know about that but I can suggest one thing to try. In various places the program is checking if the pointer has reached and end of line. Like this: while(!strstr(info,"\r"))//
But it is only checking for unix style line end. You can add an OR option like
while(!strstr(info,"\r"))||(!strstr(info,"\r\n"))||(!strstr(info,"\n\r"))
/*use appropiate c syntex here*/
So that it varifies all different kinds of line ends. It might work.
Iīm studing Computer Engineering, in Brasil. Iīm in the fourth year, and there is a kind of job here (i think it exists all over the world) where they contract students, so they apply in the real world (outtside colleges) what they learn. Itīs called "estágio" here, in portuguese, and i donīt know the equivalent in english.
I think, you are talking about internship. And, yes, it exists all over the world.
Iīve saw some programs written in c++ using Visual c++ that works with the stations Iīm having problems, and Iīm thinking about using it in the c++ builder. So, probably you people will see questions from me about writing socket programs in c++ ...
Any question is welcome and we will try best to help you. At least, I can tell you things from php/java perspective, that you can translate into C++.
BTW, you from Brasil hah! 3/4 yrs back I was really close to one from your country. and Sepultura is one of my ALL TIME favorite bands. Its Arise never went down from one of my three most favorite albums. _________________
Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Thanks for the help i received (Tousif and quantum) but iīm not going to use the builderīs component anymore. it doesnīt work. Iīm gonna try something else. Thanks again for the atention.
Fri May 07, 04 5:16 am
Tousif Guest
Post subject:
Hi Saulo,
Well..so looks like u've solved the problem.. great. I was really busy with my own project work, so wasn't able to check back on this until today.
I did some socket program in C during my BS in arizona. So, it would be a pleasure..in case you need any help...well..that would also refresh my memory as well :)
For Quantum,
I am really enjoying the lively discussion you guys are having here in this forum and looks like lot of people are actually participating..that's great. Thumbs up - u guys are doing an wonderful job. Hope this continues.