Okay... I'm trying to finish my socket handling routine, and when I use the sub handle I want to close out the client socket, but when I use what you see it dies with the following message:
Unable to Close 'IO::Socket::INET=GLOB(0x1ab2e88)': Bad file descriptor
I believe I just need to call Close($client) correctly to make sure the $client is the TRUE filehandle name.
Any suggestions?
Thanks,
Mike Slattery
use strict;
use IO::Select ;
use IO::Socket;
use constant BUFSIZE => 1024 ;
sub logmsg { print "INFO: @_\n" }
my $port = shift || 2345;
my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => $port) ;
my $sel = new IO::Select($lsn) ;
my %inbuffer;
my %ready ;
my ($buf,$fh,$client,$new,$rVal) ;
my (@items) ;
logmsg "server started on port $port";
while (@items = $sel->can_read) {
foreach $fh (@items) {
# It's the Listen Socket
if ($fh == $lsn) {
$new = $lsn->accept ;
$sel->add($new) ;
# It's any Other Socket
} else {
$rVal = sysread($fh,$buf,BUFSIZE) ;
unless (defined($rVal) && length $buf) {
#Handle EOF
print "EOF: $fh\n" ;
delete $inbuffer{$fh};
$sel->remove($fh);
close($fh) ;
next ;
}
$inbuffer{$fh} .= $buf ;
#Check for complete request
if ($inbuffer{$fh} =~ /\n$/) {
$inbuffer{$fh} =~ s/[\015\012]+$// ;
push( @{$ready{$fh}}, $inbuffer{$fh} );
delete $inbuffer{$fh} ;
}
#Handle ready requests
foreach $client (keys %ready) {
handle($client);
}
delete $ready{$client};
}
}
}
sub handle {
# requests are in $ready{$client}
my $client = shift;
my $request;
foreach $request (@{$ready{$client}}) {
if ($request =~ /^quit$/) {
print "Client: '$client' Requesting QUIT\n" ;
$sel->remove($client);
close($client) || die "Unable to Close '$client': $!\n" ;
last;
}
print "Client: '$client' Req: '$request'\n" ;
}
delete $ready{$client};
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.