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

I am attempting to use the "Multiplexing Using Select" Code on page 195 of the Advanced Perl Programming Guide, however, I am unable to see any results from the given code.

I am sending data to the specified port, but nothing is being printed to screen. What am I missing? Am I just completely off?

I am attempting to be able to send data from multiple clients to a single server and have the data sent printed to screen.

Thanks!

#!/usr/bin/perl -lw # # # Define Modules use IO::Socket; use IO::Select; #Establish Server Socket $main_sock = new IO::Socket::INET (LocalHost => 'C285', LocalPort => 9325, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $main_sock; #Process Received Data $readable_handles = new IO::Select(); $readable_handles->add($main_socket); while (1) { # Select() blocks until a socket is ready to be read ($new_readable) = IO::Select->select($readable_handles, undef, und +ef, 0); # If it comes here, there is atleast one handle # to read from. foreach $sock (@$new_readable) { if ($sock == $main_socket) { $new_sock = $sock->accept(); # Add to the list, and go back to select because the # new socket may not be readable yet. $readable_handles->add($new_sock); } else { # It is an ordinary client socket, ready for reading. $buf = <$sock>; if ($buf) { print $buf; } else { # Client closed socket. Need to do the same thing # here and remove it from $readable_handles list. print "Position = 6\n"; $readable_handles->remove($sock); clost($sock); } } } }

Replies are listed 'Best First'.
Re: Multiplexing using Select
by zentara (Cardinal) on Jan 10, 2005 at 21:11 UTC
    Sometimes it is easier to just give a standard working example, rather than explain why a snippet isn't working. I'm guessing it's your while(1)...but I didn't try your code.

    A working server:

    #!/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.

    I'm not really a human, but I play one on earth. flash japh
Re: Multiplexing using Select
by BUU (Prior) on Jan 10, 2005 at 23:47 UTC
    Your code *seems* correct, have you tried disabling output buffering via the $| variable?