OSDN Git Service

(split) LDP: Restore and add Copyrights for draft pages
[linuxjm/LDP_man-pages.git] / draft / man2 / recvmmsg.2
1 .\" Copyright (C) 2011 by Andi Kleen <andi@firstfloor.org>
2 .\" and Copyright (c) 2011 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Syscall added in following commit
27 .\"     commit a2e2725541fad72416326798c2d7fa4dafb7d337
28 .\"     Author: Arnaldo Carvalho de Melo <acme@redhat.com>
29 .\"     Date:   Mon Oct 12 23:40:10 2009 -0700
30 .\"
31 .\"*******************************************************************
32 .\"
33 .\" This file was generated with po4a. Translate the source file.
34 .\"
35 .\"*******************************************************************
36 .\"
37 .\" Japanese Version Copyright (c) 2013  Akihiro MOTOKI
38 .\"         all rights reserved.
39 .\" Translated 2013-05-22, Akihiro MOTOKI <amotoki@gmail.com>
40 .\"
41 .TH RECVMMSG 2 2012\-12\-24 Linux "Linux Programmer's Manual"
42 .SH 名前
43 recvmmsg \- 複数のメッセージをソケットから受信する
44 .SH 書式
45 .nf
46 \fB#define _GNU_SOURCE\fP
47 \fB#include <sys/socket.h>\fP
48
49 \fBint recvmmsg(int \fP\fIsockfd\fP\fB, struct mmsghdr *\fP\fImsgvec\fP\fB, unsigned int \fP\fIvlen\fP\fB,\fP
50 .br
51 \fB             unsigned int \fP\fIflags\fP\fB, struct timespec *\fP\fItimeout\fP\fB);\fP
52 .fi
53 .SH 説明
54 \fBrecvmmsg\fP() システムコールは \fBrecvmsg\fP(2) の拡張で、
55 このシステムコールを使うと一度の呼び出しでソケットから複数のメッセージを受信することができる (アプリケーションによっては性能上のメリットがある)。
56 他に \fBrecvmsg\fP(2) から拡張されている点としては、受信操作におけるタイムアウトのサポートがある。
57
58 \fIsockfd\fP 引き数は、データを受信するソケットのファイルディスクリプタである。
59
60 \fImsgvec\fP 引き数は \fImmsghdr\fP 構造体の配列である。 この配列の大きさは \fIvlen\fP で指定する。
61
62 \fImmsghdr\fP 構造体は \fI<sys/socket.h>\fP で次のように定義されている。
63
64 .in +4n
65 .nf
66 struct mmsghdr {
67     struct msghdr msg_hdr;  /* メッセージヘッダ */
68     unsigned int  msg_len;  /* このヘッダで受信されたバイト数 */
69 };
70 .fi
71 .in
72 .PP
73 \fImsg_hdr\fP フィールドは、 \fBrecvmsg\fP(2) で説明されている \fImsghdr\fP 構造体である。 \fImsg_len\fP
74 フィールドは、 このエントリで返されるメッセージのバイト数で、 このヘッダに対して \fBrecvmsg\fP(2) を呼び出した場合の返り値と同じ値が入る。
75
76 \fIflags\fP 引き数には複数のフラグを論理和 (OR) で指定できる。 フラグは、 \fBrecvmsg\fP(2)
77 で説明されているものに加えて、以下が使用できる。
78 .TP 
79 \fBMSG_WAITFORONE\fP (Linux 2.6.34 以降)
80 最初のメッセージを受信後に \fBMSG_DONTWAIT\fP を有効にする。
81 .PP
82 \fItimeout\fP 引き数は \fIstruct timespec\fP (\fBclock_gettime\fP(2) 参照) へのポインタで、
83 この構造体で受信操作のタイムアウト (秒とナノ秒) を指定する
84 (待ち時間はシステムクロックの粒度に切り上げられ、カーネルのスケジューリング遅延により少しだけ長くなる可能性がある)。 \fItimeout\fPが
85 \fINULL\fP の場合、 受信操作は無期限に停止 (block) する。
86
87 停止 (blocking) モードの \fBrecvmmsg\fP() の呼び出しは、 \fIvlen\fP
88 個のメッセージを受信するか、タイムアウトが満了するまで停止する。 非停止 (nonblocking) モードの呼び出しでは、 読み出し可能なメッセージ
89 (最大で \fIvlen\fP 個) を読み出し、 すぐに返る。
90
91 \fBrecvmmsg\fP() が返った際には、 \fImsgvec\fP のうちデータが受信された要素には、受信したそれぞれのメッセージの情報が格納されている。
92 また、 \fImsg_len\fP には受信したメッセージの大きさが入り、 \fImsg_hdr\fP の各フィールドは \fBrecvmsg\fP(2)
93 に書かれている通りに更新される。 呼び出しの返り値は、更新された \fImsgvec\fP の要素数である。
94 .SH 返り値
95 成功すると、 \fBrecvmmsg\fP() は \fImsgvec\fP に受信されたメッセージ数を返す。 エラーの場合、 \-1 を返し、 \fIerrno\fP
96 にエラーを示す値を設定する。
97 .SH エラー
98 エラーは \fBrecvmsg\fP(2) と同じである。 これに加えて、以下のエラーが起こる場合がある。
99 .TP 
100 \fBEINVAL\fP
101 \fItimeout\fP が無効である。
102 .SH バージョン
103 \fBrecvmmsg\fP() システムコールは Linux 2.6.33 で追加された。 glibc でのサポートはバージョン 2.12
104 以降で利用可能である。
105 .SH 準拠
106 \fBrecvmmsg\fP() は Linux 固有である。
107 .SH 例
108 .PP
109 以下のプログラムは、 \fBrecvmmsg\fP() を使って複数のメッセージをソケットから受信し、それらを複数のバッファに格納する。
110 呼び出しは、すべてのバッファにメッセージが格納されるか、 指定したタイムアウト時間が経過すると返る。
111
112 以下のコマンドは、 ランダムな数字が入った UDP データグラムを定期的に生成する。
113 .in +4n
114 .nf
115
116 $\fB while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234; \fP
117 \fBsleep 0.25; done\fP
118 .fi
119 .in
120
121 生成されたデータグラムをサンプルアプリケーションが読み出し、以下のような出力が得られる。
122 .in +4n
123 .nf
124
125 $\fB ./a.out\fP
126 5 messages received
127 1 11782
128 2 11345
129 3 304
130 4 13514
131 5 28421
132 .fi
133 .in
134 .SS プログラムのソース
135 \&
136 .nf
137 #define _GNU_SOURCE
138 #include <netinet/ip.h>
139 #include <stdio.h>
140 #include <stdlib.h>
141 #include <string.h>
142 #include <sys/socket.h>
143
144 int
145 main(void)
146 {
147 #define VLEN 10
148 #define BUFSIZE 200
149 #define TIMEOUT 1
150     int sockfd, retval, i;
151     struct sockaddr_in sa;
152     struct mmsghdr msgs[VLEN];
153     struct iovec iovecs[VLEN];
154     char bufs[VLEN][BUFSIZE+1];
155     struct timespec timeout;
156
157     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
158     if (sockfd == \-1) {
159         perror("socket()");
160         exit(EXIT_FAILURE);
161     }
162
163     sa.sin_family = AF_INET;
164     sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
165     sa.sin_port = htons(1234);
166     if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {
167         perror("bind()");
168         exit(EXIT_FAILURE);
169     }
170
171     memset(msgs, 0, sizeof(msgs));
172     for (i = 0; i < VLEN; i++) {
173         iovecs[i].iov_base         = bufs[i];
174         iovecs[i].iov_len          = BUFSIZE;
175         msgs[i].msg_hdr.msg_iov    = &iovecs[i];
176         msgs[i].msg_hdr.msg_iovlen = 1;
177     }
178
179     timeout.tv_sec = TIMEOUT;
180     timeout.tv_nsec = 0;
181
182     retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
183     if (retval == \-1) {
184         perror("recvmmsg()");
185         exit(EXIT_FAILURE);
186     }
187
188     printf("%d messages received\en", retval);
189     for (i = 0; i < retval; i++) {
190         bufs[i][msgs[i].msg_len] = 0;
191         printf("%d %s", i+1, bufs[i]);
192     }
193     exit(EXIT_SUCCESS);
194 }
195 .fi
196 .SH 関連項目
197 \fBclock_gettime\fP(2), \fBrecvmsg\fP(2), \fBsendmmsg\fP(2), \fBsendmsg\fP(2),
198 \fBsocket\fP(2), \fBsocket\fP(7)
199 .SH この文書について
200 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
201 である。プロジェクトの説明とバグ報告に関する情報は
202 http://www.kernel.org/doc/man\-pages/ に書かれている。