trouble with sockets...

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 3
Reputation: gginnj is an unknown quantity at this point 
Solved Threads: 0
gginnj gginnj is offline Offline
Newbie Poster

trouble with sockets...

 
-1
  #1
Sep 2nd, 2009
  1. #!/usr/local/bin/perl
  2.  
  3. use IO::Socket;
  4.  
  5. my $url = 'localhost';
  6. my $port = 6464;
  7.  
  8. my $error = 1;
  9. my $sock ;
  10.  
  11. while ($error) {
  12. my $sock = new IO::Socket::INET (
  13. LocalHost => $url,
  14. LocalPort => $port,
  15. Proto => 'tcp',
  16. Listen => 1,
  17. Reuse => 1 );
  18.  
  19. $error = 0 ;
  20. $error =1 unless $sock;
  21. }
  22.  
  23. print "Listener Port Established \n";
  24.  
  25. $error =1 ;
  26. while ($error) {
  27. my $new_sock = $sock->accept();
  28. $error =0 ;
  29. $error =1 unless $new_sock;
  30. }
  31.  
  32. print "Connection Established \n";
  33.  
  34. while (<$new_sock>) {
  35. print $_;
  36. }
  37.  
  38. close($sock);

What I get is:
Listener Port Established
Can't call method "accept" on an undefined value at /usr/mbin/testserv.pl line 27.

Any ideas? RH 8 linux (upgradeing OS is not an option)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: gginnj is an unknown quantity at this point 
Solved Threads: 0
gginnj gginnj is offline Offline
Newbie Poster

Re: trouble with sockets...

 
0
  #2
Sep 2nd, 2009
Apparantly, in my code, the "my $sock..." needed to
have the "my" removed. Since it was in a block, the
"my" was causing the variable to become localized,
and once outside the block, it didn't exist.

Once I remove the "my" and made the variable a
global one, it worked fine.

GG
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC