Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI Timeout

Status
Not open for further replies.

davewelsh

Programmer
Jun 26, 2001
71
0
0
CA
Hi. I posted this to the C++: Microsoft forum, but haven't had a reply yet.

I want to make a cgi program that sends a file to the user. I have made this program and it works when I test it. (I have a 2Mbps connection and the web server is pretty fast.)

I got a call from the host a couple days ago that said my program was timing out. Since I have tested the code several times, I figure this is not due to a crash. I think the problem is that people on dial up who want to download something take several minutes to download the file, and as such my CGI program doesn't end for several minutes.

My question is, is there some asynchronous way to send the file to the user? The way I do it now is by a loop that keeps reading data from the file into a buffer and then writing it to stdout. (Read, write, read, write, etc.) It seems to work for me, but I think the printf statements are synchonous (i.e., they wait until the data is sent to the user before execution continues) and take too long on a dail up connection.
 
When you receive a new connection, spawn a thread which will handle the file send. Your main thread can then continue to listen for new connections.
 
Are you sure about that? I don't think I can have a program running continuously. The way the program works is like this:

Code:
int main()
{

  // read the data from stdin (via a helper library YACGI)

  // do some validation on the data

  // open file

  // write http headers to stdout (file type, size,...)

  while (reading data from file)
  {
    // write data to stdout
  }

  return 0;
}

But I figure the problem is the while loop because a person with a slow speed will stay in this loops for minutes. I believe that any server-side code (asp, cgi, etc.) has to finish within a certain period of time and this is where the problem lies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top