On 3/9/07, Matt <[EMAIL PROTECTED]> wrote:
snip
You're right, but does it slowdown the script much if if there are two?
I only ask because I didn't think about combining them into one line -
not questioning your perl know-how.
snip

It depends on where in the code you do it.  In your case you had a
"sleep 2" that made it pretty much a non-issue in terms of efficiency,
but if you get in the habit of using multiple function calls where you
only need one then you will inevitably write a tight loop that does
the same thing and slows you down.  Good form is important.

snip
Still isn't working though.  If I put the server interfaces down, then
of course it fails with the "Could not create socket: $!\n".  However,
if I start the script with the server interface active it starts to
iterate through the loop.  But if I then take the server interface down
it still iterates through, counting up and printing "Socket connected".
What am I missing here?
snip

The socket won't close until you try to write something to it and
fail.  What exactly are you trying to achieve?  If you are only
interested in whether a service is available then you should move the
socket creation inside the loop.  This is more polite anyway since you
won't be hogging a port.

#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket;

my $counter = 1;

while (1) {
       my $sock = IO::Socket::INET->new(
               PeerAddr => '10.5.7.33',
               PeerPort => '21',
               Proto => 'tcp',
       );
       last unless $sock;
       $sock->close;

       print "Socket connected :: $counter\n";
       $counter++;
       sleep 2;
}
print "Socket closed\n";

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to