943,852 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 751
  • Perl RSS
Sep 2nd, 2009
-1

trouble with sockets...

Expand Post »
Perl Syntax (Toggle Plain Text)
  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)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gginnj is offline Offline
3 posts
since Sep 2009
Sep 2nd, 2009
0

Re: trouble with sockets...

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gginnj is offline Offline
3 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: substitution regex question
Next Thread in Perl Forum Timeline: how to compare two directories that contain xml files in perl??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC