1

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?

4
  • 2
    But you do. What if your file is smaller than n ? Commented Nov 19, 2015 at 10:26
  • ok let's say we know the size of file. so if we read those many bytes from a file desc, it wil return those many bytes always. but let's say it was a socket that we were reading thosse many bytes from? @SanderDeDycker Commented Nov 19, 2015 at 10:30
  • So you got lucky. It isn't specified to behave like that, so you can't rely on it. Commented Nov 19, 2015 at 10:34
  • If you want your socket to behave similarly, then make sure it's a blocking socket. Note though that it's still not safe to ignore the return value of read, because it's still not guaranteed to return n (due to not all data being available in the local buffer yet, the socket being closed, a signal interruption, ...) Commented Nov 19, 2015 at 10:37

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so it's not guaranteed to return n bytes even if it is a file read. Great. Didn't know that!
First, you have the end-of-file case. And then, a file descriptor might not refer to a plain file. At last, some file systems might not be POSIX compliant. So I don't think you should expect a firm guarantee, even if in most cases, that is the case. Remember that a file can be modified by some other process while you are reading it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.