Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How can I distinguish between a 'connection refused' and 'connection timed out?'

( #25634=categorized question: print w/ replies, xml ) Need Help??
Contributed by reyjrar on Aug 02, 2000 at 03:31 UTC
Q&A  > network programming


Description:

I have this:
$SIG{"ALRM"} = sub {close(SOCKET); }; alarm '5'; socket(SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp')); $iaddr = inet_aton($host); $paddr = sockaddr_in($myport, $iaddr); if (connect(SOCKET, $paddr)) { $SIG{"ALRM"} = sub { close(SOCKET); &outage($host); exit(0); }; alarm '5'; socket(TS, PF_INET, SOCK_STREAM, getprotobyname('tcp')); $paddrr = sockaddr_in($myport, $iaddr); connect(TS, $paddrr); recv (TS, $buff, $responselength, ''); if ($buff eq $myresponse) { &do9094stuff($host,$region); exit(0); } else { #print "$host: Hung Port Detected!\n"; close(SOCKET); &outage($host); exit(0); } } else { &outage($host); }
I know that the distinguishing needs to be done in that last else, but I have no idea where to start..

Answer: How can I distinguish between 'Connection refused' and 'Connection timed out?'
contributed by tye

$!, of course. If you use Errno qw(ECONNREFUSED ETIMEDOUT); then you can do:

if( $! == ECONNREFUSED() ) { ...connection was refused... } elsif( $! == ETIMEDOUT() ) { ...connection timed out... } else { ...some other error... }

Under Win32, connect() is a "winsock" call, not an operating system call, so it doesn't set errno. However, the glue code inside perl.exe that calls connect() copies the value from WSAGetLastError() into errno.

Unfortunately, this doesn't work all that well. For example, "$!" will show as "", making you think that 0==$!. Also, the above code won't work since the error codes are not ECONNREFUSED and ETIMEDOUT but WSAECONNREFUSED and WSAETIMEDOUT so the code that builds the Errno module doesn't know to make those available.

So, if you want your code to port to Win32, you can add something like:

BEGIN { if( $^O =~ /Win32/i ) { eval ' sub() ECONNREFUSED { 10061; } sub() ETIMEDOUT { 10060; } 1;' or die $@; } }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (13)
    As of 2014-10-30 23:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (211 votes), past polls