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

First example in this book I can't get working for the life of me. I am on Win XP and trying to get it to print out the first line of the connection (page 6 of the book) using this code.

Anyone know what's wrong? No matter what server I try to connect to it says "Use of uninitialized value...". The code is

#!/usr/bin/perl #use warnings; #use strict; use IO::Socket; my $site = <STDIN>; my $fh = IO::Socket::INET->new($site); my $line = <$fh>; print $line;

Replies are listed 'Best First'.
Re: Network Programming With Perl example 1
by tlm (Prior) on May 01, 2005 at 02:40 UTC

    That's not surprising because you changed the code in the example! The actual code provided is:

    use IO::Socket; my $server = shift; my $fh = IO::Socket::INET->new($server); my $line = <$fh>; print $line;
    ...and it works fine:
    % perl lgetr.pl mail.hotmail.com:smtp 220 mc5-f26.hotmail.com Sending unsolicited commercial or bulk e-mail +to Microsoft's computer network is prohibited. Other restrictions are + found at http://privacy.msn.com/Anti-spam/. Violations will result i +n use of equipment located in California and other states. Sat, 30 Ap +r 2005 19:25:12 -0700
    To make it a bit more informative, I would change the line where $fh is initialized to
    my $fh = IO::Socket::INET->new($server) or die "Socket open failed: $^ +E\n";
    Then, if you give it an unavailable server, instead of trying to read from an unopened handle, the program prints out an intelligible error message and exits:
    % perl lgetr.pl wuarchive.wustl.edu:daytime Socket open failed: Connection refused

    Update: s/!/^E/.

    the lowliest monk

      Originally the code was verbatim but I changed it because it didn't work. Even using your code doesn't work.

      Now using

      #!/usr/bin/perl #use warnings; #use strict; use IO::Socket; my $server = shift; my $fh = IO::Socket::INET->new($server) or die "Error: $!"; my $line = <$fh>; print $line;
      and getting
      C:\Documents and Settings\admin\Desktop\Scripts>perl network.pl mail.h +otmail.com:smtp Error: Unknown error at network.pl line 8. C:\Documents and Settings\admin\Desktop\Scripts>

        Sorry, I forgot that on Windows you should use $^E instead of $! for the error message. This won't solve your problem, but at least it may get you a more informative error message than what you got with $!.

        the lowliest monk

        Works for me (on Windows 98 with Perl 5.6.1):
        (using your code)
        C:\perl>perl test.pl mail.hotmail.com:smtp 220 mc9-f39.hotmail.com Sending unsolicited commercial or bulk e-mail +to Microso ft's computer network is prohibited. Other restrictions are found at h +ttp://priv acy.msn.com/Anti-spam/. Violations will result in use of equipment loc +ated in Ca lifornia and other states. Sat, 30 Apr 2005 20:00:05 -0700

        chas
Re: Network Programming With Perl example 1
by jasonk (Parson) on May 01, 2005 at 17:55 UTC

    In addition to the good suggestions everyone else gave you, it's also entirely likely that this problem has nothing to do with the fact that you are running it on windows, and everything to do with the location of the windows server you are running it on. It is very common for ISPs to block all outgoing smtp traffic and force you to use their outgoing mail server (so you can't send spam from your house). Try opening up a command line and using telnet to connect to the hostname and port you are trying this on, it could very well be that no application is going to work on the hostname and port you are trying to connect to (but since you don't say what those are, it's tough to guess).


    We're not surrounded, we're in a target-rich environment!
      Hi,Thanks. I don't have a firewall on and I tried connecting via telnet;
      Microsoft Telnet> o mx.hotmail.com:smtp Connecting To mx.hotmail.com:smtp...Could not open connection to the h +ost, on po rt 23: Connect failed Microsoft Telnet> o hotmail.com Connecting To hotmail.com...Could not open connection to the host, on +port 23: C onnect failed Microsoft Telnet>
      Is this bad? And if the ISP is blocking this, there is NOTHING I can do at all so there's no point in reading the rest of the book?
        Have you tried checking with your ISP to see if they are blocking SMTP ports on your system? I know that my ISP does block me from sending mail using my work server via port 25. The server for my work account is specifically set to a different port to get around this problem. Might be what's happening to you.

        A lot of ISP's have done this to try to cut down on zombie spam being sent through their servers apparently.

        HTH!

        Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.

        You could always try to learn these things by using a shell account on a unix host that does have the ability to connect to these ports.

        Or even find out what you can do by accessing the ports to mailservers your network does allow.

        Squibbie Pooh Ski Doo.
        Hi, simply try the following in a dosbox: "telnet mx.hotmail.com 25" The syntax of telnet on win16/32-hosts is "telnet <host> <port> Regards, fritzo
        I don't mean any disrespect, but I would guess that you don't _know_ whether or not you have a firewall blocking your connection attempts. If you have XP, you're probably running SP2, and even after turning the included firewall off, that doesn't guarantee that all firewalling within XP has been removed (I remember reading lots of "issues" at Tech Support Guys where after the FW was supposedly turned off, things were still broken).

        Are you using a firewalling router between your machine and the internet? That is yet another possible place that needs investigation.

        Generally speaking, those that definitely have a clue are those that post details about what firewalls they do have, and what they've done to open those up to allow the testing they are attempting to do.

        -Scott

        As fritzo indicated, the error given back indicates that you are not connecting on port 25, but rather 23, which is telnet. The syntax you are using is not correct for Win*.

        Microsoft Telnet> o mx.hotmail.com 25 Connecting To mx.hotmail.com...Could not open a connection to host on +port 25 : Connect failed Microsoft Telnet>

        The connection failed due to my environment setup, I expected that, but the important point is that it failed on the correct port...
Re: Network Programming With Perl example 1
by Anonymous Monk on May 01, 2005 at 04:40 UTC
    OKAY GUYS!!

    I can't get it working on my WinXP system so I converted it to a CGI and uploaded to my linux web host and it DOES work. I sent the perl file to my WinXP friend and it doesn't work for his system either.

    Can someone tell me what I have to do to get it to work on Windows? It's important otherwise reading the rest of this book won't do me any good if I can't use it.

      Unknown error isn't very useful, but the error still has a number. To get the error number (which you can later look up on msdn) use int (as in int($!)  int($^E)).
      C:\>perl -e"$!=122;warn $!; $!=333;warn $!; die int $!" Unknown error at -e line 1. Unknown error at -e line 1. 333 at -e line 1.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

      If you are using XP sp2, try turning off the firewall while you test this. Also, some anti-virus programs like McAfee also block certain ports such as the smtp port from going out).