Eliminate junk chars after ReplaceStdio() in new telnet conn
Posted: Tue Jul 17, 2012 2:14 pm
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: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:
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()':
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?
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
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 );