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

Oh Monks!

I have a problem with unholy sockets - I can listen on port and connect to it from localhost, but from a remote host - I cannot. When I try the same port with netcat on listen mode - no problem, so the port is not blocked

The pseudo server code:

#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Handle; use Fcntl qw(:DEFAULT); use POSIX qw(:errno_h); my $port = shift or return; my $sock = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => $port, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1); $sock or die "could not create socket: $!"; #.......listening on port print "___accepting on port $port...\n"; my ($new_sock, $c_addr) = $sock->accept(); my ($client_port, $c_ip) =sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($c_ip); my $client_host =gethostbyaddr($c_ip, AF_INET); print "got a connection from: $client_host [$client_ipnum] at port + $port\n"; close $sock; close $new_sock; exit;

When I try to connect I get:

telnet 212.31.100.100 20 Trying 212.31.100.100... telnet: connect to address 212.31.100.100: Connection refused telnet: Unable to connect to remote host

Any idea?

Replies are listed 'Best First'.
Re: Unable to connect remotely
by Corion (Patriarch) on Oct 11, 2009 at 12:05 UTC

    When you create your socket:

    my $sock = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => $port,

    you tell it to only listen on localhost. Which likely is not what you want. Maybe you want to omit the LocalHost parameter alltogether, so it listens on all interfaces?

      Indeed, your all-seeing-eye pointed my miserable mistake!

      Now works like a charm! Thank you oh mighty monks!