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

Hello Monks;
I am having a little problem running a TCP/IP server/client application and need your help. In my application, I am processing some information from a file and sending it across a socket. However, for each info I send, I need to get a response with in 10ms. Therefore, whenever I send the info, I need to start a timer. When the timer expires, I call the "recv" function to see if I have recieved a data string of certain format.
In the mean time if no data has been received, I do not want the recieve function to block until data is recieved. I will like to be able to resend previous info and wait another 10ms.

My problems are :
1. How do I get recv() to be non-blocking in Perl.
2. How do I get a timer to measure the 10ms wait period.

Thanks for your assistance and I highly appreciate every Monk input.

Replies are listed 'Best First'.
Re: InterProcess Communication
by cdarke (Prior) on May 06, 2008 at 14:06 UTC
    See the four-argument version of select, the forth argument is a timeout.
Re: InterProcess Communication
by moritz (Cardinal) on May 06, 2008 at 12:51 UTC
    For the timer you can use Time::HiRes, which is in core since 5.7.3.

    recv says it accepts the same flags as the underlying system call, which is (on my system) MSG_DONTWAIT.

Re: InterProcess Communication
by pc88mxer (Vicar) on May 06, 2008 at 15:43 UTC
    As cdarke suggests, I would also recommend looking into the four argument version of select in which case it's not necessary to make recv non-blocking. However, I am now telling people to use select via the module IO::Select when applicable because it makes it so much easier to use. In fact, I think the core perl documentation should be updated to at least mention the existence of IO::Select if not also to show examples from it. It would make the concept much more accessible and attractive, and that would help score points for perl.