|Syntactic Confectionery Delight|
Making IO::Socket::UNIX client time outby kroach (Pilgrim)
|on Oct 02, 2019 at 12:53 UTC||Need Help??|
kroach has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to make a IO::Socket::UNIX client connection time out after some time if the server is too slow to respond. I tried using the Timeout argument from IO::Socket but it doesn't appear to work in this case. Here is a server/client example:
The client is set to time out after 2 seconds and the server to take at least 5 seconds to respond. I expected the client to stop after 2 seconds, however it just reads the response after 5 seconds as if the Timeout was completely ignored. Am I doing something wrong?
There is a solution using alarm() in perlipc which works for any kind of blocking code:
However, there is probably a cleaner way to do it. Any ideas? What's the recommended way to make a socket client timeout?
It's possible to use setsockopt to set the read timeout:
In order to set a write timeout, replace SO_RCVTIMEO with SO_SNDTIMEO. This is exactly what IO::Socket::Timeout does, I'm leaving a pure solution here for educational purposes, in case someone stumbles upon this thread in the future.