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

Hi Monks

I have a master sending commands to all his children in nonblocking mode, this works fine, but I like to offer the option of waiting for reply if needed. (aka like setup).

But there is no flag O_BLOCK or similar for fcntl. what are the flags to wait for.

Currently I use a select for a timeout period before reading from the socket but this is not very elegant.

Thanks

Replies are listed 'Best First'.
Re: nonblocking socket back to blocking
by BrowserUk (Patriarch) on Oct 17, 2007 at 00:06 UTC

    O_NONBLOCK is a bit-flag. To unset it, try:

    fcntl(fd, F_SETFL, ~O_NONBLOCK)

    Whether that will work probably depends upon which interpretation of POSIX your system thinks it's complying to.

    You might want to query the current setting and just remove the nonblock bit.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: nonblocking socket back to blocking (select)
by tye (Sage) on Oct 17, 2007 at 01:27 UTC

    Sending will only block if the buffers have gotten too full. So rather than sending non-blocking, just select with a 0 time-out. If select fails, then report that failure and don't send (instead of sending non-blocking and reporting that the attempt failed). Though I wonder if that could run into problems if the amount of data you are trying to send is in large chunks. I suspect the select mechanism means that instead of blocking the socket would either only send part of the data or would fail with something like EMSGSIZE (depending on the type of socket).

    The usual way this type of thing is done is to have multiple objects that queue up read and write requests and then one select loop that reads/writes for any sockets that are ready for their queued requests. Having your server "lock up" waiting for a reply certainly sounds "not very elegant" to me.

    - tye        

      Thanks I've found that a timeout select loop works best. Based on the excellent feedback I keep it that way Thanks Again
Re: nonblocking socket back to blocking
by Somni (Friar) on Oct 16, 2007 at 23:25 UTC
    If you're using select on the socket why don't you just select for reading? That way whenever select returns you know there's data to read, and it doesn't matter if you block or not.

    As for your stated question, sockets are normally set to block operations. Something in your code has set it to non-blocking; remove that part of your code.