After a socket has been setup, is it better practice to use read(2) and write(2) calls on the socket descriptor, or to associate a stream with the socket descriptor using fdopen(3) and then use stdio(3) functions?
int sfd = socket(PF_INET, SOCK_STREAM, 0);
// setup the socket using sfd
FILE * stream = fdopen(sfd, "r+");
// use fprintf, fscanf, etc
EDIT: I also unbuffer the stream
setbuf(stream, NULL)
To avoid having to flush it as mentioned in the comments.
I've been using this approach because it lets me reuse code written for FILE* streams, and I have the advantage of being able to use format strings (I am working with human readable text). GNU seems to be suggesting that this is a good idea.
http://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html
However usually when I see code using sockets, the socket descriptor is used instead of a stream for all operations. Is there an advantage to using the lower level functions?
If you need more precise control and handling of error conditions, use read
and write
. If you don't, and prefer the convenience of stdio functions, then use the FILE*
wrapper.
One problem with using a FILE *
wrapper is that you don't have control over how and when data is actually written to the socket. This can result in inefficient network utilization and excessive delays (due to Nagle's algorithm interacting with delayed ACK) if you're not careful.
I would recommend using read
and write
directly if this is a high-performance Internet application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With