diff --git a/man2/sendmmsg.2 b/man2/sendmmsg.2 index 12ad3ff0c..b426b4de0 100644 --- a/man2/sendmmsg.2 +++ b/man2/sendmmsg.2 @@ -23,8 +23,6 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.\" FIXME Adding an example program would improve this page -.\" .TH SENDMMSG 2 2012-02-27 "Linux" "Linux Programmer's Manual" .SH NAME sendmmsg \- send multiple messages on a socket @@ -165,6 +163,74 @@ is capped to .\" For error handling an application using sendmmsg needs to retry at .\" the first unsent message, so capping is simpler and requires less .\" application logic than returning EINVAL. +.SH EXAMPLE +The example below uses +.BR sendmmsg () +to send +.I onetwo +and +.I three +in two distinct UDP datagrams using one system call. The contents +of the first datagram originates from a pair of buffers. + +.nf +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +int +main() +{ + int sockfd; + struct sockaddr_in sa; + struct mmsghdr msg[2]; + struct iovec msg1[2], msg2; + int retval; + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd == \-1) { + perror("socket()"); + exit(EXIT_FAILURE); + } + + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + sa.sin_port = htons(1234); + if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa))) { + perror("connect()"); + exit(EXIT_FAILURE); + } + + memset(msg1, 0, sizeof(msg1)); + msg1[0].iov_base = "one"; + msg1[0].iov_len = 3; + msg1[1].iov_base = "two"; + msg1[1].iov_len = 3; + + memset(&msg2, 0, sizeof(msg2)); + msg2.iov_base = "three"; + msg2.iov_len = 5; + + memset(msg, 0, sizeof(msg)); + msg[0].msg_hdr.msg_iov = msg1; + msg[0].msg_hdr.msg_iovlen = 2; + + msg[1].msg_hdr.msg_iov = &msg2; + msg[1].msg_hdr.msg_iovlen = 1; + + retval = sendmmsg(sockfd, msg, 2, 0); + if (retval == -1) + perror("sendmmsg()"); + else + printf("%d messages sent\\n", retval); + + exit(0); +} +.fi .SH SEE ALSO .BR recvmmsg (2), .BR sendmsg (2),