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

Good afternoon,
I have been playing around with perl sockets (IO::Sockets) but have been experiencing some unexplained problems. I replicated the code from:
http://www.perlfect.com/articles/sockets.shtml
and have looked through the documentation about the sockets module at:
http://search.cpan.org/author/GBARR/IO-1.20/IO/Socket.pm
The below is my implimetation of the code from the first web site:
This first bit if code is the sending portion of the socket (cliant)
#!/usr/bin/perl use strict; use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => '10.2.7.193', PeerPort => '7788', Proto => 'tcp', ); die "Could not create socket :-( $!\n" unless $sock; print $sock "hello world!"; close($sock);
The next bit of code is the recieving portion of the socket code (server)
#!/usr/bin/perl -w use strict; use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => '10.2.7.193', LocalPort => '7788', Proto => 'tcp', Listen => 2, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(defined(<$new_sock>)) { my $storage = $_; print $storage; } close($sock);
When I execute the recieving program on the server machine and then execute the sending program on the cliant machine, I would expect that the cliant would send the text "hello world" to the server machine. I, however, am recieving the following error message instead:
Use of uninitialized value in print at ./socket_serv line 17, <GEN1> l +ine 1.
I am new to socket programming, but do realize that is very powerful. If anyone has any ideas of what might be wrong or any resources that might help me out, I would be very appreciative of your help. Thank you for your time in looking at my problem. Have a nice day :-)

Replies are listed 'Best First'.
Re: Perl Sockets Problem
by cbro (Pilgrim) on Jul 07, 2003 at 21:40 UTC
    Your client is fine (and most of your 'server' too).
    Using Data::Dumper, I very quickly found out that $storage (actually $_) is not defined the way you tried. Why don't you try something like this:
    my $new_sock = $sock->accept(); while (my $storage = <$new_sock>) { print $storage . "\n"; $new_sock = $sock->accept(); } close($sock);
    That will give you a loop until you manually kill the script. If you just want to receive one message then take out the $new_sock = $sock->accept() that's inside the while loop. If you want to keep the socket open for multiple messages, but also want to make sure the socket is cleanly closed (which my above example does not do). Write a SIG handler that makes sure the socket gets closed upon receiving a SIGHUP, SIGTERM, etc.
    HTH,
    Chris
      Thanks! That looks like a good way to keep a continuous connection open between the two hosts. This should be very helpful for my application. I intend to use perl to fork off processes in a cluster. Looks like sockets has some good potential. Thanks again.
      Joe
Re: Perl Sockets Problem
by tcf22 (Priest) on Jul 07, 2003 at 21:38 UTC
    Using defined returns true, but won't set $_ (at least I don't think that it will).
    Try this
    my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock);
      Wow! Thanks a bunch. That seemed to work. I guess sometimes it's the little things that make the differance. Now I have something to work with. Thanks again.
      Joe