Further to your /msg; the reason I didn't offer you any code is because i don't use *nix; and using non-blocking and select on Windows is sufficiently different that it makes it non-portable and I don't have the knowledge to point out the differences; or support the transition.
However, here is the code for a very simple Windows select-mode server. It demonstrates the basic mechanisms involve though some of the details (the IOCTL call etc.) are not necessary or applicable on *nix. Maybe it will give you an overview:
#! perl -slw
use strict;
use IO::Socket;
use IO::Select;
use constant PACKET => pack 'C*', 0 .. 255;
print "Perl -v: ", $];
print "IO::Socket: ", $IO::Socket::VERSION;
print "IO::Select: ", $IO::Select::VERSION;
printf "%s\n", unpack 'H*', PACKET;
my $noBlock = 1;
my $lsn = IO::Socket::INET->new(
Reuse => 1, Proto => 'tcp', Listen => 100, LocalPort => 12345
) or die "Server failed to create listener: $^E";
print "Listener created";
ioctl( $lsn, 0x8004667e, \$noBlock );
binmode $lsn;
my $sel = IO::Select->new( $lsn );
while( 1 ) {
warn 'Registered handles:', $sel->handles;
warn 'Count: ', $sel->count();
my @ready = $sel->can_read(10);
warn "handles ready:[ @ready ]"; <STDIN>;
for my $ready ( @ready ) {
if( $ready == $lsn ) {
my $client = $lsn->accept or next;
binmode $client;
ioctl( $client, 0x8004667e, \$noBlock );
$sel->add( $client );
warn "Added $client";
}
else {
{
$ready->recv( my $in, 4, 0 ) or warn( "re
+cv failed: [$^E]" ), last;
length $in == 4 or warn( "Wr
+ong length: [$^E]" ), last;
my $bytes = unpack 'N', $in;
$ready->recv( my $dataIn, $bytes, 0 ) or warn( "re
+cv failed: [$^E]" ), last;
length $dataIn == $bytes or warn( "Wr
+ong length: [$^E]" ), last;
$dataIn eq PACKET or warn( "Da
+ta corrupt: [$^E]" ), last;
printf "Got: (%d) '%s'\n", $bytes, unpack 'H*', $dataI
+n;
$ready->send( pack( 'N', 256 ), 0 ) or warn( "Se
+nd failed: [$^E]" ), last;
$ready->send( $dataIn, 0 ) or warn( "Se
+nd failed: [$^E]" ), last;
}
$ready->shutdown( 2 );
$ready->close;
$sel->remove( $ready );
warn "Removed $ready";
}
}
}
close $lsn;
Alternatively, you should do a site search (google: "site:perlmonks.org IO::Select"). IDs to look for are zentara and pg; bioth of whom have posted what I believe to be well written select-mode server code for *nix.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
|