http://www.perlmonks.org?node_id=602621

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

I have a problem with IO::Select. See the server.pl/client.pl listing below for how I try to use it. My problem is, that at first select does what it should, but then can_read(0) always returns something, which it should not.
I think you can understand what the problem is as soon as you look at the code+logfile. I can't find out how to solve this and I would appreciate any help.

The server:
#!/usr/bin/perl use strict; use warnings; use IO::File; use IO::Socket; use IO::Select; use Data::Dumper; my $sock = new IO::Socket::INET ( LocalHost => 'perlplexity.org', LocalPort => 1200, Listen => 5, Proto => 'tcp', Reuse => 1, ); die "Socket couldn't be created: $!" unless $sock; $sock->autoflush; my $message = ""; my $state = "UNCONNECTED"; my $fh = new IO::File "~/.somefile", "r"; my $select = new IO::Select(); # create handle set for reading $select->add($sock); # add the main socket to the set $select->add($fh); # add the main socket to the set while (1) { my @ready = $select->can_read(0); my $conn; my $buff; foreach my $rh (@ready) { print "GOT SOMETHING!\n"; if ($rh == $sock) { $conn = $rh->accept(); $select->add($conn); print "CONNECTION ADDED\n"; } elsif ($rh == $conn) { $buff = ""; my $rx_txt; while (length($rx_txt)) { $sock->recv($rx_txt, 1024); $buff.=$rx_txt; } $select->remove($rh); if ($buff eq "HELLO\n") { $state = "CONNECTED"; print "CONNECTED\n"; } else { $state = "UNCONNECTED"; print "UNCONNECTED\n"; $select->remove($rh); close($rh); } } elsif ($rh == $fh) { if ($state == "CONNECTED") { while (<$fh>) {$sock->send($_);} print $fh ""; } } } }
This is the client:
use strict; use warnings; use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => 'perlplexity.org', PeerPort => 1200, Proto => 'tcp', ); die "Socket could not be created. Reason: $!\n" unless $sock; $sock->send("HELLO\n"); close ($sock);
The logfile:
lofile, obtained from "perl server.pl > log" and the perl client.pl on + the other machine GOT SOMETHING! CONNECTION ADDED GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! ... this goes on until I interrupt it with ctrl-c
The question is: Why are there so many "GOT SOMETHINGS"?