What is the Difference Between read() and recv() , and Between send() and write()?
Solution 1:
The difference is that recv()
/send()
work only on socket descriptors and let you specify certain options for the actual operation. Those functions are slightly more specialized (for instance, you can set a flag to ignore SIGPIPE
, or to send out-of-band messages...).
Functions read()
/write()
are the universal file descriptor functions working on all descriptors.
Solution 2:
Per the first hit on Google
read() is equivalent to recv() with a flags parameter of 0. Other values for the flags parameter change the behaviour of recv(). Similarly, write() is equivalent to send() with flags == 0.
Solution 3:
read()
and write()
are more generic, they work with any file descriptor.
However, they won't work on Windows.
You can pass additional options to send()
and recv()
, so you may have to used them in some cases.
Solution 4:
I just noticed recently that when I used write()
on a socket in Windows, it almost works (the FD passed to write()
isn't the same as the one passed to send()
; I used _open_osfhandle()
to get the FD to pass to write()
). However, it didn't work when I tried to send binary data that included character 10. write()
somewhere inserted character 13 before this. Changing it to send()
with a flags parameter of 0 fixed that problem. read()
could have the reverse problem if 13-10 are consecutive in the binary data, but I haven't tested it. But that appears to be another possible difference between send()
and write()
.