Issue
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?
Solution
You can certainly use stdio
with sockets. You can even write a program that uses nothing but stdin
and stdout
, run it from inetd
(which provides a socket on STDIN_FILENO
and STDOUT_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 select
or poll
because there is no fselect
or fpoll
working on FILE *
's and you can't even implement one yourself because there's no standard way of querying a FILE *
to find out whether its input buffer is empty.
As soon as you need to handle multiple connections, stdio
is not good enough.
Answered By - Alan Curry Answer Checked By - Mary Flores (WPSolving Volunteer)