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

Hi Monks,

I am trying to write some simple server client scripts, I'm using some code I used about 8 years ago (and I'm sure it worked then). However I can't get the 2 to talk, where am I going wrong. The Listener appears to be working (no errors) although I can't see it under netstat. Where as teh connecting script comes back with 'Socket could not be created. Reason: Cannot assign requested address at ./test_connect.pl line 13.'. The 2 bits of code are below.

The Socket Listener:

#!/usr/bin/perl use IO::Socket; $sock = new IO::Socket::INET (LocalHost => '192.168.1.50', LocalPort => '2000', Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; print "\n Test Socket Listener has connected to port $Port\n_________ +__________ _____________________________________________\n\n"; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock);

and the connecting socket:

#!/usr/bin/perl use IO::Socket; # Set up Socket connection $sock = new IO::Socket::INET ( PeerAddr => '192.168.1.50', PeerPort => '2000', Proto => 'tcp' ); die "Socket could not be created. Reason: $!" unless $sock; print "\n Test Socket Listener has connected to port $Port\n_________ +__________ _____________________________________________\n\n"; print $sock "Hello"; close($sock);

Thanks

Monks

Replies are listed 'Best First'.
Re: Simple Sockets
by ikegami (Patriarch) on May 11, 2006 at 17:04 UTC

    netstat won't display the port, but netstat /a should. (In Windows)

    Are you in Windows XP, or do you run a firewall on your machine? It could be preventing the socket from being created or reached.

    According to the docs, <HANDLE> shouldn't be used with sockets. sysread should be used instead. I don't think that's the problem, though.

    In fact, it ran for on my system after I removed LocalHost => '192.168.1.50', from the server (and you probably shouldn't be using it either) and used '127.0.0.1' for the PeerAddr on the client.

      Thanks, I was doing it between SUSE and Solaris.

      Everything seems to work fine on SUSE.

Re: Simple Sockets
by Fletch (Bishop) on May 11, 2006 at 17:00 UTC

    Well, you're trying to listen on port 2000 and the error message tells you it can't get that port. That probably means something else is already running and listening on that port (and given I see an entry in /etc/services saying that's an assigned port it's entirely reasonable; not to mention 2000's close enough to 1024 that I don't doubt something else could have gotten in for outgoing traffic on a heavily used box).

    Pick a different (higher; try 9999) port number and see if you get the same error.

    Update: Aaah, it's your client script that's producing the error. Never mind me, I misread. Where's my caffeine . . .

    Hrmm, are you sure your listener's running? Does it print out the line after you check that $sock is defined? If you check with lsof does the process appear to have the socket open (presuming you're on some flavour of *NIX here)?

    I'd still try a different port just for kicks as well.

      Thanks, a higher port seemed to work on SUSE.

      For some reason it still doesn't work on Solaris, but never mind.

      Thanks again.