sendmsg | |
Prototype: |
#include <sys/socket.h> #include <resolv.h> #include <sys/uio.h> int sendmsg(int sockfd, const struct msghdr *msg, unsigned int options); |
General Description: | Assemble a message from several blocks of data. This routine takes the data the iovec structure and creates single message. If the msg_name points to a real sockaddr, the routine sends the message connectionless. If NULL, the routine assumes that the socket is connected. |
Return Value: | This call returns the total number of bytes sent or -1 if error (check errno for more information). |
Parameters | |
sockfd | The open socket descriptor. |
msg | This is a reference to a msghdr structure that holds destination, flags and messages. The structure definitions follow:struct iovec{void *iov_base; /* Buffer start */__kernel_size_t iov_len; /* Buffer length */}; |
struct msghdr{__ptr_t msg_name; /* Dest address */socklen_t msg_namelen; /* Address length */struct iovec *msg_iov; /* Buffers vector */size_t msg_iovlen; /* Vector length */__ptr_t msg_control; /* Ancillary data */size_t msg_controllen; /* Ancillary data len */int msg_flags; /* Received msg flags */};The ancillary data allows the program to transfer special data like file descriptors. | |
options | Message-controlling flags (same as send()). |
Possible Errors | |
Examples | |
int i, sd, len, bytes; char buffer[MSGS][100]; struct iovec io[MSGS]; struct msghdr msg; struct sockaddr_in addr; sd = socket(PF_INET, SOCK_DGRAM, 0); bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(8080); inet_aton(&addr.sin_addr, "127.0.0.1"); bzero(&msg, sizeof(msg)); msg.msg_name = &addr; msg.msg_namelen = sizeof(addr); for ( i = 0; i < MSGS; i++ ) { io[i].iov_base = buffer[i]; sprintf(buffer[i], "Buffer #%d: this is a test\n", i); io[i].iov_len = strlen(buffer[i]); } msg.msg_iov = io; msg.msg_iovlen = MSGS; if ( (bytes = sendmsg(sd, &msg, 0)) < 0 ) perror("sendmsg"); |