It’s often said that one shouldn’t use C standard I/O functions (like fprintf(), fscanf()) when working with sockets.
I can’t understand why. I think if the reason was just in their buffered nature, one could just flush the output buffer each time he outputs, right?
Why everyone uses UNIX I/O functions instead? Are there any situations when the use of standard C functions is appropriate and correct?
You can certainly use
stdiowith sockets. You can even write a program that uses nothing butstdinandstdout, run it frominetd(which provides a socket onSTDIN_FILENOandSTDOUT_FILENO), and it works even though it doesn’t contain any socket code at all.What you can’t do is mix buffered I/O with
selectorpollbecause there is nofselectorfpollworking onFILE *‘s and you can’t even implement one yourself because there’s no standard way of querying aFILE *to find out whether its input buffer is empty.As soon as you need to handle multiple connections,
stdiois not good enough.