在套接字编程中,read()和recv(),以及send()和write()在性能、速度和其他行为方面有什么不同?
当前回答
Read()和write()更通用,它们适用于任何文件描述符。 但是,它们不能在Windows上运行。
您可以将其他选项传递给send()和recv(),因此在某些情况下可能必须使用它们。
其他回答
“性能和速度”?那些不是…同义词,在这里吗?
无论如何,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().
根据谷歌的第一次点击
Read()等效于带有flags参数为0的recv()。flags参数的其他值会改变recv()的行为。类似地,write()等价于带有flags == 0的send()。
区别在于recv()/send()只作用于套接字描述符,并允许您为实际操作指定某些选项。这些函数稍微专业化一些(例如,您可以设置一个标志来忽略SIGPIPE,或者发送带外消息……)。
函数read()/write()是适用于所有描述符的通用文件描述符函数。
linux上的另一件事是:
Send不允许对非套接字fd进行操作。因此,例如在usb端口上写入,写入是必要的。
推荐文章
- 递归复制文件夹,不包括一些文件夹
- C多行宏:do/while(0) vs作用域块
- 如何使用文件的行作为命令的参数?
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?
- 运行ssh后立即执行命令
- 函数名周围的括号是什么意思?
- 用C语言创建自己的头文件
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- main()中的Return语句vs exit()
- 如果不是内存地址,C指针到底是什么?
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 保护可执行文件不受逆向工程的影响?
- 如何让命令行参数传递给unix/linux系统上正在运行的进程?
- 从C语言的函数返回一个struct