This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use the exception set in select (connect()) to early return when remote end is busy...
[perl5.git] / dist / IO / lib / IO / Socket.pm
index 9938c78..06e4e6c 100644 (file)
@@ -23,7 +23,7 @@ require IO::Socket::UNIX if ($^O ne 'epoc' && $^O ne 'symbian');
 
 @ISA = qw(IO::Handle);
 
-$VERSION = "1.31";
+$VERSION = "1.32";
 
 @EXPORT_OK = qw(sockatmark);
 
@@ -118,10 +118,21 @@ sub connect {
            my $sel = new IO::Select $sock;
 
            undef $!;
-           if (!$sel->can_write($timeout)) {
-               $err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
-               $@ = "connect: timeout";
-           }
+            my($r,$w,$e) = IO::Select::select(undef,$sel,$sel,$timeout);
+            if(@$e[0]) {
+                # Windows return from select after the timeout in case of
+                # WSAECONNREFUSED(10061) if exception set is not used.
+                # This behavior is different from Linux.
+                # Using the exception
+                # set we now emulate the behavior in Linux 
+                #    - Karthik Rajagopalan
+                $err = $sock->getsockopt(SOL_SOCKET,SO_ERROR);
+                $@ = "connect: $err";
+            }
+            elsif(!@$w[0]) {
+                $err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
+                $@ = "connect: timeout";
+            }
            elsif (!connect($sock,$addr) &&
                 not ($!{EISCONN} || ($! == 10022 && $^O eq 'MSWin32'))
             ) {
@@ -493,6 +504,16 @@ an AF_INET socket the value of &AF_INET will be returned.
 Unified method to both set and get options in the SOL_SOCKET level. If called
 with one argument then getsockopt is called, otherwise setsockopt is called.
 
+=item getsockopt(LEVEL, OPT)
+
+Get option associated with the socket. Other levels than SOL_SOCKET
+may be specified here.
+
+=item setsockopt(LEVEL, OPT, VAL)
+
+Set option associated with the socket. Other levels than SOL_SOCKET
+may be specified here.
+
 =item socktype
 
 Returns the numerical number for the socket type. For example, for