in reply to Ping and check for server running on port 27015

On another help site I frequent someone suggested using something like the following to ping the server and port.
#!/usr/local/bin/perl use IO::Socket; ## Array of ports that you want to check. $port = "27015"; $remoteip = "www.your_server.com"; print "\nChecking for service at $remoteip\n"; print " "; print "=" x (length($remoteip) + 25); print "\n\n"; $connected = 0; $checkport = IO::Socket::INET->new( PeerAddr => "$remoteip", PeerPort => "$port", Proto => 'tcp', Timeout => '0') or $connected = 1; if (!($connected)) { print " Port $port is up.\n"; } else { print " Port $port is down.\n"; } close $checkport;
What do you think, will this work?

Replies are listed 'Best First'.
Re: Re: Ping and check for server running on port 27015
by Maclir (Curate) on Jan 24, 2001 at 03:32 UTC
    While i haven't a comment on the socket side of your code, you may want to reverse the logic of your $connected flag. Intuitively, if I see a flag such as "$connected", the assumption is that a true value would mean we are conencted.

    Then you can say something like:

    if ( $connected) { print " Port $port is up.\n"; } else { print " Port $port is down.\n"; }
    That also means you will have to reverse the logic in your "or" modifier, but your program will more intuitive and easier to maintain.