CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Isn't this the same code?

 



nonamae
New User

Apr 20, 2013, 7:56 PM

Post #1 of 5 (419 views)
Isn't this the same code? Can't Post

I am trying to code a socket recv retry on UDP socket, and when I try something like this, it works, even when there is no retry, only a "keep going"

Code
   $c->recv($rec, $maxlen); 
$pack_resp = new Net::Radius::Packet $dict, $rec;
next if not defined $pack_resp->code();

but when I try this:

Code
   my $retries = 0; 
do {
$retries++;
$c->recv($rec, $maxlen);
print "retry $retries on session $sessionnumber \n" if $debug;
$pack_resp = new Net::Radius::Packet $dict, $rec;
print "hello";
sleep 3;
} while ( (not defined $pack_resp->code()) and ($retries le 3) );
next if $retries gt 3 ;

it just stops working and the "hello" never gets printed.

What is the difference when I call a "new" object from inside a Do...while cycle and out of it?
Thanks in advance


Laurent_R
Veteran / Moderator

Apr 21, 2013, 2:54 AM

Post #2 of 5 (413 views)
Re: [nonamae] Isn't this the same code? [In reply to] Can't Post

Does it print the "retry ..." sentence?

If not, then it is most probably not going through the loop. If yes, try to change the 'print "hello";' line to 'print "hello\n";'

It might not solve your problem, but at least debugging will be clearer. For the rest, I do not know what the '$pack_resp->code()' instruction is supposed to return, so I can't help you on this part.


nonamae
New User

Apr 21, 2013, 7:53 AM

Post #3 of 5 (406 views)
Re: [Laurent_R] Isn't this the same code? [In reply to] Can't Post

Yes, it's going into the cycle, it prints: retry 1 session 1
then after that it doesn't print the hello, so I think the new method is not returning

$pack_resp->code() should return a integer if the socket received something, if not, i found on the first code to return me undef

$pack_resp->code()
sub code { $_[0]->{Code}; }


Laurent_R
Veteran / Moderator

Apr 21, 2013, 8:13 AM

Post #4 of 5 (404 views)
Re: [nonamae] Isn't this the same code? [In reply to] Can't Post

I can't really help on the socket thing, but one thing you should change is this:


Code
   } while ( (not defined $pack_resp->code()) and ($retries <= 3) );  
next if $retries >= 3 ;


because you really want numeric comparisons, not "ASCIIbetical" comparison.

By the way the last line ("next if...") should probably be removed, because it is not in the loop (unless it is part of a bigger loop that you haven't shown).

Did you try to change your print instruction to:


Code
   print "hello \n";



nonamae
New User

Apr 21, 2013, 7:14 PM

Post #5 of 5 (392 views)
Re: [Laurent_R] Isn't this the same code? [In reply to] Can't Post

Thanks to help me get a clean code.
Actually the problem disappeared when I turned off blocking socket, like this:


Code
# socket to connect from here to RADIUS server 
my $c = IO::Socket::INET->new (Proto => "udp",
PeerPort => $authport,
PeerAddr => $authhost,
Blocking => false #THIS WAS THE MISS =(
)
or die $!;


if you are curious, the final code, without erasing the prints

Code
sub pdef { defined $_[0] ? $_[0] : "UNDEF"; } 
...
while(true){
...
my $retries = 0;
do {
$retries++;
$c->recv($rec, $maxlen);
print "retry $retries on session $sessionnumber \n" if $debug;
$pack_resp = new Net::Radius::Packet $dict, $rec;
print "hello\n";
sleep $timeout;
print "timeout code: " . pdef($pack_resp->code()) . " \n";
} while ( ( not defined $pack_resp->code() ) and ($retries <= 2) );
msg timestamp ." ". "No answer from server $authhost:$authport", $verbose if not defined $pack_resp->code();
next if $retries >= 3 ; #
...
}#end while


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives