[En-Nut-Discussion] Telnet server with echo and cursor key support
Hugo Simon
hugo.simon at gmx.de
Sat Jul 5 15:47:05 CEST 2003
Hi.
Now I have built a simple read line function that reads a line of characters
from a stream, supporting the BS key and an echo. It works fine on
stdin/stdout routed to the uart0 device. But with a TCP Telnet socket
connected to a stream there came some unreadable characters on connection
and on pressing return. I don't know why. Maybe someone have a look at the
code and give me some hint.
Thank you
Thorsten
/*
* read a line from a stream with echo and BS support
*/
void readln(char *line, u_char len, FILE *stream)
{
u_char x=0;
char ch;
*line=0;
do
{
ch=fgetc(stream);
switch (ch)
{
case 8:
if (x>0)
{
line[--x]=0;
fputc(ch,stream);
fputc(' ',stream);
fputc(ch,stream);
}
break;
case '\n':
fputs("\n",stream);
break;
default:
if ((ch>=0x20) && (ch<=0x80))
{
if (x<len)
{
fputc(ch,stream);
line[x++]=ch;
line[x]=0;
}
}
}
} while (ch!='\n');
}
/*
* Process client telnet requests.
*/
void ProcessRequests(FILE *stream)
{
static char buff[128];
/*
* Send a welcome banner.
*/
fputs_P(banner_P, stream);
for(;;)
{
/*
* Flush output and read a line.
*/
fflush(stream);
readln(buff, sizeof(buff), stream);
puts(buff);
file://Processline(buff,stream);
}
}
/*
* Telnet Server Thread
*/
THREAD(Telnet, arg)
{
TCPSOCKET *sock;
FILE *stream;
/* loop endless for connections. */
puts("Starting Telnet Daemon...");
for(;;)
{
/* Create a socket. */
if ((sock = NutTcpCreateSocket()) != 0)
{
/* Listen on port 23. If we return, we got a client. */
printf("Waiting for a telnet client...");
if (NutTcpAccept(sock, 23) == 0)
{
puts("connected");
/*
* Open a stream and associate it with the socket, so
* we can use standard I/O. Note, that socket streams
* currently do support text mode.
*/
if((stream = _fdopen((int)sock, "r+b")) != 0)
{
/* Process client requests. */
ProcessRequests(stream);
puts("Disconnected");
/* Close the stream. */
fclose(stream);
}
else
puts("Assigning a stream failed");
}
else
puts("failed");
/* Close our socket. */
NutTcpCloseSocket(sock);
}
}
}
More information about the En-Nut-Discussion
mailing list