1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" with some material from a draft by
3 .\" Stephan Mueller <stephan.mueller@atsec.com>
4 .\" in turn based on Andi Kleen's recvmmsg.2 page.
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .\"*******************************************************************
30 .\" This file was generated with po4a. Translate the source file.
32 .\"*******************************************************************
33 .TH SENDMMSG 2 2012\-12\-16 Linux "Linux Programmer's Manual"
35 sendmmsg \- 複数のメッセージをソケットへ送信する
38 \fB#define _GNU_SOURCE\fP
39 \fB#include <sys/socket.h>\fP
41 \fBint sendmmsg(int \fP\fIsockfd\fP\fB, struct mmsghdr *\fP\fImsgvec\fP\fB, unsigned int \fP\fIvlen\fP\fB,\fP
42 \fB unsigned int \fP\fIflags\fP\fB);\fP
45 .\" See commit 228e548e602061b08ee8e8966f567c12aa079682
46 \fBsendmmsg\fP() システムコールは \fBsendmsg\fP(2) の拡張で、
47 このシステムコールを使うと一度の呼び出しでソケットに複数のメッセージを送信できる (アプリケーションによっては性能上のメリットがある)。
49 \fIsockfd\fP 引き数は、 データを送信するソケットのファイルディスクリプタである。
51 \fImsgvec\fP 引き数は \fImmsghdr\fP 構造体の配列である。 この配列の大きさは \fIvlen\fP で指定する。
53 \fImmsghdr\fP 構造体は \fI<sys/socket.h>\fP で次のように定義されている。
58 struct msghdr msg_hdr; /* メッセージヘッダ */
59 unsigned int msg_len; /* 送信されたバイト数 */
64 \fImsg_hdr\fP フィールドは、 \fBsendmsg\fP(2) で説明されている \fImsghdr\fP 構造体である。 \fImsg_len\fP
65 フィールドは \fImsg_hdr\fP から送信されたメッセージのバイト数を返すのに使用される。 この値は \fBsendmsg\fP(2)
66 をこのヘッダに対して呼び出した場合の返り値と同じである。
68 \fIflags\fP 引き数には複数のフラグを論理和 (OR) で指定できる。フラグは \fBsendmsg\fP(2) と同じである。
70 停止 (blocking) モードの \fBsendmmsg\fP() の呼び出しは、 \fIvlen\fP 個のメッセージが送信されるまで停止する。 非停止
71 (nonblocking) モードの呼び出しでは、 送信できるだけのメッセージ (最大で \fIvlen\fP 個) を送信し、 すぐに返る。
73 \fBsendmmsg\fP() が返った際には、 \fImsgvec\fP の送信が行われた要素の \fImsg_len\fP フィールドは、対応する
74 \fImsg_hdr\fP から送信されたバイト数が入っている。 呼び出しの返り値は、更新された \fImsgvec\fP の要素数である。
76 成功すると、 \fBsendmmsg\fP() は \fImsgvec\fP から送信されたメッセージ数を返す。 返り値が \fIvlen\fP よりも小さい場合、
77 呼び出した側では再度 \fBsendmmsg\fP を呼び出して残りのメッセージを送信することができる。
79 エラーの場合、 \-1 を返し、 \fIerrno\fP にエラーを示す値を設定する。
81 .\" commit 728ffb86f10873aaf4abd26dde691ee40ae731fe
82 .\" ... only return an error if no datagrams could be sent.
83 .\" If less than the requested number of messages were sent, the application
84 .\" must retry starting at the first failed one and if the problem is
85 .\" persistent the error will be returned.
87 .\" This matches the behaviour of other syscalls like read/write - it
88 .\" is not an error if less than the requested number of elements are sent.
89 エラーは \fBsendmsg\fP(2) と同じである。 エラーが返されるのは、 データグラムが全く送信できなかった場合のみである。
91 \fBsendmmsg\fP() システムコールは Linux 3.0 で追加された。 glibc でのサポートはバージョン 2.14 で追加された。
93 \fBsendmmsg\fP() は Linux 固有である。
95 .\" commit 98382f419f32d2c12d021943b87dea555677144b
96 .\" net: Cap number of elements for sendmmsg
98 .\" To limit the amount of time we can spend in sendmmsg, cap the
99 .\" number of elements to UIO_MAXIOV (currently 1024).
101 .\" For error handling an application using sendmmsg needs to retry at
102 .\" the first unsent message, so capping is simpler and requires less
103 .\" application logic than returning EINVAL.
104 \fIvlen\fP に指定できる値の最大値は \fBUIO_MAXIOV\fP (1024) である。
106 以下の例では、 \fBsendmmsg\fP() を使って、 一度のシステムコールで、 \fIonetwo\fP と \fIthree\fP を二つの別々の UDP
107 データグラムで送信する。 一つ目のデータグラムの内容は、二つのバッファから取得される。
111 #include <netinet/ip.h>
115 #include <sys/types.h>
116 #include <sys/socket.h>
122 struct sockaddr_in sa;
123 struct mmsghdr msg[2];
124 struct iovec msg1[2], msg2;
127 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
133 sa.sin_family = AF_INET;
134 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
135 sa.sin_port = htons(1234);
136 if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {
141 memset(msg1, 0, sizeof(msg1));
142 msg1[0].iov_base = "one";
144 msg1[1].iov_base = "two";
147 memset(&msg2, 0, sizeof(msg2));
148 msg2.iov_base = "three";
151 memset(msg, 0, sizeof(msg));
152 msg[0].msg_hdr.msg_iov = msg1;
153 msg[0].msg_hdr.msg_iovlen = 2;
155 msg[1].msg_hdr.msg_iov = &msg2;
156 msg[1].msg_hdr.msg_iovlen = 1;
158 retval = sendmmsg(sockfd, msg, 2, 0);
160 perror("sendmmsg()");
162 printf("%d messages sent\en", retval);
168 \fBrecvmmsg\fP(2), \fBsendmsg\fP(2), \fBsocket\fP(2), \fBsocket\fP(7)
170 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
171 である。プロジェクトの説明とバグ報告に関する情報は
172 http://www.kernel.org/doc/man\-pages/ に書かれている。