ethrbunny has asked for the wisdom of the Perl Monks concerning the following question:

Im using IO::Socket::SSL to communicate between a central site and several (hundred) machines. Its backwards from the normal 'server/client' relationship as the one machine acts as a client to download info from the many (server) processes.

The issue Im facing is sometimes the server process will accept the socket->new and return what appears to be a valid connection but any attempt to sysread hangs. I was hoping to change to an asynchronous connection in an attempt to time out this 'block' but Im not sure how to do it.

Should I go with a select() and attempt to trap errors and timeouts or use a threading model to 'watch' the process? Other suggestions?

FWIW I have been trying to put together a 'select' solution but am not having much luck getting the code structured properly around the various IO calls I need to make.

Replies are listed 'Best First'.
Re: non-blocking socket client?
by moritz (Cardinal) on Dec 08, 2009 at 16:09 UTC
    Non-blocking network requests can be comfortably implemented with POE, AnyEvent or similar modules/frameworks. Maybe one of them can help you.
Re: non-blocking socket client?
by SuicideJunkie (Vicar) on Dec 08, 2009 at 16:45 UTC

    It sounds like the key is to get your reading and your parsing/logic separated. Once that is done, you can easily swap out different reading methods and see what works best.

    One way to do it single threaded is to put socket/buffer pairs into a hash, and call a function to fill those buffers. Once one of the buffers gets to a usable point (such as containing a newline), you can split/regex/substr it off the buffer and do what needs doing.

    Your filler function can dump all the sockets into a select and see which ones can be read. Read (once!) only those that are readable, and append to the associated buffer.
    Returning a list of only the socket (keys) which changed during that read cycle might be handy to save CPU.