Search code examples
c++cblockingfwrite

Timeout fwrite call to keep it from blocking


Is there a method to keep fwrite from blocking, like say using timeouts?

The scenario is this: I need to make a fwrite call (since I am largely reusing a code , I need to stick to it) on a network. However if I pull off the network cable, the fwrite blocks. Is there a workaround? Any help/suggestions will be greatly appreciated.


Solution

  • This should have the same effect as @Alexander Bakulin's suggestion, but is somewhat shorter.

    int fd = fdnum(stream);
    int ioctl( fd, FIONBIO, 1 );
    

    Reference: http://oss.org.cn/ossdocs/gnu/linux/syscalls_17.html