Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

sendmsg() - send a message from socket to socket

&pagelevel(3)&pagelevel



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


Kernighan-Ritchie-C:
int sendmsg(s, msg, flags);

int s, flags;
struct msghdr *msg;

ANSI-C:
int sendmsg(int s, struct msghdr* msg, int flags);


Description

The sendmsg() function is supported in the AF_INET, AF_INET6 and AF_ISO address families and provides the following functionality depending on the parameterization(msg parameter):

  • sendmsg() can be used to send user data from a socket to a partner socket.

  • Only in AF_ISO: sendmsg() can be used to write connection data to socket s.

A pointer to an object of the data type struct msghdr must be specified as the current parameter for msg. The desired functionality for sendmsg() is selected via the component msg->msg_control (data type caddr_t or char *)

  • If msg->msg_control is the null pointer, user data is sent.

  • Only in AF_ISO: If msg->msg_control is not the null pointer, msg->msg_control is interpreted as a pointer to a storage area with the structure cmsghdr, and connection data is written to the socket.

    This allows sendmsg() to send an acknowledgment of the connection request to the communications partner without transferring user data or connection data.


msghdr structure

The msghdr structure is declared in <sys.socket.h> as follows:

struct msghdr {
   caddr_t       msg_name;            /* destination address */
   int           msg_namelen;         /* length of the destination address */
   struct iovec *msg_iov;             /* scatter/gather fields */
   int           msg_iovlen;          /* number of elements in msg_iov */
   caddr_t       msg_control;         /* auxiliary data */
   int           msg_controllen;      /* length of the buffer for */
                                      /* auxiliary data */
   int           msg_flags;           /* flag for received message */
};
struct msghdr *msg;

msg->msg_name and msg->msg_namelen are only interpreted in the AF_INET and AF_INET6 address families with the socket type SOCK_DGRAM. msg_name indicates the address of a socket address structure, and msg_namelen indicates the length of this address structure. If these parameter are not to be used, msg_name should have the value of the null pointer and msg_namelen the value 0.
msg->msg_iov is a pointer to a storage area with objects of the type struct iovec. msg->msg_iovlen indicates the number of elements (max. 16) in this storage area.msg->msg_control is a pointer to an object of the type struct cmsghdr which must be supplied with the connection data to be written before sendmsg() is called (only AF_ISO). msg->msg_controllen indicates the length of *msg->msg_control.
In msg->msg_flags, sendmsg() indicates the end of a record with MSG_EOR (only AF_ISO).


iovec structure

The iovec structure is declared in <sys.uio.h> as follows:

struct iovec{
    caddr_t  iov_base;  /* buffer for auxiliary data */
    int      iovlen;    /* buffer length */
};


cmsghdr structure

The cmsghdr structure is declared in <sys.socket.h> as follows:

struct cmsghdr {
   u_int     cmsg_len;           /* number of data bytes incl. header */
   int       cmsg_level;         /* generating protocol */
   int       cmsg_type;          /* protocol-specific type * */
   /* followed by u_char cmsg_data[] */
};
struct cmsghdr *cmsg;

cmsg->cmsg_len contains the length of the storage area of *msg->msg_control. SOL_TRANSPORT is entered for the ISO transport service in cmsg->cmsg_level. cmsg->cmsg_type indicates the connection data type (TPOPT_CONN_DATA, TPOPT_CFRM_DATA, TPOPT_DISC_DATA).
Connection data and the final null byte are entered in the data area of the cmsghdr.

TPOPT_REDI_DATA and TPOPT_REDI_BDOK are provided for the handoff procedure. In this case, the structure cmsg_redhdr is required. Refer to chapter "ExtendedSOCKETS(BS2000) functions" for a description.

Return value

>=0:

Number of bytes of user data sent.

AF_ISO: 0 for connection data (CONN_DATA, CFRM_DATA, DISC_DATA)

-1:

If errors occur. errno is set to indicate the error. 

Errors indicated by errno

EBADF

The s parameter is not a valid descriptor.

ECONNRESET

The connection to the partner was interrupted.

EINVAL

A parameter specifies an invalid value.

EIO

I/O error. The message could not be passed to the transport system.

ENETDOWN

The connection to the network is down.

ENOTCONN

No connection exists for the socket.

EOPNOTSUPP

The function call includes illegal attributes

EPIPE

The partner has interrupted the connection.

EWOULDBLOCK

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