Linux C++ 套接字发送/接收与写入/读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19971858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:20:50  来源:igfitidea点击:

C++ socket send/recv vs write/read

c++linuxsockets

提问by Necktwi

In socket ssize_t send(int __fd, const void* __buf, size_t __n, int __flags)we got this __flagsargument. I'm using MSG_NOSIGNALflag all through the connection. Is there a way to achieve this flag functionality in write? Since I'm using this flag all through the connection, It could be set when socketis created. Feel free to mention If there are ways to achieve all the __flagfunctions.

在 socket 中ssize_t send(int __fd, const void* __buf, size_t __n, int __flags)我们得到了这个__flags参数。我在MSG_NOSIGNAL整个连接中都使用了标志。有没有办法在write. 由于我在整个连接中都使用此标志,因此可以在创建时设置它socket。如果有办法实现所有__flag功能,请随意提及。

回答by Some programmer dude

No. When writing to a socket with writeit's the same thing as calling sendwith the flagsargument set to zero.

不。当写入套接字时,writesend与将flags参数设置为零的调用相同。

From the official POSIX reference

来自官方 POSIX 参考

If fildesrefers to a socket, write()shall be equivalent to send()with no flags set.

如果fildes 引用套接字,则write()应等效于未设置标志的send()



There is however a way of "setting" this flag permanently, and that is to ignore the SIGPIPEsignal:

然而,有一种永久“设置”此标志的方法,那就是忽略SIGPIPE信号:

signal(SIGPIPE, SIG_IGN);