[En-Nut-Discussion] Implementing TCP disconnect timeouts
Greg Wood
greg.wood at shaw.ca
Sat Jun 24 07:17:08 CEST 2006
Hello everyone,
I am still poking around with my ethernut board to get an handle on the
fundamentals I'll need when I get started on a real project. I have
been trying to implement a mechanism to disconnect a TCP socket when
there is a timeout. I have implemented a simple timer management system
driven by TIMER2 overflow, this works quite well in its limited
capabilities. The trouble I am having is that the timeout will occur
correctly if the client (I'm using the Hercules setup utility to send
some raw bytes by TCP) is already disconnected. If I leave the client
connected my code will not exit and disconnect the port. My timer does
operate correctly (I can see a port bit change state when expires) and
as soon as I disconnect (assuming the timer has already expired) the
client my coded exits and disconnects the port. I have tried a number
of different things but nothing seems to work properly. Basically I
think the problem is that the fread function blocks until new data
arrives or the client disconnects. My Code is below. The TCP thread is
created, opens the file/port and calls Dump_TCP. When Dump_TCP returns
the port and file is closed.
How could I implement the functionality I am after? What I am thinking
in the future is a timeout for a telnet session so if there is no
activing for 5 minutes or so it would disconnect.
Regards,
Greg Wood
void Dump_TCP(FILE *data, HANDLE timeout)
{
unsigned char TCP_data[256];
int size=0;
unsigned char i,lines, chars;
int end_of_file=1;
NutSleep(125);
HANDLE timer1;
printf("Displaying Socket Data\n");
reset_timer(&disconnect_timeout);
TCP_timeout=0;
for(;;)
{
fflush(data);
PORTB|=0x40;
while(size==0&&TCP_timeout==0) <----PROBLEM
SECTION
{
NutThreadYield();
printf("Size %d %d", size, TCP_timeout);
size=fread(TCP_data, 1, sizeof(TCP_data), data);
}
PORTB&=0x40;
if(TCP_timeout==1)
{
TCP_timeout=0;
printf("disconnect timeout\n");
break;
}
//size=fread(TCP_data, 1, sizeof(TCP_data), data);
//if(size==0)
// continue;
reset_timer(&disconnect_timeout);
lines=size>>3;
chars=size&0x07;
for(i=0;i<lines;i++)
{
printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", TCP_data[i*8],
TCP_data[i*8+1],TCP_data[i*8+2],TCP_data[i*8+3],TCP_data[i*8+4],TCP_data
[i*8+5],TCP_data[i*8+6],TCP_data[i*8+7]);
//fprintf(data,"%02X %02X %02X %02X %02X %02X %02X %02X\n",
TCP_data[i*8],
TCP_data[i*8+1],TCP_data[i*8+2],TCP_data[i*8+3],TCP_data[i*8+4],TCP_data
[i*8+5],TCP_data[i*8+6],TCP_data[i*8+7]);
}
lines<<=3;
for(i=0;i<chars;i++)
{
printf("%02X ",TCP_data[lines+i]);
//fprintf(data,"%02X ",TCP_data[lines+i]);
}
printf("\n");
//fputs(TCP_data, data);
size=0;
}
}
THREAD (TCP,arg)
{
TCPSOCKET *port;
FILE *data;
user_timer_start(1000,1,&disconnect_timeout,&disconnect,0);
for(;;)
{
printf("Creating socket on 12345\n");
port = NutTcpCreateSocket();
NutTcpAccept(port, 12345);
data = _fdopen((int) port, "r+b");
//reset_timer(timeout);
Dump_TCP(data,arg);
fclose(data);
NutTcpCloseSocket(port);
}
}
--
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.
Version: 7.1.392 / Virus Database: 268.5.6/338 - Release Date:
12/05/2006
More information about the En-Nut-Discussion
mailing list