I wanted to know how does read() function work when a socket descriptor is passed to it and when a file descriptor is passed to it. In case of file descriptor, it always returns n bytes as specified, or less if there are no n bytes. However in case of a socket descriptor, it's not necessary it will return n bytes. So in order to make sure if we have received n bytes, we'll have to put an application logic and keep count of how many bytes we have received and terminate when the count is n. My question is, why don't we have to put an application logic when we are reading from a file?
1 Answer
Read read(2) man page:
man 2 read
You'll better assume that it always may return a byte count less that the entire buffer you passed to it (in particular, because it could be difficult to know if the file descriptor refers to a socket, a tty, some other device, a pipe, a fifo, or some plain file, and also because you could have some file systems with non-POSIX compliant semantics). You also might have reached the end of file (EOF), etc...
For TCP sockets, remember that they only are a stream of bytes, and a given single send may be received in several reads, etc etc... In particular, message chunks could be split/reassembled by "the network" (e.g. routers).
For plain files, remember that some other process could change it (e.g. write into it, truncate it, etc..) while your process is reading it.
n?read, because it's still not guaranteed to returnn(due to not all data being available in the local buffer yet, the socket being closed, a signal interruption, ...)