Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ socket send/recv vs write/read

Tags:

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.

like image 963
Necktwi Avatar asked Nov 14 '13 07:11

Necktwi


People also ask

What is the difference between read and recv in socket programming?

The only difference between recv() and read(2) is the presence of flags. With a zero flags argument, recv() is generally equivalent to read(2) (but see NOTES).

What is the difference between write and send?

The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2). Also, the following call send(sockfd, buf, len, flags); is equivalent to sendto(sockfd, buf, len, flags, NULL, 0); The argument sockfd is the file descriptor of the sending socket.

What is major difference between sent () and Sento () functions?

Generally speaking, send() is used for TCP SOCK_STREAM connected sockets, and sendto() is used for UDP SOCK_DGRAM unconnected datagram sockets.

What is the difference between RECV and Recvfrom?

Unlike the recv() call, which can only be used on a connected stream socket or bound datagram socket, recvfrom() can be used to receive data on a socket whether or not it is connected. If no messages are available at the socket, the recvfrom() call waits for a message to arrive unless the socket is nonblocking.


1 Answers

No. When writing to a socket with write it's the same thing as calling send with the flags argument set to zero.

From the official POSIX reference

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


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

signal(SIGPIPE, SIG_IGN); 
like image 52
Some programmer dude Avatar answered Oct 12 '22 12:10

Some programmer dude