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
C++ socket send/recv vs write/read
提问by Necktwi
In socket ssize_t send(int __fd, const void* __buf, size_t __n, int __flags)
we got this __flags
argument. I'm using MSG_NOSIGNAL
flag 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 socket
is created. Feel free to mention If there are ways to achieve all the __flag
functions.
在 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 write
it's the same thing as calling send
with the flags
argument set to zero.
不。当写入套接字时,write
它send
与将flags
参数设置为零的调用相同。
From the official POSIX reference
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 SIGPIPE
signal:
然而,有一种永久“设置”此标志的方法,那就是忽略SIGPIPE
信号:
signal(SIGPIPE, SIG_IGN);