.\" Copyright (c) 2012 by Michael Kerrisk .\" with some material from a draft by .\" Stephan Mueller .\" in turn based on Andi Kleen's recvmmsg.2 page. .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2013 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2013-05-22, Akihiro MOTOKI .\" .TH SENDMMSG 2 2020\-06\-09 Linux "Linux Programmer's Manual" .SH 名前 sendmmsg \- 複数のメッセージをソケットへ送信する .SH 書式 .nf \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP .PP \fBint sendmmsg(int \fP\fIsockfd\fP\fB, struct mmsghdr *\fP\fImsgvec\fP\fB, unsigned int \fP\fIvlen\fP\fB,\fP \fB int \fP\fIflags\fP\fB);\fP .fi .SH 説明 .\" See commit 228e548e602061b08ee8e8966f567c12aa079682 \fBsendmmsg\fP() システムコールは \fBsendmsg\fP(2) の拡張で、 このシステムコールを使うと一度の呼び出しでソケットに複数のメッセージを送信できる (アプリケーションによっては性能上のメリットがある)。 .PP \fIsockfd\fP 引数は、 データを送信するソケットのファイルディスクリプターである。 .PP \fImsgvec\fP 引数は \fImmsghdr\fP 構造体の配列である。 この配列の大きさは \fIvlen\fP で指定する。 .PP \fImmsghdr\fP 構造体は \fI\fP で次のように定義されている。 .PP .in +4n .EX struct mmsghdr { struct msghdr msg_hdr; /* メッセージヘッダー */ unsigned int msg_len; /* 送信されたバイト数 */ }; .EE .in .PP \fImsg_hdr\fP フィールドは、 \fBsendmsg\fP(2) で説明されている \fImsghdr\fP 構造体である。 \fImsg_len\fP フィールドは \fImsg_hdr\fP から送信されたメッセージのバイト数を返すのに使用される。 この値は \fBsendmsg\fP(2) をこのヘッダーに対して呼び出した場合の返り値と同じである。 .PP \fIflags\fP 引数には複数のフラグを論理和 (OR) で指定できる。フラグは \fBsendmsg\fP(2) と同じである。 .PP 停止 (blocking) モードの \fBsendmmsg\fP() の呼び出しは、 \fIvlen\fP 個のメッセージが送信されるまで停止する。 非停止 (nonblocking) モードの呼び出しでは、 送信できるだけのメッセージ (最大で \fIvlen\fP 個) を送信し、 すぐに返る。 .PP \fBsendmmsg\fP() が返った際には、 \fImsgvec\fP の送信が行われた要素の \fImsg_len\fP フィールドは、対応する \fImsg_hdr\fP から送信されたバイト数が入っている。 呼び出しの返り値は、更新された \fImsgvec\fP の要素数である。 .SH 返り値 成功すると、 \fBsendmmsg\fP() は \fImsgvec\fP から送信されたメッセージ数を返す。 返り値が \fIvlen\fP よりも小さい場合、 呼び出した側では再度 \fBsendmmsg\fP を呼び出して残りのメッセージを送信することができる。 .PP エラーの場合、 \-1 を返し、 \fIerrno\fP にエラーを示す値を設定する。 .SH エラー .\" commit 728ffb86f10873aaf4abd26dde691ee40ae731fe .\" ... only return an error if no datagrams could be sent. .\" If less than the requested number of messages were sent, the application .\" must retry starting at the first failed one and if the problem is .\" persistent the error will be returned. .\" .\" This matches the behavior of other syscalls like read/write - it .\" is not an error if less than the requested number of elements are sent. エラーは \fBsendmsg\fP(2) と同じである。 エラーが返されるのは、 データグラムが全く送信できなかった場合のみである。「バグ」も参照のこと。 .SH バージョン \fBsendmmsg\fP() システムコールは Linux 3.0 で追加された。 glibc でのサポートはバージョン 2.14 で追加された。 .SH 準拠 \fBsendmmsg\fP() は Linux 固有である。 .SH 注意 .\" commit 98382f419f32d2c12d021943b87dea555677144b .\" net: Cap number of elements for sendmmsg .\" .\" To limit the amount of time we can spend in sendmmsg, cap the .\" number of elements to UIO_MAXIOV (currently 1024). .\" .\" 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. \fIvlen\fP に指定できる値の最大値は \fBUIO_MAXIOV\fP (1024) である。 .SH バグ If an error occurs after at least one message has been sent, the call succeeds, and returns the number of messages sent. The error code is lost. The caller can retry the transmission, starting at the first failed message, but there is no guarantee that, if an error is returned, it will be the same as the one that was lost on the previous call. .SH 例 以下の例では、 \fBsendmmsg\fP() を使って、 一度のシステムコールで、 \fIonetwo\fP と \fIthree\fP を二つの別々の UDP データグラムで送信する。 一つ目のデータグラムの内容は、二つのバッファーから取得される。 .PP .EX #define _GNU_SOURCE #include #include #include #include #include #include int main(void) { int sockfd; struct sockaddr_in addr; 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); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(1234); if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) { 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\en", retval); exit(0); } .EE .SH 関連項目 \fBrecvmmsg\fP(2), \fBsendmsg\fP(2), \fBsocket\fP(2), \fBsocket\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。