Thread: Getting cgi output to stagger...how?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    31

    Getting cgi output to stagger...how?

    Hi,
    I write all my cgi scripts in C. I'm trying to get my latest one to stagered its output to the webserfers browser. As in something like:

    Processing....
    10% complete
    ...
    ...
    50% compelte
    ...
    ...
    100% complete

    Like that to the web browser with time delays in between. Anyone kno whow to get a cgi script to do that?

    Thanks in advance.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    First, turn off OS buffering (don't remember the command, you'll have to google). Second, you can't guarentee that you will be successful, all you can do is try, as there is potential for a great deal of buffering between you and the browser, not to mention that the browser may do buffering as well. I did this some time ago, I will root around and post back if I can find the source.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    31

    Thanks

    Yes please do - any help greatly appreciated. I'm finding very little info available on this.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    This worked for me compiled on Linux with IE and Mozilla on Windows:

    Code:
    #include <stdio.h>
    
    int main(){
        int cnt = 0;
    
        setvbuf(stdout,NULL,_IONBF,0);
    
        printf("Content-type: text/html\n\n");
        printf("<html><body>\n");
    
        do {
            printf("Line number %d<br>\n", ++cnt);
            fflush(NULL);
            sleep(1);
        }while (cnt < 4);
        printf("</body></html>\n");
        return 0;
    }
    I am not sure of the web server; I don't think it matters that much as long as it meets the various standards.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    31
    Many thanks for your time and effort.

    This half works. It seems only the last flush works as the output only comes out in 2 stages. The first stage all the staggered outputs come out as one output but the very last data output does indeed delay and appear later as desired. Hmmm.... wish I knew how to fix this.

    E.g. Lets say I have 5 data outputs each with a delay between them. The first 4 still get lumped together as one output but the last one gets delayed

    I don't have a loop like you've got, I just have multiple flush statements sequentially after each other. This is not my actual code but an example of what I mean -:

    printf("Content-Type: text/html\n\n");
    printf("processing.....\n");
    fflush();
    sleep(5);
    printf(".....1....");
    fflush();
    sleep(5);
    printf(".....2....");
    fflush();
    sleep(5);
    printf("complete!<br>\n");
    fflush();

    For some reason after approx 10 secs this seems to output "processing..........1.........2...." and then 5 seconds later "complete!<br>\n".
    Any ideas how to get all of it to stagger properly?

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    If you run it on the command line and it works, and/or run a web server on the local machine and test it there and it works, then there is something betwixt your production server and your test browser that is caching the data and there isn't much you can do about it. There are some requests you can make as part of your header (where the "Content-type" goes) that asks that no one buffer the data, but they are requests only and anyone is free to ignore them. I have never used them, meta refresh is probably a better way to go for most browsers.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM