Send/sendto/sendmsg

Source: Internet
Author: User
Tags sendmsg

[Send/sendto/sendmsg System Call]

Function Description:
Send a message. Send can only be used for connection-based sockets. The only difference between send and write is the existence of a flag. When the flag is 0, send is equivalent to write. Sendto and sendmsg can be used for both connectionless sockets and connection-based sockets. Except for the non-blocking mode, the call will be blocked until the data is sent.
Usage:
# Include <sys/types. h>
# Include <sys/socket. h>
Ssize_t send (INT sock, const void * Buf, size_t Len, int flags );
Ssize_t sendto (INT sock, const void * Buf, size_t Len, int flags, const struct sockaddr * To, socklen_t tolen );
Ssize_t sendmsg (INT sock, const struct msghdr * MSG, int flags );

Parameters:
Sock: index the socket from which data will be sent.
Buf: indicates the buffer to be sent.
Len: the length of the buffer above.
Flags: a combination of the following zero or multiple flags, which can be connected through the or operation.

Msg_dontroute: Do not use a gateway to send packets, but send packets to directly connected hosts. This flag is mainly used for diagnosis or routing.
Msg_dontwait: The operation will not be blocked.
Msg_eor: terminate a record.
Msg_more: The caller needs to send more data.
Msg_nosignal: when the other end terminates the connection, the request should not send the sigpipe signal on the stream-based error socket.
MSG_OOB: Send out-of-band data (data to be processed preferentially), and the current protocol must support this operation.

To: point to the region where the acceptor address is stored. It can be null.
Tolen: the length of the above memory area, which can be 0.
MSG: refers to the memory buffer that stores the sending message header. The structure is as follows:

Struct msghdr {
Void * msg_name;
Socklen_t msg_namelen;
Struct iovec * msg_iov;
Size_t msg_iovlen;
Void * msg_control;
Socklen_t msg_controllen;
Int msg_flags;
};

Possible data structures are:

Struct cmsghdr {
Socklen_t cmsg_len;
Int cmsg_level;
Int cmsg_type;

};

Return description:
When execution is successful, the number of sent bytes is returned. -1 is returned for failure, and errno is set to one of the following values
Eacces: for Unix domain sockets, writing to the target socket file is not allowed, or a directory node of the path precursor cannot be searched.
Eagain, ewouldblock: the socket is marked as non-blocking, And the sending operation is blocked.
Ebadf: sock is not a valid descriptive word.
Econnreset: the connection is reset by the user.
Edestaddrreq: the socket is not in connection mode and the peer address is not specified.
Efault: memory space access error
Eintr: The operation is interrupted.
Einval: the parameter is invalid.
Eisconn: the connection-based socket has been connected and the receiving object is specified.
Emsgsize: the message size is too large.
Enomem: insufficient memory
Enotconn: the socket is not connected, and the target is not given
Enotsock: the sock index is not a socket
Epipe: The local connection is closed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.