[En-Nut-Discussion] httpd + thread

Ulrich Hertlein ulrich.hertlein at artcom.de
Wed Sep 14 12:49:40 CEST 2005


Anpilogov Andrey wrote:
> I modified an example httpd and has added such thread:
> THREAD(TCPClient, arg)
> {
>    TCPSOCKET *sock;
>    FILE *stream;
>    char buff[128];
> 
>    for (;;) {
>        if ((sock = NutTcpCreateSocket()) != 0) {
> //            if (NutTcpConnect(sock, 
> inet_addr(Conf.server_ip),Conf.server_port) == 0) {
> //                if ((stream = _fdopen((int) sock, "r+b")) != 0) {
>                    NutSleep(15000);
> //                    fclose(stream);
> //                } else
> //                    puts("Assigning a stream failed");
> //            } else
> //              puts("failed");
>            NutTcpCloseSocket(sock);
>        }
>        NutSleep(1000);
>    }
> }
> 
> I start thus:
> NutThreadCreate("tcp", TCPClient, 0, 192);
> All works normally, but when I try to open CGI script device rebooted. 8 (

You're maybe running into the same trap that I ran into a few days ago, 
namely using too small a stack frame. The last arg in NutCreateThread() 
is the stack size in bytes so you're only using 192 bytes.

In the thread the 'buff[128]' array is allocated from the stack so 
you're at this point left with less than 64 bytes of stack. That's not a 
lot and can easily be consumed by OS functions (such as 
NutTcpCreateSocket) in which case it probably starts trashing 
memory/return addresses/whatever.

Try to increase your stack size (1024 bytes is what kept me out of 
trouble). Or make your 'buff' a global variable so it's not allocated on 
the stack.

/Ulrich

-- 
Ulrich Hertlein | Software Development

ART+COM AG
Kleiststr. 23-26 | 10787 Berlin | Germany

phone: +49.30.21001-433
fax: +49.30.21001-555
http://www.artcom.de



More information about the En-Nut-Discussion mailing list