in reply to New to perl: IO Select question :(
You never had the opportunity to accept any connection from the client side. Add $sock to IO::Select is no good, as it is not an "active channel" yet. Add the accept(), plus fix some minor syntax stuff, your code works.
use strict; use warnings; use IO::Socket; use IO::Select; my $sock = IO::Socket::INET->new( Proto=>"tcp", LocalHost=>"localhost", Listen=>16, Reuse=>1, LocalPort=>3000 ) or die("Could not create socket!\n") +; my $connection = $sock->accept(); my $readSet = new IO::Select(); $readSet->add($connection); while(1) { my @rhSet = IO::Select->select($readSet, undef, undef, 0); foreach my $rh (@{$rhSet[0]}) { my $buf=<$rh>; if($buf) { printf "$buf"; } else { $readSet->remove($rh); close($rh); } } }
Client side test code:
use IO::Socket; my $sock = IO::Socket::INET->new( Proto=>"tcp", PeerHost=>"localhost", PeerPort=>3000 ) or die("Could not create socket!\n") +; print $sock "abcd\n";
Well, I leave it to you to add back the code to accept further connections.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: New to perl: IO Select question :(
by Crackers2 (Parson) on Aug 21, 2005 at 03:17 UTC | |
by pg (Canon) on Aug 21, 2005 at 03:50 UTC | |
by Bob9000 (Scribe) on Aug 21, 2005 at 05:02 UTC |
In Section
Seekers of Perl Wisdom