send, sendto, sendmsg - sending a message from the socket

Outline

#include <sys/types.h>
#include <sys/socket.h>

int send(int s, const void *msg, size_t len, int flags);
int sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
int sendmsg(int s, const struct msghdr *msg, int flags);

description

The Send , sendto , and sendmsg for transmitting messages to another socket. The Send only used to connect a socket, while sendto and sendmsg may be used in any case.

Use the destination address to specify, tolen define its length. The length of the message with len specified. If the message is too long to pass the lower layer protocol, the function returns EMSGSIZE error message will not be sent.

Error in the data transmission process is not returned to the generated Send. If a local error occurs, -1 is returned.

When the message to be transmitted is greater than the length of the socket buffer is currently available, Send will be blocked unless a non-blocking input and output mode on the socket. For non-blocking mode, in which case the return EAGAIN error. Of The system call SELECT (2) may be used to detect when more data can be transmitted.

Parameter flags is a sign word, you can contain the following flags:

Support for socket-band data,
MSG_OOB will send OUT-of-Band (band) data (for example, SOCK_STREAM type sockets); underlying protocol must support band data.
MSG_DONTROUTE
Not used in the gateway only sends the packet directly to the host connected to the network can receive the flag data is typically only used for diagnosis and routing procedures routable protocol suite to use this flag;... Not the socket packet.
MSG_DONTWAIT
Using non-blocking operation; If the operation requires blocking the return EAGAIN error (can also use the F_SETFL the fcntl (2) is provided O_NONBLOCK to achieve this function.)
MSG_NOSIGNAL
When the other end of the socket stream connection does not interrupt transmission SIGPIPE signal, but still return EPIPE error.
MSG_CONFIRM (only for Linux 2.3 or later)
Notification link layer forwarding process happened: Got a successful response if the other end of the link layer is not notified, it will follow the normal discovery on the adjacent host network (such as through free arp) can only be used for.. SOCK_DGRAM and SOCK_RAW type socket, IPv4 and IPv6 and is only effective details see ARP (. 7)

Structure msghdr defined as follows. For details, see the recv (2) and below.

the msghdr {struct 
    void * msg_name; / * Address option * / 
    the socklen_t msg_namelen; / * address length * / 
    struct the iovec * msg_iov; / * message array * / 
    size_t msg_iovlen; / * number of elements in msg_iov * / 
    void * s field msg_control; / * additional information, see below * / 
    the socklen_t field msg_controllen; / * length of the auxiliary data buffer * / 
    int the msg_flags; / * message receiving flag * / 
};

You may be used msg_control and field msg_controllen . Any control information transmitting member kernel can handle the maximum length of the control message buffer net.core.optmem_max be defined for each socket sysctl; see Socket (. 7).

return value

It returns the number of characters sent when successful, otherwise -1.

error code

Some of the standard error is generated in the other socket layer is the lower layer protocol module generated; See respective man pages.

EBADF
Specifying an invalid descriptor.
ENOTSOCK
Parameter s is not a socket.
EFAULT
Parameter specifies the user address space illegally.
EMSGSIZE
Message length out of range.
EAGAIN or EWOULDBLOCK
Socket to non-blocking, but the requested operation requires blocking.
ENOBUFS
Network interface output queue is full. This usually means that the interface has stopped sending, there may be a temporary congestion (which do not occur in linux, when the device queue overflow packets are simply discarded.
ENTRY
Received signal.
ENOMEM
No available memory.
chosen
Parameter passing is illegal.
EPIPE
Connecting the local terminal socket is closed. In this case the process also receive SIGPIPE signal unless a MSG_NOSIGNAL  

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11098313.html