14.3 recv and send Functions
These two functions are similar to the standard read and write functions, but one additional argument is required.
#include <sys/socket.h> |
ssize_t recv(int sockfd , void *buff , size_t nbytes , int flags ); |
ssize_t send(int sockfd , const void *buff , size_t nbytes , int flags ); |
Both return: number of bytes read or written if OK, 1 on error |
Figure 14.6.
flags for I/O functions.MSG_DONTROUTE | This flag tells the kernel that the destination is on a locally attached network and not to perform a lookup of the routing table. We provided additional information on this feature with the SO_DONTROUTE socket option (Section 7.5). This feature can be enabled for a single output operation with the MSG_DONTROUTE flag, or enabled for all output operations for a given socket using the socket option. |
MSG_DONTWAIT | This flag specifies nonblocking for a single I/O operation, without having to turn on the nonblocking flag for the socket, perform the I/O operation, and then turn off the nonblocking flag. We will describe nonblocking I/O in Chapter 16, along with turning the nonblocking flag on and off for all I/O operations on a socket. |
This flag is newer than the others and might not be supported on all systems. | |
MSG_OOB | With send, this flag specifies that out-of-band data is being sent. With TCP, only one byte should be sent as out-of-band data, as we will describe in Chapter 24. With recv, this flag specifies that out-of-band data is to be read instead of normal data. |
MSG_PEEK | This flag lets us look at the data that is available to be read, without having the system discard the data after the recv or recvfrom returns. We will talk more about this in Section 14.7. |
MSG_WAITALL | This flag was introduced with 4.3BSD Reno. It tells the kernel not to return from a read operation until the requested number of bytes have been read. If the system supports this flag, we can then omit the readn function (Figure 3.15) and replace it with the following macro:[View full width]
Even if we specify MSG_WAITALL, the function can still return fewer than the requested number of bytes if (i) a signal is caught, (ii) the connection is terminated, or (iii) an error is pending for the socket. |