buffer the output

Hai Friends

I am writing a cgi program in C. The CGI program has a link to start my tcp chat server... The tcp chat server prints some information when it starts... I have captured the informations and sent to the client browser through the cgi program... But the output is displayed all atonce...But my need is that i have to print the string immediately like how it gets printed on the terminal when i start the server.....

Can anyone help me with this issue...and provide me a detailed explanation

Thanks in advance

Use setvbuf to unbuffer the output. man page

I tried this.. But still its of no use.... Any other way..?

main()
{
int i;
setvbuf(stdout,NULL,_IONBF,0);
printf("Content-Type: text/html\n\n");
printf("Hello");
printf("Hai");
printf("Bye");
sleep(5);
printf("Hello");
printf("Hai");
printf("Bye");
}

My output in browser should be like
Hello Hai Bye
(and after few seconds)
Hello Hai Bye

Collins

I could not even get that to compile. I had to add a line
#include <stdio.h>

At that point, I got the timing effect that you wanted but the output is:

$ ./xx
Content-Type: text/html

HelloHaiByeHelloHaiBye$

You need to add some spaces and newlines to the printf's.

Run your program in the unix environment from the terminal. Do you still see line buffering? If so, your stdio package has a bug. If it works from the terminal but not in the browser, then the buffering must happening elsewhere.

Hai

I have mensioned in my question itself that i am using CGI program and i need to get the output in the browser.... I have added all the stdio.h and the <br> tag to get the output in the new line... But my exact requirement is that i have to get the output as

Hello Hai Bye
(after some time)
Hello Hai Bye

In the Browser when i call the cgi program which is written in C.

Please try this ....

add <html> </html>
to print the text on the browser ...

Following may work .... ; I have n't tried as there is no env :slight_smile:

main()
{
int i;
setvbuf(stdout,NULL,_IONBF,0);
printf("Content-Type: text/html\n\n");
printf("<html>") ;
printf("Hello");
printf("Hai");
printf("Bye");
sleep(5);
printf("Hello");
printf("Hai");
printf("Bye");
printf("</html>") ;
}

hai bhargav

No bhargav

Still its not working.... Can you suggest me any other idea....

Thanks
Collins