Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use file descriptors or streams for reading/writing to sockets

Tags:

c

file-io

sockets

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?

like image 776
szmoore Avatar asked Feb 04 '13 11:02

szmoore


1 Answers

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.

like image 178
David Schwartz Avatar answered Nov 16 '22 02:11

David Schwartz