matt.schnarr has asked for the wisdom of the Perl Monks concerning the following question:

I am curious to know if Select will maintain a buffer of all it's connections?

Below is an example of a TCP server that will receive data and then output the results to screen. The unfortunate scenario I am seeing is that there seems to be some form of buffer where I send one event in, and nothing appears on screen, however, if I send a second event in, then the first message shows up (so on and so on).

Can anyone explain why this is happening and is there a solution to this problem????

Thanks!

Matthew

#!/usr/bin/perl use Net::hostent; use IO::Socket::INET; use IO::Select; my $s = new IO::Select; $IP = '192.168.0.1'; $PORT = 15005; my $server = new IO::Socket::INET( LocalAddr => $IP, LocalPort => $PORT, Proto => 'tcp', Listen => SOMAXCONN, ReuseAddr => 1 ); # Listen for multiple connections. $s->add($server); $s->add(\*STDIN); print ">> $0 accepting connections on $PORT\n"; while ( my @ready = $s->can_read() ) { foreach my $fh (@ready) { # if($fh == \*STDIN){ $in_text = <>; # foreach my $fh (@ready){ print "$in_ip >> $in_text";} # } if ( $fh == $server ) { my $new = $server->accept; $s->add($new); my $hostinfo = inet_ntoa( $new->peeraddr ); # Show IP that just connected. print "CONN >> $hostinfo\n"; } else { #my $in_ip = inet_ntoa($fh->peeraddr);#error-prone my $in_ip = $fh->peeraddr ? inet_ntoa( $fh->peeraddr ) : "unknown"; my $in_text = scalar <$fh>; # If the client closes the connection, #remove this socket, but #keep the script running, waiting/serving #other connections. if ( $in_text ne "" ) { } else { $s->remove($fh); $fh->close; print "DISCONN >> $in_ip\n"; } # Every time "TEXT\n" is sent, display this print "$in_ip >> $in_text"; } } } print "Program end.\n"; #What its meant to do: Listen on a port #for connections, allow multiple #connections at once (hence the foreach() #forking), and when the client #quits, successfully closes the connection.

Replies are listed 'Best First'.
Re: Does "Select" have a buffer?
by Random_Walk (Prior) on Jan 25, 2005 at 18:34 UTC

    Perl will buffer output to the screen by default, this may be causing your problem. If you set the value of $| to 1 or $|++ then it will overide this behaviour. If you are being called by another script that may or may not want to keep output buffering then it is polite to local $|; $|++ so the caller is not upset.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Does "Select" have a buffer?
by borisz (Canon) on Jan 25, 2005 at 18:35 UTC
    Maybe the output of print is buffered. Put
    $|++;
    at the top of your program and retry.
    Boris