You could (but I don't really recommend) turn on TCP keepalive for the socket in question. See the setsockopt function for how to flip socket options. The SO_KEEPALIVE is the socket option I mean: (google this for more details, but here's a reasonable description: http://www.unixguide.net/network/socketfaq/4.7.shtml). As that link mentions, you may then have more work to change the keepalive timeout from 2 hours, but on Linux check your /proc/sys/net/ipv4/tcp_keepalive* settings.
This should make the socket go bad before you come to write to it. But really, what's the point of that? You're still going to have to do error handling on the print/write anyway (to catch other errors, or a server reboot which occurs after your last keepalive but before your write).
If you write to a broken socket on Unix/Linux, your application should receive a SIGPIPE, which is ordinarily fatal. Install a signal handler for that (or ignore - but it's better to handle+log on SIGPIPE) and make sure you check errno (a.k.a. $!) for EPIPE errors after you do any write to a socket. If you try to write a request and it fails, and errno is EPIPE, then reconnect and try again.
This is the low-level description of what your (Unix) app needs to do. Various higher-level convenience layers may exist for your protocol, depending on what it is.
(There's lots more low-level info in places like "man 7 socket" if you're interested). Good luck.
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.