in reply to Re: Socket connect curiosity
in thread Socket connect curiosity
How shall we determine if sleep() is sleeping for a lot more than your prescribed 3 seconds? Let's try some debugging messages! e.g.
else { $attempts++; print("Trying again ... ($attempts) \n"); print( scalar localtime() . "About to sleep\n" ); sleep(3); #sleep 3 seconds before trying again print( scalar localtime() . "Finished sleep\n" ); }
I would bet London to a brick you'll discover the sleep function is just fine. So what else could cause delays in your loop? That connection() function gets called every time so how about we put some debugging messages around that? e.g.
print( scalar localtime() . "About to connect\n" ); $connected = connect(SOCK, $paddr); print( scalar localtime() . "Finished connect\n" );
To make your life a bit easier you might want to autoflush messages so that they appear as soon as you print them. Stick the following at the top of your script:
select( ( select(STDOUT), $| = 1 )[0] ); select( ( select(STDERR), $| = 1 )[0] );
Next you'll probably ask how you can implement a time-out mechanism on your connect. That's a two part problem: controlling how long the DNS look up takes (when DNS breaks resolving addresses can take a long time) and controlling how long the connect will wait. For now I won't advise how to deal with this problem; I hope I've given you some insight as to how to determine what the problem you're actually facing is.
|
|---|