在套接字编程中,read()和recv(),以及send()和write()在性能、速度和其他行为方面有什么不同?
当前回答
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().
其他回答
“性能和速度”?那些不是…同义词,在这里吗?
无论如何,recv()调用接受read()不接受的标志,这使得它更强大,或者至少更方便。这是一个不同之处。我不认为有显著的性能差异,但还没有进行测试。
您可以使用write()和read()来代替send()和recv(),但是send()和recv()对数据传输提供了更大的控制
recv()和read()之间的唯一区别是是否存在标志。使用零标志参数,recv()通常等效于read()
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().
linux上的另一件事是:
Send不允许对非套接字fd进行操作。因此,例如在usb端口上写入,写入是必要的。