sendのヘルプ・マニュアル
日本語 英語
send --help
man send
SEND(2) Linux Programmer’s Manual SEND(2)
名前
send, sendto, sendmsg - ソケットへメッセージを送る
書式
#include
#include
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
説明
システムコール send(), sendto(), sendmsg() は、もう一方のソケットへメッ
セージを転送するのに使用される。
send() は、ソケットが 接続された (connected) 状態にある場合にのみ使用で
きる (つまり、どの相手に送信するかは既知である)。 send() と write(2) の
違いは、引き数に flags があるかどうかだけである。引き数 flags が 0 の場
合、 send() は write(2) と等価である。また、
send(sockfd, buf, len, flags);
は
sendto(sockfd, buf, len, flags, NULL, 0);
と等価である。
引 き数 sockfd は、データを送信するパケットのファイル・ディスクリプタで
ある。
sendto() は、接続 型 (connection-mode) の ソ ケ ッ ト (SOCK_STREAM,
SOCK_SEQPACKET) で使用された場合、引き数 dest_addr と addrlen は無視さ
れる (各々の引き数が NULL と 0 でない場合は EISCONN エラーも返される)。
ま た、ソケットが実際には接続されていなかった時には ENOTCONN エラーが返
される。接続型のソケット以外で使用された場合は、接続先の ア ド レ ス は
dest_addr で与えられ、そのサイズは addrlen で指定される。 sendmsg() で
は、接続先のアドレスは msg.msg_name で 与 え ら れ 、 そ の サ イ ズ は
msg.msg_namelen で指定される。
send() と sendto() では、メッセージは buf に格納されており、その長さは
len であると解釈される。 sendmsg() では、メッセージは配 列 msg.msg_iov
の各要素が指す位置に格納されている。 sendmsg() では、補助データ (制御情
報とも呼ばれる) を送信することもできる。
メッセージ長が長過ぎるために、そのソケットが使用するプロトコルでは、 メ
ッセージをソケットに渡されたままの形で送信することができない場合、 EMS-
GSIZE エラーが返され、そのメッセージは転送されない。
send() では、配送の失敗の通知は明示的に行われる。ローカル側でエラーが検
出された場合は、返り値 -1 として通知される。
メ ッ セージがソケットの送信バッファに入れることができない場合、 send()
は通常は停止 (block) する (ソケットが非停止 (non-blocking) I/O モードで
な い場合)。非停止モードの場合にはエラー EAGAIN か EWOULDBLOCK で失敗す
る。いつデータをさらに送信できるようになるかを知るために、 select(2) コ
ールを使用することができる。
flags 引き数は、以下のフラグの (0 個以上の) ビット単位の論理和をとった
ものを指定する。
MSG_CONFIRM (Linux 2.3.15 以降)
転送処理に進展があった、つまり相手側から成功の応答を受けたことを
リンク層に知らせる。リンク層がこの通知を受け取らなかった場合には
、通常どおり (ユニキャスト ARP を使うなどの方法で) 近傍 (neigh-
bor) の再検索を行う。 SOCK_DGRAM と SOCK_RAW のソケットに対して
のみ有効で、現在のところ IPv4 と IPv6 のみ実装されている。詳しく
は arp(7) 参照のこと。
MSG_DONTROUTE
パケットを送り出すのにゲートウェイを使用せず、直接接続されている
ネットワーク上のホストだけに送る。通常、このフラグは診断 (diag-
nostic) やルーティング・プログラムによってのみ使用される。このフ
ラグは、経路制御が行われるプロトコルファミリーに対してのみ定義さ
れている。パケットソケットには定義されていない。
MSG_DONTWAIT
非停止 (non-blocking) 操作を有効にする。操作が停止されるような場
合には EAGAIN か EWOULDBLOCK を返すよう に す る (fcntl(2) の
F_SETFL で O_NONBLOCK フラグを指定することによっても有効にでき
る)。
MSG_EOR (Linux 2.2 以降)
レコードの終了を指示する (SOCK_SEQPACKET のようにこの概念に対 応
しているソケット種別のときに有効)。
MSG_MORE (Linux 2.4.4 以降)
呼 び 出し元にさらに送るデータがあることを示す。このフラグは TCP
ソケットとともに使用され、 TCP_CORK ソケットオプションと同じ効果
が得られる (tcp(7) を参照)。 TCP_CORK との違いは、このフラグを使
うと呼び出し単位でこの機能を有効にできる点である。
Linux 2.6 以降では、このフラグは UDP ソケットでもサポートされ て
おり、このフラグ付きで送信された全てのデータを一つのデータグラム
にまとめて送信することを、カーネルに知らせる。まとめられたデータ
グラムは、このフラグを指定せずにこのシステムコールが実行された際
に初めて送信される (udp(7) に記載されているソケットオプ シ ョ ン
UDP_CORK も参照)。
MSG_NOSIGNAL (Linux 2.2 以降)
ストリーム指向のソケットで相手側が接続を切断した時に、エラーとし
て SIGPIPE を送信しないように要求する。この場合でも EPIPE は返さ
れる。
MSG_OOB
帯 域 外 (out-of-band) デ ー タをサポートするソケット (例えば
SOCK_STREAM) で 帯域外データを送る。下位プロトコルも 帯域外デ ー
タをサポートしている必要がある。
msghdr 構 造 体の内容は以下の通り。各フィールドの正確な記述については
recv(2) と以下の説明を参照すること。
struct msghdr {
void *msg_name; /* 追加のアドレス */
socklen_t msg_namelen; /* アドレスのサイズ */
struct iovec *msg_iov; /* scatter/gather 配列 */
size_t msg_iovlen; /* msg_iov の要素数 */
void *msg_control; /* 補助データ(後述) */
socklen_t msg_controllen; /* 補助データバッファ長 */
int msg_flags; /* 受信メッセージのフラグ */
};
msg_control と msg_controllen メンバーを使用して制御情報を送信するこ と
が できる。カーネルが処理できる制御バッファのソケットあたりの最大長は、
/proc/sys/net/core/optmem_max の値に制限されている。 socket(7) を参照。
返り値
成 功した場合、これらのシステムコールは送信されたバイト数を返す。エラー
の場合、 -1 を返し、 errno を適切に設定にする。
エラー
これらはソケット層で発生する一般的なエラーである。これ以外に、下層の プ
ロ トコル・モジュールで生成されたエラーが返されるかもしれない。これらに
ついては、それぞれのマニュアルを参照すること。
EACCES (Unix ドメインソケットはパス名で識別される。) ソケット・ファイル
への書き込み許可がなかったか、パス名へ到達するまでのディレクトリ
のいずれかに対する検索許可がなかった。 (path_resolution(7) も 参
照のこと)
EAGAIN または EWOULDBLOCK
ソ ケ ッ ト が非停止に設定されており、要求された操作が停止した。
POSIX.1-2001 は、この場合にどちらのエラーを返すことも認めてお り
、 これら 2 つの定数が同じ値を持つことも求めていない。したがって
、移植性が必要なアプリケーションでは、両方の可能性を確認すべきで
ある。
EBADF 無効なディスクリプターが指定された。
ECONNRESET
接続が接続相手によりリセットされた。
EDESTADDRREQ
ソ ケットが接続型 (connection-mode) ではなく、かつ送信先のアドレ
スが設定されていない。
EFAULT ユーザー空間として不正なアドレスがパラメーターとして指定された。
EINTR データが送信される前に、シグナルが発生した。 signal(7) 参照。
EINVAL 不正な引き数が渡された。
EISCONN
接続型ソケットの接続がすでに確立していたが、受信者が指定されてい
た。 (現在のところ、この状況では、このエラーが返されるか、受信者
の指定が無視されるか、のいずれかとなる)
EMSGSIZE
そのソケット種別ではソケットに渡されたままの形でメッセージを送信
する必要があるが、メッセージが大き過ぎるため送信することができな
い。
ENOBUFS
ネットワーク・インターフェースの出力キューが一杯である。一般的に
は、一時的な輻輳 (congestion) のためにインターフェースが送信を止
めていることを意味する。 (通常、Linux ではこのようなことは起こら
ない。デバイスのキューがオーバーフローした場合にはパケットは黙っ
て捨てられる)
ENOMEM メモリが足りない。
ENOTCONN
ソケットが接続されておらず、接続先も指定されていない。
ENOTSOCK
引き数 sockfd がソケットでない。
EOPNOTSUPP
引 き数 flags のいくつかのビットが、そのソケット種別では不適切な
ものである。
EPIPE 接続指向のソケットでローカル側が閉じられている 。 こ の 場 合 、
MSG_NOSIGNAL が設定されていなければ、プロセスには SIGPIPE も同時
に送られる。
準拠
4.4BSD, SVr4, POSIX.1-2001. (これらの関数コールは 4.2BSD で最初に登 場
した)。
POSIX.1-2001 に は 、 MSG_OOB と MSG_EOR フラグだけが記載されている。
MSG_CONFIRM フラグは Linux での拡張である。
注意
上記のプロトタイプは Single Unix Specification に従っている。 glibc2 も
同様である。 flags 引き数は 4.x BSD では int であり、 libc4 と libc5 で
は unsigned int である。 len 引き数は 4.x BSD と libc4 では int であ り
、 libc5 で は size_t である。 addrlen 引き数は 4.x BSD と libc4 と
libc5 では int である。 accept(2) も参照すること。
POSIX.1-2001 では、構造体 msghdr の フ ィ ー ル ド msg_controllen は
socklen_t 型であるべきだとされているが、現在の glibc (glibc 2.4) では
size_t 型である。
バグ
Linux は ENOTCONN を返す状況で EPIPE を返すことがある。
例
sendto() の利用例が getaddrinfo(3) に記載されている。
関連項目
fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2), shutdown(2),
socket(2), write(2), cmsg(3), ip(7), socket(7), tcp(7), udp(7)
Linux 2009-02-23 SEND(2)
SEND(2) Linux Programmer’s Manual SEND(2)
NAME
send, sendto, sendmsg - send a message on a socket
SYNOPSIS
#include
#include
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
DESCRIPTION
The system calls send(), sendto(), and sendmsg() are used to transmit a
message to another socket.
The send() call may be used only when the socket is in a connected
state (so that the intended recipient is known). The only difference
between send() and write(2) is the presence of flags. With zero flags
argument, send() is equivalent to write(2). Also, the following call
send(sockfd, buf, len, flags);
is equivalent to
sendto(sockfd, buf, len, flags, NULL, 0);
The argument sockfd is the file descriptor of the sending socket.
If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
socket, the arguments dest_addr and addrlen are ignored (and the error
EISCONN may be returned when they are not NULL and 0), and the error
ENOTCONN is returned when the socket was not actually connected. Oth-
erwise, the address of the target is given by dest_addr with addrlen
specifying its size. For sendmsg(), the address of the target is given
by msg.msg_name, with msg.msg_namelen specifying its size.
For send() and sendto(), the message is found in buf and has length
len. For sendmsg(), the message is pointed to by the elements of the
array msg.msg_iov. The sendmsg() call also allows sending ancillary
data (also known as control information).
If the message is too long to pass atomically through the underlying
protocol, the error EMSGSIZE is returned, and the message is not trans-
mitted.
No indication of failure to deliver is implicit in a send(). Locally
detected errors are indicated by a return value of -1.
When the message does not fit into the send buffer of the socket,
send() normally blocks, unless the socket has been placed in non-block-
ing I/O mode. In non-blocking mode it would fail with the error EAGAIN
or EWOULDBLOCK in this case. The select(2) call may be used to deter-
mine when it is possible to send more data.
The flags argument is the bitwise OR of zero or more of the following
flags.
MSG_CONFIRM (Since Linux 2.3.15)
Tell the link layer that forward progress happened: you got a
successful reply from the other side. If the link layer doesn’t
get this it will regularly reprobe the neighbor (e.g., via a
unicast ARP). Only valid on SOCK_DGRAM and SOCK_RAW sockets and
currently only implemented for IPv4 and IPv6. See arp(7) for
details.
MSG_DONTROUTE
Don’t use a gateway to send out the packet, only send to hosts
on directly connected networks. This is usually used only by
diagnostic or routing programs. This is only defined for proto-
col families that route; packet sockets don’t.
MSG_DONTWAIT (since Linux 2.2)
Enables non-blocking operation; if the operation would block,
EAGAIN or EWOULDBLOCK is returned (this can also be enabled
using the O_NONBLOCK flag with the F_SETFL fcntl(2)).
MSG_EOR (since Linux 2.2)
Terminates a record (when this notion is supported, as for sock-
ets of type SOCK_SEQPACKET).
MSG_MORE (Since Linux 2.4.4)
The caller has more data to send. This flag is used with TCP
sockets to obtain the same effect as the TCP_CORK socket option
(see tcp(7)), with the difference that this flag can be set on a
per-call basis.
Since Linux 2.6, this flag is also supported for UDP sockets,
and informs the kernel to package all of the data sent in calls
with this flag set into a single datagram which is only trans-
mitted when a call is performed that does not specify this flag.
(See also the UDP_CORK socket option described in udp(7).)
MSG_NOSIGNAL (since Linux 2.2)
Requests not to send SIGPIPE on errors on stream oriented sock-
ets when the other end breaks the connection. The EPIPE error
is still returned.
MSG_OOB
Sends out-of-band data on sockets that support this notion
(e.g., of type SOCK_STREAM); the underlying protocol must also
support out-of-band data.
The definition of the msghdr structure follows. See recv(2) and below
for an exact description of its fields.
struct msghdr {
void *msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void *msg_control; /* ancillary data, see below */
socklen_t msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */
};
You may send control information using the msg_control and msg_con-
trollen members. The maximum control buffer length the kernel can pro-
cess is limited per socket by the value in /proc/sys/net/core/opt-
mem_max; see socket(7).
RETURN VALUE
On success, these calls return the number of characters sent. On
error, -1 is returned, and errno is set appropriately.
ERRORS
These are some standard errors generated by the socket layer. Addi-
tional errors may be generated and returned from the underlying
protocol modules; see their respective manual pages.
EACCES (For Unix domain sockets, which are identified by pathname)
Write permission is denied on the destination socket file, or
search permission is denied for one of the directories the path
prefix. (See path_resolution(7).)
EAGAIN or EWOULDBLOCK
The socket is marked non-blocking and the requested operation
would block. POSIX.1-2001 allows either error to be returned
for this case, and does not require these constants to have the
same value, so a portable application should check for both pos-
sibilities.
EBADF An invalid descriptor was specified.
ECONNRESET
Connection reset by peer.
EDESTADDRREQ
The socket is not connection-mode, and no peer address is set.
EFAULT An invalid user space address was specified for an argument.
EINTR A signal occurred before any data was transmitted; see sig-
nal(7).
EINVAL Invalid argument passed.
EISCONN
The connection-mode socket was connected already but a recipient
was specified. (Now either this error is returned, or the
recipient specification is ignored.)
EMSGSIZE
The socket type requires that message be sent atomically, and
the size of the message to be sent made this impossible.
ENOBUFS
The output queue for a network interface was full. This gener-
ally indicates that the interface has stopped sending, but may
be caused by transient congestion. (Normally, this does not
occur in Linux. Packets are just silently dropped when a device
queue overflows.)
ENOMEM No memory available.
ENOTCONN
The socket is not connected, and no target has been given.
ENOTSOCK
The argument sockfd is not a socket.
EOPNOTSUPP
Some bit in the flags argument is inappropriate for the socket
type.
EPIPE The local end has been shut down on a connection oriented
socket. In this case the process will also receive a SIGPIPE
unless MSG_NOSIGNAL is set.
CONFORMING TO
4.4BSD, SVr4, POSIX.1-2001. These function calls appeared in 4.2BSD.
POSIX.1-2001 only describes the MSG_OOB and MSG_EOR flags. The
MSG_CONFIRM flag is a Linux extension.
NOTES
The prototypes given above follow the Single Unix Specification, as
glibc2 also does; the flags argument was int in 4.x BSD, but unsigned
int in libc4 and libc5; the len argument was int in 4.x BSD and libc4,
but size_t in libc5; the addrlen argument was int in 4.x BSD and libc4
and libc5. See also accept(2).
According to POSIX.1-2001, the msg_controllen field of the msghdr
structure should be typed as socklen_t, but glibc currently (2.4) types
it as size_t.
BUGS
Linux may return EPIPE instead of ENOTCONN.
EXAMPLE
An example of the use of sendto() is shown in getaddrinfo(3).
SEE ALSO
fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2), shutdown(2),
socket(2), write(2), cmsg(3), ip(7), socket(7), tcp(7), udp(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2009-02-23 SEND(2)