Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Stupid IO::select question

by BlaisePascal (Monk)
on Aug 03, 2000 at 21:08 UTC ( [id://26029]=perlquestion: print w/replies, xml ) Need Help??

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

Here's the thing...

I've got a socket, created with IO::Socket::INET, that I've registered with an IO::select object.

I'm writing to the socket using the following code:

if (my @s = $select->can_write()) { for (@s) { if (print $_ $message) { print "message sent"; } else { print STDERR "Could not write to socket: $!\n"; } } }
I only have one socket registered with the select, so it only writes to the one socket. This code works fine, except...

The server I'm talking to has the annoying habit of terminating the connection with no warning. When that happens, the $select->can_write() is not blocking, but immediately returns the one registered socket (which was closed from the other end), and the print fails.

Is can_write() supposed to return a handle that I clearly cannot write to? If so, what suggestions can I get for bulletproofing my code?

Replies are listed 'Best First'.
Re: Stupid IO::select question
by ferrency (Deacon) on Aug 03, 2000 at 21:42 UTC
    Disclaimer: I haven't actually tried this.

    But, you might want to try a has_exception() call to see if there's an error condition on the socket(s) before printing to it (them).

    Alan

Re: Stupid IO::select question
by tye (Sage) on Aug 04, 2000 at 08:29 UTC

    It almost makes sense. select is supposed to tell you when a call won't block. Having the write fail instead of blocking, it makes sense that select needs to return (otherwise the select would just hang forever if you didn't give a timeout).

    Reading the IO::Socket module, I was bit surprised that waiting for more than one condition type isn't easier. But anyway, I'd do this:

    my $canWrite= IO::Select->new( $sock ); my $hasError= IO::Select->new( $sock ); my( $aWrite, $aError )= IO::Select->select( undef, $canWrite, $hasError ); for( @$aError ) { warn "Error on socket ($_).\n"; } for( @$aWrite ) { if( print $_ $message ) { print "message sent.\n"; } else { warn "Can't write to socket ($_): $!\n"; } }
    and see what happens. Then you have the error loop do recovery based on what behavior you see, etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://26029]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found