Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

send(), sendto(), sendmsg() - send a message from socket to socket

&pagelevel(4)&pagelevel

#include <sys/socket.h> 
#include <netinet/in.h>

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

Description

The send(), sendto() and sendmsg() functions send messages from one socket to another. send() can only be used with a socket over which a connection is set up (see the connect() function). sendto() and sendmsg() can always be used.

The s parameter designates the socket from which a message is sent. The destination address is passed with to, where tolen specifies the length of the destination address.

The length of the message is specified with len. If the message is too long to be transported completely by the underlying protocol level, error EMSGSIZE is returned and the message is not transferred.

The flags parameter is currently not supported. A value not equal to 0 leads to an error and the errno variable is set to the value EOPNOTSUPP.

If the process which calls send(), sendmsg() or sendto() receives a signal before any send data was buffered, the function concerned is called again in a standard case. The function is not called again if the calling process has specified with sigaction() to interrupt these function calls (see manual "C Library Functions for POSIX Applications").

The sendmsg() function uses the msghdr structure to reduce the number of directly supplied parameters. The msghdr structure is defined in the <sys/socket.h> header file as follows:

struct msghdr {
   void         *msg_name;            /* Optional address */
   size_t        msg_namelen;         /* Length of the address */
   struct iovec *msg_iov;             /* Scatter/gather fields */
   int           msg_iovlen;          /* Number of members in msg-iov */
   caddr_t       msg_accrights        /* Send/receive access rights */
   int           msg_accrightslen;
   void         *msg_control;         /* Auxiliary data */
   size_t        msg_controllen;      /* Length of auxiliary data buffer */
   int           msg_flags;           /* Flag for received message */
};

The elements msg_name and msg_namelen specify the destination address if the socket has no connection set up. The null pointer can be passed for msg_name if no names are desired or requested. You will find a description of how to assign an address to a socket in the section "Socket addressing".

The elements msg_iov and msg_iovlen describe the scatter and gather fields.

The elements msg_accrights and msg_accrightslen as well as msg_control and msg_controllen are ignored by default (see inline documentation in <sys/socket.h> if necessary).

Return value

≥0:

If successful. The value indicates the number of sent bytes.

-1:

If errors occur. errno is set to indicate the error. The descriptor sets are then not changed.

Errors

EBADF

The s parameter is not a valid descriptor.

ECONNRESET

The connection to the partner was interrupted (only with type SOCK_STREAM sockets).

EDESTADDRREQ

The socket is not connection-oriented, a permanent partner was not defined and no partner was specified in the call.

EFAULT

The data is to be stored in a non-existent or protected part of the process address range.

EHOSTUNREACH

The destination host cannot be reached.

EINTR

The calling process received a signal before any data could be buffered for sending. The setting to interrupt the function call and not repeat it is active.

EINVAL

A parameter specified an illegal value.

EMSGSIZE

The message is too long to be sent in one piece.

ENETDOWN

The connection to the network is down.

ENOBUFS

The output queue for a network interface is full. This generally leads to the interface stopping sending, but can be due to a temporary jam.

ENOTCONN

No connection exists for the socket.

ENOTSOCK

The descriptor s does not reference a socket.

EOPNOTSUPP

The flags or msg->msg_accrights parameter was specified, and this is not supported.

EPIPE

The socket is not enabled for writing or the socket is connection-oriented and the partner has shut the connection down.

If the socket is of type SOCK_STREAM, the SIGPIPE signal is generated for the calling process.

EWOULDBLOCK

The socket is marked as non-blocking and the requested operation would block.

EAFNOSUPPORT

Addresses of the address family specified for sendto() or sendmsg() cannot be used with this socket.

If the socket address family is AF_UNIX, execution of send(), sendto() and sendmsg() can also lead to an error for the following reasons:

EACCES

Access rights are refused for a path name component or write rights to the specified socket are refused.

ENAMETOOLONG

A path name component exceeds NAME_MAX characters or the complete path name is longer than PATH_MAX characters.

ENOENT

A path name component refers to a non-existent file or the path name is blank.

ENOTDIR

A path name component is not a directory.

See also

connect(), getsockopt(), recv(), socket(), fcntl(), select(), write()