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

I'm having some trouble getting my first foray into socket programming to work. This, I expect should open a socket on host 'foo' port 1880. This seems to work except, when I do a 'netstat -a' I don't see an entry for port 1880 which I expect to see listening. Here is the code:
#!/usr/bin/perl -w use strict; use IO::Socket; my $new_sock; my $buf; my $sock = new IO::Socket::INET (LocalHost => 'foo', localPort => 1880, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Unable to create socket: $!" unless $sock; print "Waiting for message...\n"; while ($new_sock = $sock->accept()) { print $buf while (defined ($buf = <$new_sock>)) } close ($sock);
Further more when I try the folowing code on another machine I get a 'Connection refused' error:
#!/usr/bin/perl -w use strict; use IO::Socket; my $sock = new IO::Socket::INET (PeerAddr => 'foo', PeerPort => 1880, Proto => 'tcp' ); die "Could not create socket: $!\n" unless $sock; while (1) { print "Enter message: "; chomp (my $msg = <STDIN>); print "Sending $msg\n"; print $sock "$msg\n"; $sock->flush; } close ($sock)
These examples come from Advanced Perl, So I suspect they are correct.
Any suggestions will be greatly appreciated!

Update:
The server code is running on a windows nt4 machine while the client is an HPUX 11.x box

UPDATE:
I've got it working!

I noticed that while setting up the $sock object I did

my $sock = new IO::Socket::INET (LocalHost => 'sgilbertdt', localPort => 1200, Proto => 'tcp', Listen => 5, Reuse => 1 );
The localHost shouldbe LocalHost.

As soon as I made the change and ran the script again, it showed up in netstat and I was able to pass the message from my Unix client.

Spelling Counts

Thanks Everyone

TIA

Sweetblood

Replies are listed 'Best First'.
Re: Trouble with sockets
by eXile (Priest) on Jun 21, 2004 at 14:33 UTC
    Is your machine really called 'foo' and is this hostname resolved correctly to an IP-address belowing to your machine?

    If I try your code on my machine (replacing 'foo' with 'localhost') it runs fine. Maybe you should try IP-addresses instead of hostnames. If you use a hostname, a machine will always try to resolve this to an IP-adress before it uses it (using DNS or another resolving mechanism), it's just one more step that can go wrong.

    Hope this helps
      The machine name is correct, though per your suggestion I did try LocalHost in the server script and IP in my client script. Unfortunetly, I got the same results, 'Could not create socket: Connection refused' and I still do not see the connection doing 'netstat -a'.

      Thanks anyway!

      Sweetblood

        mmm, maybe my post was a bit confusing. When I tried 'localhost ' on my own machine, I did that because my machine's name is not 'foo'. You better put the same IP-address in both scripts. If you put 'localhost' in the 'LocalHost' hash-key to build a new socket this socket will not be accessible from other machines on the network (under normal circumstances).

        But even with LocalHost => 'localhost' in your script I'd at least expect you to see a listening port when you do a 'netstat -a'. Maybe it's better to do 'netstat -an' so your TCP and UDP portnumers are not translated to servicenames. Another way to see if you've got a listening TCP port is to use telnet to connect to this service. For instance to check a connection on TCP port 1800 on host 1.1.1.1:

        telnet 1.1.1.1 1800
        This will give you either a (useless) connection or a connection refused.

Re: Trouble with sockets
by pbeckingham (Parson) on Jun 21, 2004 at 14:29 UTC

    I had the same problem. It turned out that I had firewalls on both machines that prevented my two programs from communicating.

      Both machines are on the same network and not behind any firewalls. I can connect to and from these machines using other protocols such as telnet, ftp, UNC, etc. I think the problem is with the winnt4 machine running the server code, since I don't see a connection using netstat -a. I believe I should see an entry for that port listed as 'Listening'.

      Thanks!

      Sweetblood

        My machines were both in the same room, on the same router and subnet. Firewall software on both machines (Linux and OS X) prevented the connection.

        This code will give you a functional server you can telnet into. Don't use it for anything but testing:

        #!/usr/bin/perl -w use strict; use IO::Socket; my $sock = IO::Socket::INET->new( Listen => 1, LocalAddr => 'localhost', LocalPort => 9000, ) || die "Unable to create socket: $!\n"; print "Waiting for message...\n"; while ( my $fh = $sock->accept()) { print while <$fh>; } close ($sock);

        cheers

        tachyon