Link to home
Start Free TrialLog in
Avatar of maloriopolium
maloriopolium

asked on

Best way to read from stdin

Hi,

I would like to know if there is a better method to read from stdin than the current one I have.

This is what I currently use:


But the problem is, some very long strings seems to get truncated from time to time and I don't receive the entire string being sent.
Can someone please help me identify what could be the problem?

A bit of background:
My program listens to input on stdin. It is called by Microsoft telnet server because MS telnet server is responsible for listening to incoming telnet connections. So everytime something is picked up by MS telnet server, MS telnet server then calls my program which receives the string as stdin. So either my code to read from stdin is not very well written, or could it be that there is a problem with MS telnet server passing me a truncated string?

I need help on this urgently.

Thanks very much.
string str = "";
CStdStringA bufs = "";
 
while(true)
{
         fflush(stdin);
 
	while(((ch = getchar()) != '\r'))
	{
 
		if(ch == '\n')
		{
			break;
		}
		else
		{
			bufs.append(1, (char) ch);
			i++;
		}
	}
         str = bufs;
         bufs = ""; //Reset the string to empty
 
          .....//Code to handle the str.....
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maloriopolium
maloriopolium

ASKER

Hi jkr,

Also I've noticed that the truncation seems to only occur for extremely long strings eg: strings with over 1020+ characters. Could it be that MS telnet server is somehow buffering packets and adding a \n at the end of the packet before sending the next packet?

Well, neither the telnet server not the CStrings should be responsible for that. On the other hand, if you reassemble that data on the receiving side, is anything missing (I fear my optimisim is wearing off if so)?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops, get rid of that first getline() in my code snippet =)
Hi Iwinkenb,

You think it's better without the fflush(stdin)? Ok I will try that, but not sure if the problem will go away, you see, this truncation problem is reported by a customer and only happens intermittently, usually when a very long string is encountered.

>Also, why not read in a line simply as:
This is because the strings that are coming in are always terminated by a '/r' character. This is part of a SIP2 protocol standard, so that is by I am using getchar() to do things character by character until the '/r' character is found.
jkr,

Can you please tell me where do I place the call to setvbuf(stdin, NULL, _IONBF, 0);?
>> This is because the strings that are coming in are always terminated by a '/r' character.

        getline(cin, str, '\r');

reference page :

        http://www.cplusplus.com/reference/string/getline.html




I don't know the SIP protocol, but I know that telnet provides ways to negotiate a maximum line length. Maybe that's what happens ? The 1020 you mentioned is pretty close to 1024 which is an often used boundary. Network packets for example could be limited to that size. How do you receive the string ?

Did you verify that the whole string is actually SENT ? Use a sniffer to monitor network traffic and/or a debugger to monitor the application.
Hi Inifinity08,

>>Did you verify that the whole string is actually SENT ? Use a sniffer to monitor network traffic and/or >>a debugger to monitor the application.
The hard part with all this is that I cannot reproduce the problem on my work machine. I start a telnet localhost session and send the exact same string in a command prompt window, press 'Enter' and the string is always received in its entirety. The truncation only happens at the customers site.
They use a epayment kiosk to collect payments from users. From what I know, it is a web application which communicates via a telnet session. I use Microsoft Telnet Server to handle the telnet connection. Is there a way to specify the max line length with Microsoft Telnet Server?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or in short, all information you can gather about this problem is a step closer to the solution ;)
>>You think it's better without the fflush(stdin)?
Yes.  Read http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351 for more information on the subject.
>>Can you please tell me where do I place the call to setvbuf(stdin, NULL,
>>_IONBF, 0);?

Right after the corresponding 'fopen()'.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
      Split: jkr {http:#20811996} & lwinkenb {http:#20812165} & Infinity08 {http:#20813160}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Sean
EE Cleanup Volunteer