OSDN Git Service

4678f5cce5d2b2fef88d911136b72df724448ce9
[linuxjm/LDP_man-pages.git] / release / 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 .TH RECVMMSG 2 2012\-12\-24 Linux "Linux Programmer's Manual"
37 .SH 名前
38 recvmmsg \- 複数のメッセージをソケットから受信する
39 .SH 書式
40 .nf
41 \fB#define _GNU_SOURCE\fP
42 \fB#include <sys/socket.h>\fP
43
44 \fBint recvmmsg(int \fP\fIsockfd\fP\fB, struct mmsghdr *\fP\fImsgvec\fP\fB, unsigned int \fP\fIvlen\fP\fB,\fP
45 .br
46 \fB             unsigned int \fP\fIflags\fP\fB, struct timespec *\fP\fItimeout\fP\fB);\fP
47 .fi
48 .SH 説明
49 \fBrecvmmsg\fP() システムコールは \fBrecvmsg\fP(2) の拡張で、
50 このシステムコールを使うと一度の呼び出しでソケットから複数のメッセージを受信することができる (アプリケーションによっては性能上のメリットがある)。
51 他に \fBrecvmsg\fP(2) から拡張されている点としては、受信操作におけるタイムアウトのサポートがある。
52
53 \fIsockfd\fP 引き数は、データを受信するソケットのファイルディスクリプタである。
54
55 \fImsgvec\fP 引き数は \fImmsghdr\fP 構造体の配列である。 この配列の大きさは \fIvlen\fP で指定する。
56
57 \fImmsghdr\fP 構造体は \fI<sys/socket.h>\fP で次のように定義されている。
58
59 .in +4n
60 .nf
61 struct mmsghdr {
62     struct msghdr msg_hdr;  /* メッセージヘッダ */
63     unsigned int  msg_len;  /* このヘッダで受信されたバイト数 */
64 };
65 .fi
66 .in
67 .PP
68 \fImsg_hdr\fP フィールドは、 \fBrecvmsg\fP(2) で説明されている \fImsghdr\fP 構造体である。 \fImsg_len\fP
69 フィールドは、 このエントリで返されるメッセージのバイト数で、 このヘッダに対して \fBrecvmsg\fP(2) を呼び出した場合の返り値と同じ値が入る。
70
71 \fIflags\fP 引き数には複数のフラグを論理和 (OR) で指定できる。 フラグは、 \fBrecvmsg\fP(2)
72 で説明されているものに加えて、以下が使用できる。
73 .TP 
74 \fBMSG_WAITFORONE\fP (Linux 2.6.34 以降)
75 最初のメッセージを受信後に \fBMSG_DONTWAIT\fP を有効にする。
76 .PP
77 \fItimeout\fP 引き数は \fIstruct timespec\fP (\fBclock_gettime\fP(2) 参照) へのポインタで、
78 この構造体で受信操作のタイムアウト (秒とナノ秒) を指定する
79 (待ち時間はシステムクロックの粒度に切り上げられ、カーネルのスケジューリング遅延により少しだけ長くなる可能性がある)。 \fItimeout\fPが
80 \fINULL\fP の場合、 受信操作は無期限に停止 (block) する。
81
82 停止 (blocking) モードの \fBrecvmmsg\fP() の呼び出しは、 \fIvlen\fP
83 個のメッセージを受信するか、タイムアウトが満了するまで停止する。 非停止 (nonblocking) モードの呼び出しでは、 読み出し可能なメッセージ
84 (最大で \fIvlen\fP 個) を読み出し、 すぐに返る。
85
86 \fBrecvmmsg\fP() が返った際には、 \fImsgvec\fP のうちデータが受信された要素には、受信したそれぞれのメッセージの情報が格納されている。
87 また、 \fImsg_len\fP には受信したメッセージの大きさが入り、 \fImsg_hdr\fP の各フィールドは \fBrecvmsg\fP(2)
88 に書かれている通りに更新される。 呼び出しの返り値は、更新された \fImsgvec\fP の要素数である。
89 .SH 返り値
90 成功すると、 \fBrecvmmsg\fP() は \fImsgvec\fP に受信されたメッセージ数を返す。 エラーの場合、 \-1 を返し、 \fIerrno\fP
91 にエラーを示す値を設定する。
92 .SH エラー
93 エラーは \fBrecvmsg\fP(2) と同じである。 これに加えて、以下のエラーが起こる場合がある。
94 .TP 
95 \fBEINVAL\fP
96 \fItimeout\fP が無効である。
97 .SH バージョン
98 \fBrecvmmsg\fP() システムコールは Linux 2.6.33 で追加された。 glibc でのサポートはバージョン 2.12
99 以降で利用可能である。
100 .SH 準拠
101 \fBrecvmmsg\fP() は Linux 固有である。
102 .SH 例
103 .PP
104 以下のプログラムは、 \fBrecvmmsg\fP() を使って複数のメッセージをソケットから受信し、それらを複数のバッファに格納する。
105 呼び出しは、すべてのバッファにメッセージが格納されるか、 指定したタイムアウト時間が経過すると返る。
106
107 以下のコマンドは、 ランダムな数字が入った UDP データグラムを定期的に生成する。
108 .in +4n
109 .nf
110
111 $\fB while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234; \fP
112 \fBsleep 0.25; done\fP
113 .fi
114 .in
115
116 生成されたデータグラムをサンプルアプリケーションが読み出し、以下のような出力が得られる。
117 .in +4n
118 .nf
119
120 $\fB ./a.out\fP
121 5 messages received
122 1 11782
123 2 11345
124 3 304
125 4 13514
126 5 28421
127 .fi
128 .in
129 .SS プログラムのソース
130 \&
131 .nf
132 #define _GNU_SOURCE
133 #include <netinet/ip.h>
134 #include <stdio.h>
135 #include <stdlib.h>
136 #include <string.h>
137 #include <sys/socket.h>
138
139 int
140 main(void)
141 {
142 #define VLEN 10
143 #define BUFSIZE 200
144 #define TIMEOUT 1
145     int sockfd, retval, i;
146     struct sockaddr_in sa;
147     struct mmsghdr msgs[VLEN];
148     struct iovec iovecs[VLEN];
149     char bufs[VLEN][BUFSIZE+1];
150     struct timespec timeout;
151
152     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
153     if (sockfd == \-1) {
154         perror("socket()");
155         exit(EXIT_FAILURE);
156     }
157
158     sa.sin_family = AF_INET;
159     sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
160     sa.sin_port = htons(1234);
161     if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {
162         perror("bind()");
163         exit(EXIT_FAILURE);
164     }
165
166     memset(msgs, 0, sizeof(msgs));
167     for (i = 0; i < VLEN; i++) {
168         iovecs[i].iov_base         = bufs[i];
169         iovecs[i].iov_len          = BUFSIZE;
170         msgs[i].msg_hdr.msg_iov    = &iovecs[i];
171         msgs[i].msg_hdr.msg_iovlen = 1;
172     }
173
174     timeout.tv_sec = TIMEOUT;
175     timeout.tv_nsec = 0;
176
177     retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
178     if (retval == \-1) {
179         perror("recvmmsg()");
180         exit(EXIT_FAILURE);
181     }
182
183     printf("%d messages received\en", retval);
184     for (i = 0; i < retval; i++) {
185         bufs[i][msgs[i].msg_len] = 0;
186         printf("%d %s", i+1, bufs[i]);
187     }
188     exit(EXIT_SUCCESS);
189 }
190 .fi
191 .SH 関連項目
192 \fBclock_gettime\fP(2), \fBrecvmsg\fP(2), \fBsendmmsg\fP(2), \fBsendmsg\fP(2),
193 \fBsocket\fP(2), \fBsocket\fP(7)
194 .SH この文書について
195 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
196 である。プロジェクトの説明とバグ報告に関する情報は
197 http://www.kernel.org/doc/man\-pages/ に書かれている。