There are a few ways to handle this, and it partly depends on what you're wanting. Chances are your hang is due to the system blocking your read call when there's nothing to be read. Any way you can keep this from happening will probably work.
If you go with alarm(), I'd probably either not handle the signal or just use the handler to clean up a bit. There are better ways to handle the case of nothing to read on a socket. Still, if your network glitches are infrequent but usually last a while, alarm() is probably your best bet. No reason to waste cycles and memory for a process to try to recover if the network's not back up soon.
If your network glitches are short, using nonblocking reads or the select() function may be your best option. With a nonblocking read, you just check to see if you're getting as much data as you expected, and loop with a sleep() or something until you do. The select() function (four-argument version) lets you see if there's anything to read before trying the read. If there's nothing to read, your program can log that info and sleep a while. You'd still want to check the amount of data you're getting once you do perform a read.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.