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

According to the pod,

If Listen is defined then a listen socket is created, else if the socket type, which is derived from the protocol, is SOCK_STREAM then connect() is called.

but when I run the code in the first anon block below, absolutely nothing happens. No errors are returned, an IO::Socket handle is returned (see the output below), but neither my firewall nor my packet sniffer show any activity whatsoever. (AS 5.6.1 and/or AS 5.8 under NT4/sp6a)

The second anon block uses Socket.pm to connect, write and read from the same host:port and works fine. I see everything go through the sniffer.

What is wrong with the first block of code?

#! perl -slw use strict; { use IO::Socket::INET; my %socket = ( Host=>'www.perl.org', Port=>80, Proto=>'tcp', Timeout=>10, Type=>SOCK_STREAM ); my $socket = IO::Socket::INET->new(%socket) or die $!; print $socket; close $socket or die $!; print 'IO::Socket::INET closed'; } { use Socket; select STDOUT; print 'About to open socket'; socket(SH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die $!; print 'socket opened'; my $dest = sockaddr_in(80, inet_aton('www.perl.org')); connect(SH, $dest) or die $!; print 'connected'; select SH; $|=1; print SH 'GET / HTTP/1.0' . "\cM\cJ\cM\cJ" or die $!; select STDOUT; print 'sent'; print <SH> or die $!; print 'received'; }

Output

D:\Perl\test>test IO::Socket::INET=GLOB(0x1c19ce8) IO::Socket::INET closed About to open socket socket opened connected sent HTTP/1.1 200 OK Date: Thu, 24 Apr 2003 05:00:15 GMT Server: Apache/2.0.44-dev (Unix) DAV/2 Last-Modified: Wed, 16 Apr 2003 15:55:53 GMT ETag: "6fcce-3-95229440" Accept-Ranges: bytes Content-Length: 3 Connection: close Content-Type: text/html; charset=ISO-8859-1 x2 received

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
•Re: IO::Socket:INET doesn't attempt to connect
by merlyn (Sage) on Apr 24, 2003 at 05:44 UTC

      Thanks merlyn++ Not only did that work fine, it also made me realise my error in the long form.

      If I make up the parameter names instead of using the ones in the POD, they aren't going to be recognised.


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.
Re: IO::Socket:INET doesn't attempt to connect
by Zaxo (Archbishop) on Apr 24, 2003 at 05:44 UTC

    This statement:

    my $socket = IO::Socket::INET->new(%socket) or die $!;
    doesn't do what you want. &IO::Socket::INET::new does not work with or die $!. It returns a (true) reference to a Symbol whether or not the socket's connection was established. You may want to check defined( $socket->connected()), inherited from IO::Socket.

    After Compline,
    Zaxo

      Thanks Zaxo. In my defense, the 5.8 IO::Socket::INET pod contains this example:

      $sock = IO::Socket::INET->new(PeerPort => 9999, PeerAddr => inet_ntoa(INADDR_BROADCA +ST), Proto => udp, LocalAddr => 'localhost', Broadcast => 1 ) or die "Can't bind : $@\n";

      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.