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

Hi. Using IO::Socket::INET for the first time, i have an interesting problem, when my client is on the same host thant the server, it works, otherwise, the socket connection is refused. This is kinda weird since when using non object modules (ie. Socket, Carp...) and the plain commands: socket() connect() accept(), it works... Here is my client:
#!/usr/bin/perl -w use IO::Socket; my $sock = new IO::Socket::INET ( PeerHost => shift || 'localhost', PeerPort => 7070, Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; print $sock "Hello there!\n"; close($sock);
And the server:
#!/usr/bin/perl -Tw use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => shift || 7070, Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $socket; my $data; for ( ; $socket = $sock->accept(); close $socket) { while ($data = <$socket>){ print $data; } } close($sock);

Replies are listed 'Best First'.
Re: Socket problem
by gbarr (Monk) on Oct 03, 2001 at 18:33 UTC
    On the server side you have LocalHost => 'localhost' which tells the socket to listen only on the ip for localhost (127.0.0.1), so the server is not listening on your network IP. Remove that line and it the server will listen on all IP addresses the machine has.
      Well, okay... It works. I thought LocalHost was the hostname/ip of the listening machine... Many thanks :o)