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

use strict; use warnings; use IO::Socket; use Cwd; my $host = 'localhost'; my $port = 2110; my $socket = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => "tcp", Type => SOCK_STREAM ) or die( "Cannot connect to $host:$port : $@\n" );
Every time I run this I get "Cannot connect to host:port:". Any idea what I did wrong?

Replies are listed 'Best First'.
Re: IO::Socket not connecting
by dave_the_m (Monsignor) on Nov 21, 2004 at 16:14 UTC
    or die( "Cannot connect to $host:$port : $@\n" );
    That should be $!, not $@

    Dave.

      Thanks, I changed that and it says what it did before along with: - Unknown error
        I bet you're on Windows, then. Try printing $^E. $! prints 'unknown error' when there isn't a UNIX equivalent for the Windows error message found in $^E.
Re: IO::Socket not connecting
by pg (Canon) on Nov 21, 2004 at 18:04 UTC

    It is clear that your code has no problem, other than the minor issue dave_the_m pointed out, which was unrelated. The only answer is that the other party is not listening.

Re: IO::Socket not connecting
by dba (Monk) on Nov 21, 2004 at 16:49 UTC
    Can you give more information about the host you are connecting to?
    Is there a firewall running on the box?
    Are you trying to connect to a TTY/VTY port to a *nix host?
Re: IO::Socket not connecting
by gman (Friar) on Nov 22, 2004 at 14:21 UTC
    Can you try using telnet?
    #>telnet host port

    if you can connect with telnet you can connect with your
    application. But as pg stated, it apears that you have no
    application listening on port 2110.

    gamn