[En-Nut-Discussion] Nut as web server, a problem found and solved

Piotr Szlachta pszlachta at supermedia.pl
Thu Nov 1 23:01:35 CET 2007


Hi

My NutOs hosted web site

http://homeautomation.pl 

becomes more and more popular, and I'm facing new problems. I solved one
that is worth to share.

When multiply clients were connecting me at a time server content was not
displayed. 
Issue occurred when I increased amount of HTTPD threads to 25.


Problem was here: _fdopen failed to open file associated with socket.

We can do:

1. increase FOPEN_MAX to >= amount of HTTPD threads.

FOPEN_MAX is defined in nut_io.h


2. I believe we should change httpd.c example, as I changed my HTTPD thread.


is:
 
 if((file = _fdopen((int) socket, "r+b")))	{
   NutHttpProcessRequest(file);
   fclose(file);
 } 

Should be:

//
// if too many files opened switch thread and keep client connected
//
		
while(!(file = _fdopen((int) socket, "r+b"))) NutThreadYield();

if(file) {
  NutHttpProcessRequest(file, &socket->so_remote_addr);
  fclose(file);
} else 
    printf("failed to open.... ");


Or better:

// We can add timeout, just in case...

u_char timeout=0;
while(!(file = _fdopen((int) socket, "r+b")) && ++timeout<0xFF)
NutSleep(10);

if(file) {
  NutHttpProcessRequest(file, &socket->so_remote_addr);
  fclose(file);
} else 
    printf("failed to open.... ");




Regards
Piotr





More information about the En-Nut-Discussion mailing list