Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to make a chat script based on SOCKET.It listen() to a port and when a user come it accept() it and builds a NEW_SOCKET contacting with the user(print the web page). The parent process fork() a child process that will print to the NEW_SOCKETand use pipe() talk to the child. The child section looks like: ############
$connect=0; $readpipe="READ$connect"; $writepipe="WRITE$connect"; pipe($readpipe,$writepipe); $writepipe->autoflush(); my $pid; if (!($pid=fork)) { close $writepipe; $readpipe->autoflush; select(NEW_SOCKET); while(1) { my $words; my $rbits=''; vec($rbits,fileno($readpipe),1)=1; select($rbits,undef,undef,undef); $words=<$readpipe>; print "<br>$words"; } close NEW_SOCKET; exit; } $connect++; close $readpipe; $writepipe->autoflush(); $pipes{$pid}=$writepipe;
############### First it worked well lasting for 3 or 4 minutes.But suddenly it was out of control.I think "select($rbits,undef,undef,undef);" statement lose his effect and he didn't like to wait for the filehandle become ready any more.The child print innumerable "
" to user's browser Madly and Continually! Now I am stranded.Can you give me a lesson?Thank you.

Replies are listed 'Best First'.
Re: Can you tell me why?
by chromatic (Archbishop) on Feb 23, 2000 at 21:27 UTC
    In my experience, accept() returns a valid filehandle -- you can print directly to it without having to use a pipe. I would look into the HTTP::Daemon module from libwww-perl. It takes care of most of the dirty low-level work.

    See also Randal Schwartz's Web Techniques columns, particularly 23 and 24. He builds a fork()ing mini web server there.

    If you're determined to keep your existing code, I would look at your while loop. There seems to be no way to exit it, and perhaps the socket is timing out.