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

Hello all,

I am a self taught Perl novice, trying to use IO::Socket on a Windows 2000 server. (I know, I know... let the flames begin...) My code works, but I am limited by a system or stack "default" Timeout setting. Apparently, there is a bug in the Windows version of IO::Socket::INET, to the point where the Timeout property is commented out.

I have even attempted to write code which does not use the ::INET portion of the module, but I still can't seem to implement a Timeout. The "default" timeout that is applied to my program is 23 seconds (go figure). I need something as small as 2 or 3 seconds.

Additionally, I can't seem to find the property for "connected", when just using IO::Socket. So, now I'm really stuck - no Timeout value, AND no "am I connected?" return.

I need to make a Client connection to an established Server, to verify that the Server (TCP port) is running.

I am in a crunch for a resolution to this. Any and All help is greatly appreciated. Thank you!

My last attempt at creating the Socket is like so:

use IO::Socket; $server = new IO::Socket; $proto = getprotobyname('tcp'); $server->socket(PF_INET, SOCK_STREAM, $proto); $internet_addr = inet_aton($myIPaddress); $paddr = sockaddr_in(135, $internet_addr);

Replies are listed 'Best First'.
Re: Seeking IO::Socket Advice
by pg (Canon) on Nov 18, 2003 at 17:29 UTC

    No, it is not broken. I tested with this piece of code on windows 2000 server, and worked:

    use IO::Socket::INET; use strict; use warnings; my $s = new IO::Socket::INET(Proto => "tcp", LocalPort => 3000, Timeou +t => 2); while (1) { my $c = $s->accept(); print time, "\n"; }

    And this is what I got:

    1069176374 1069176376 1069176378

    BTW, even if you use level socket calls, you still can set timeout by calling setsockopt, check this out under perlfunc. There are couple of timeout related settings, for example SO_RCVTIMEO and SO_SNDTIMEO.

Re: Seeking IO::Socket Advice
by ptkdb (Monk) on Nov 18, 2003 at 17:21 UTC
    I'm not sure what you want to do, just to connect to the server to see if it's alive? No traffic, transactions etc? If the socket is not there(i.e. the server isn't running) a tcp socket will fail immedidately with the infamous and hurtful "connection refused"(ECONNREFUSED)

    You might want to spend some time reading the man page on IO::Socket::INET

    Perl provides a WONDERFUL built in variable $! that is kind of a combination of errno and strerror. You can check the result of your connection with this variable in addition to getting an undef back to clarify WHY it didn't connect:

    use Errno ; # for ECONNREFUSED use IO::Socket::INET ; # portions copies from the IO::Socket::INET man page: $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org', PeerPort => 'http(80)', Proto => 'tcp'); if( $sock == undef && $! == ECONNREFUSED ) { # server not operating? }
Re: Seeking IO::Socket Advice
by DTOakey (Initiate) on Nov 18, 2003 at 20:19 UTC
    I should also supply some details. I'm restricted as to my environment. I am using Perl 5.005, and IO::Socket ver 1.1603. These, unfortunately, cannot be changed.

    Thanks to the comments and suggestions already supplied. However, my problem still exists. On my Windows server, "ECONNREFUSED" doesn't appear to work as you might expect (not at all?). Also, the connection I'm trying to make is going out over a WAN link, (I don't know if this matters or not.)

    As for the suggestion to use the Timeout and then use the "accept" method, is this not testing a Server port on my own server? What I need to do is use a Client Connection to test a Server over the WAN.

    Below is the code I used. I can fill in a value for Timeout all I want, but it appears to be ignored.

    use IO::Socket::INET; $server = '192.168.27.236'; $p = new IO::Socket::INET ( PeerAddr => $server, PeerPort => 135, Proto => "tcp", Timeout => 5 ); if ($p) { print "\tPINGed $server at ".scalar(localtime).".\n"; } else { print "\tFailed to PING $server at ".scalar(localtime).".\n"; }
    What I get is, (notice the amount of time until "failure"):
    Started at Tue Nov 18 15:03:02 2003. Trying to RPCPing 192.168.27.236 at Tue Nov 18 15:03:02 2003. Failed to PING 192.168.27.236 at Tue Nov 18 15:03:25 2003. Finished at Tue Nov 18 15:03:25 2003.
    A successful response is almost immediate.

    Again, Thanks!

Re: Seeking IO::Socket Advice
by hanenkamp (Pilgrim) on Nov 18, 2003 at 17:56 UTC
    I am a self taught Perl novice, trying to use IO::Socket on a Windows 2000 server. (I know, I know... let the flames begin...)

    We, as Perl monks, may prefer Unix (I prefer Linux), but it's good news for Perl when it's used anywhere and everywhere. If anyone were to flame you, shame on them. Especially, since Perl's motto is TMTOWTDI!