• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7184
  • Last Modified:

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

0
maloriopolium
Asked:
maloriopolium
  • 4
  • 3
  • 3
  • +2
3 Solutions
 
jkrCommented:
Hm, there's hardly a "best way", yet you could try to  set the buffering mode to "unbuffered", e.g. by calling

setvbuf(stdin, NULL, _IONBF, 0);

apart from that, all input shpuld be delivered without dropping any data.
0
 
maloriopoliumAuthor Commented:
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?

0
 
jkrCommented:
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)?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lwinkenbCommented:
Have you tried removing fflush(stdin)?  Even if that isn't causing your problem, you should remove it anyways.  According to the c++ standard, the results of doing that are undefined.

Also, why not read in a line simply as:
std::string s;
getline(std::cin,s);
 
while(true)
{
        s.clear();
        getline(std::cin,s);
 
        .....//Code to handle the str.....
}

Open in new window

0
 
lwinkenbCommented:
oops, get rid of that first getline() in my code snippet =)
0
 
maloriopoliumAuthor Commented:
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.
0
 
maloriopoliumAuthor Commented:
jkr,

Can you please tell me where do I place the call to setvbuf(stdin, NULL, _IONBF, 0);?
0
 
Infinity08Commented:
>> 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.
0
 
maloriopoliumAuthor Commented:
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?

0
 
Infinity08Commented:
>> Is there a way to specify the max line length with Microsoft Telnet Server?

I assume so since it's foreseen in the telnet protocol. The line length is agreed upon during the connection set-up afaik. You can again sniff the network to check that (if you don't have access to the sending side).

Do you have access to one of the machines where the problem occurs ? Can you monitor network traffic on their system ? Do you have logging functionality in your application ?

If you can't re-produce it on your end, then chances are that you have to look for the "problem" in the differences between your test setup, and the real setup at the client site.
0
 
Infinity08Commented:
Or in short, all information you can gather about this problem is a step closer to the solution ;)
0
 
lwinkenbCommented:
>>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.
0
 
jkrCommented:
>>Can you please tell me where do I place the call to setvbuf(stdin, NULL,
>>_IONBF, 0);?

Right after the corresponding 'fopen()'.
0
 
SeanDurkinCommented:
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
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now