Eliminate junk chars after ReplaceStdio() in new telnet conn

Discussion to talk about software related topics only.
Post Reply
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

Eliminate junk chars after ReplaceStdio() in new telnet conn

Post by Ridgeglider »

I am getting what appear to be random junk chars when I first connect to a telnet connection and ReplaceStdio() for stdin, stdout, and stderr to that telnet channel. I'm trying to eliminate the junk chars, as they sometimes (rarely) interact badly with a command processor task my app is running that looks at those same character inputs.
My app does the following:

1) Several tasks outputting debug status messages to stdout on Com1 via printf() calls.

2) Another task that's a simple command processor that takes 1 letter inputs to a case statement using either fgets() or getchar() ie from stdin.

3) A 3rd task roughly modeled on the Serial2TCP examples that diverts IO from Com1 to a telnet connection when one becomes present. The priority for this task is higher (lower PRIO#) than the command processor task. This task listens for, and then accepts a telnet connection. When (fdnet >0), but before the select statement, I call:

Code: Select all

ReplaceStdio( 0,  fdnet ); 		//reroute STDIN from fdnet
 ReplaceStdio( 1,  fdnet ); 		//reroute STDOUT to fdnet
 ReplaceStdio( 2,  fdnet ); 		//reroute STDERR for fdnet
The end of the "while ( fdnet > 0 )" loop includes an OSTimeDly(1) to allow the other tasks to run.

Just after the ReplaceStdio() calls in the task that makes the telnet connection, I've tried:

Code: Select all

 while( charavail()) {
	 char junk;
	 junk = getchar();
}

This does not purge the junk, even if I add some delay time.

I've also tried similar code in the command processor when it first finds (fdnet>0). No luck in either case.
Ideas?

By the way, to make stdin work, I had to eliminate the telnet connection's 'read()':

Code: Select all

int n = read( fdnet, buffer, buflen );
that follows the select statement that processes input on fdnet. If these chars are read after the select trips, getchar() does not work, apparently because the chars get read before getchar can see them. Now, instead of calling read() I do nothing to allow these chars arrive at getchar() in the other task. I was wondering if there is a function equivalent to read(), but perhaps called spychars() that does reads without removing chars from the fd?
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: Eliminate junk chars after ReplaceStdio() in new telnet

Post by pbreed »

You are likely seeing telnet negotiation characters....

They will always start with the TELNET_IAC or 0xFF

See here for an overview...

http://mars.netanya.ac.il/~unesco/cdrom ... de300.html

You either want to do the negotiation... or at least parse enough to know what to ignore....
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

Re: Eliminate junk chars after ReplaceStdio() in new telnet

Post by Ridgeglider »

Thank you Paul.
I found that if I do a read() right after the fdnet accept() it clears those chars out very successfully so they never make it to stdin.

Out of curiosity, is there a function like read(), maybe called spy() that returns the chars to a buffer, but which does not remove them from the fd source?

That way I good look at them before they get to stdin

Now, if I try to do a read() after the select() fires, the chars get removed from the fd and never passed to stdin where they can be accessed by getchar() or fgets().
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: Eliminate junk chars after ReplaceStdio() in new telnet

Post by pbreed »

Doing replace stdio with a TCP connection is really problematic.
If the TCP connection closes and ever returns an error to the stdio
then stdio gets trashed and NEVER recovers until reset.

The right way to do this (and a I posted an example of this either here or on the yahoo boards a couple of years ago)
is to create your own custom fd and replace stdio with that then use a task to pend on both data going out stdio and in by whatever means that should get pumped from your special custom fd to/from whatever your using to do actual I/O.

This allows things like multiple telnet connections that all see the stdio output etc....

I should probably recreate this and capture it as an app note... alas I up to my eyeballs in a wifi driver and its likely
that at least three other things will be on fire by the time I come up for air... remind me in September and I'll write something up ;-)

Paul
roland.ames

Re: Eliminate junk chars after ReplaceStdio() in new telnet

Post by roland.ames »

I have used TCP connections (not telnet) to replace STDIO, without problems.

Have a look at the attached file.


I have cut this code from one of my projects, and I am very busy doing other stuff so apologies if I have left something out, the file won't compile as is, but the structure of the code is what you want.
Attachments
stdioswaptask.cpp
(4.15 KiB) Downloaded 267 times
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

Re: Eliminate junk chars after ReplaceStdio() in new telnet

Post by Ridgeglider »

Thanks again Paul. I hunted around and I believe this is your old code with the custom dual_fd??
Attachments
dualstdio.zip
(4.13 KiB) Downloaded 302 times
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: Eliminate junk chars after ReplaceStdio() in new telnet

Post by pbreed »

Yes that was the example I put together....

Paul
Post Reply