in reply to An IO::Sockets 'test for live connection' question:

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.

  • Comment on Re: An IO::Sockets 'test for live connection' question: