'\" t .\" Copyright (c) 1983, 1991 The Regents of the University of California. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $Id: socket.2,v 1.4 1999/05/13 11:33:42 freitag Exp $ .\" .\" Modified 1993-07-24 by Rik Faith .\" Modified 1996-10-22 by Eric S. Raymond .\" Modified 1998, 1999 by Andi Kleen .\" Modified 2002-07-17 by Michael Kerrisk .\" Modified 2004-06-17 by Michael Kerrisk .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated Mon Mar 3 23:40:11 JST 1997 .\" by HANATAKA Shinya .\" Modified Sun Aug 15 23:52:28 JST 1999 .\" by HANATAKA Shinya .\" Updated Mon Jan 13 JST 2000 by Kentaro Shirakata .\" Updated Mon Oct 15 JST 2001 by Kentaro Shirakata .\" Updated Mon Oct 26 JST 2002 by Kentaro Shirakata .\" Updated 2008-11-09, Akihiro MOTOKI , LDP v3.13 .\" .\"WORD: endpoint 端点 .\"WORD: descriptor ディスクリプター .\"WORD: link layer リンク層 .\"WORD: byte stream バイト・ストリーム .\"WORD: out-of-band 帯域外 .\"WORD: sequenced 順序性のある .\"WORD: reliable 信頼性のある .\"WORD: two-way 双方向の .\"WORD: datagram データグラム .\"WORD: implement 実装 .\"WORD: super-user スーパー・ユーザー .\"WORD: full-duplex 全二重 .\"WORD: non-blocking I/O 非停止 I/O .\"WORD: asynchronous 非同期 .\" .TH SOCKET 2 2009-01-19 "Linux" "Linux Programmer's Manual" .\"O .SH NAME .SH 名前 .\"O socket \- create an endpoint for communication socket \- 通信のための端点(endpoint)を作成する .\"O .SH SYNOPSIS .SH 書式 .\"O .BR "#include " " /* See NOTES */" .BR "#include " " /* 「注意」参照 */" .br .B #include .sp .BI "int socket(int " domain ", int " type ", int " protocol ); .\"O .SH DESCRIPTION .SH 説明 .\"O .BR socket () .\"O creates an endpoint for communication and returns a descriptor. .BR socket () は通信のための端点(endpoint)を作成し、ディスクリプター(descriptor)を返す。 .PP .\"O The .\"O .I domain .\"O argument specifies a communication domain; this selects the protocol .\"O family which will be used for communication. .\"O These families are defined in .\"O .IR . .\"O The currently understood formats include: .I domain 引数は通信を行なうドメインを指定する; これはどの プロトコル・ファミリ(protocol family)を通信に使用するかを指定する。 これらのファミリは .I に定義されている。 現在、理解できるフォーマットは以下の通り。 .TS tab(:); l l l. .\"O Name:Purpose:Man page 名前:目的:マニュアル T{ .BR AF_UNIX ", " AF_LOCAL T}:T{ .\"O Local communication ローカル通信 T}:T{ .BR unix (7) T} T{ .B AF_INET .\"O T}:IPv4 Internet protocols:T{ T}:IPv4 インターネット・プロトコル:T{ .BR ip (7) T} T{ .B AF_INET6 .\"O T}:IPv6 Internet protocols:T{ T}:IPv6 インターネット・プロトコル:T{ .BR ipv6 (7) T} T{ .B AF_IPX .\"O T}:IPX \- Novell protocols: T}:IPX \- Novell プロトコル: T{ .B AF_NETLINK T}:T{ .\"O Kernel user interface device カーネル・ユーザ・デバイス T}:T{ .BR netlink (7) T} T{ .B AF_X25 .\"O T}:ITU-T X.25 / ISO-8208 protocol:T{ T}:ITU-T X.25 / ISO-8208 プロトコル:T{ .BR x25 (7) T} T{ .B AF_AX25 T}:T{ .\"O Amateur radio AX.25 protocol アマチュア無線 AX.25 プロトコル T}: T{ .B AF_ATMPVC .\"O T}:Access to raw ATM PVCs: T}:生の ATM PVC にアクセスする: T{ .B AF_APPLETALK .\"O T}:Appletalk:T{ T}:アップルトーク:T{ .BR ddp (7) T} T{ .B AF_PACKET T}:T{ .\"O Low level packet interface 低レベルのパケットインターフェース T}:T{ .BR packet (7) T} .TE .PP .\"O The socket has the indicated .\"O .IR type , .\"O which specifies the communication semantics. .\"O Currently defined types .\"O are: ソケットは .I type で指定される型を持ち、それは通信方式(semantics)を指定する。 定義されている型は現在以下の通り。 .TP 16 .B SOCK_STREAM .\"O Provides sequenced, reliable, two-way, connection-based byte streams. .\"O An out-of-band data transmission mechanism may be supported. 順序性と信頼性があり、双方向の、接続された バイト・ストリーム(byte stream)を提供する。 帯域外(out-of-band)データ転送メカニズムもサポートされる。 .TP .B SOCK_DGRAM .\"O Supports datagrams (connectionless, unreliable messages of a fixed .\"O maximum length). データグラム(接続、信頼性無し、固定最大長メッセージ) をサポートする。 .TP .B SOCK_SEQPACKET .\"O Provides a sequenced, reliable, two-way connection-based data .\"O transmission path for datagrams of fixed maximum length; a consumer is .\"O required to read an entire packet with each input system call. 固定最大長のデータグラム転送パスに基づいた順序性、信頼性のある 双方向の接続に基づいた通信を提供する。受け取り側ではそれぞれの入力 システム・コールでパケット全体を読み取ることが要求される。 .TP .B SOCK_RAW .\"O Provides raw network protocol access. 生のネットワーク・プロトコルへのアクセスを提供する。 .TP .B SOCK_RDM .\"O Provides a reliable datagram layer that does not guarantee ordering. 信頼性はあるが、順序は保証しないデータグラム層を提供する。 .TP .B SOCK_PACKET .\"O Obsolete and should not be used in new programs; .\"O see .\"O .BR packet (7). 廃止されており新しいプログラムで使用してはいけない。 .BR packet (7) を参照すること .PP .\"O Some socket types may not be implemented by all protocol families; .\"O for example, .\"O .B SOCK_SEQPACKET .\"O is not implemented for .\"O .BR AF_INET . ある種のソケット型が全てのプロトコル・ファミリで実装されているわけではない。 例えば .B SOCK_SEQPACKET は .B AF_INET には実装されていない。 .PP .\"O Since Linux 2.6.27, the .\"O .I type .\"O argument serves a second purpose: .\"O in addition to specifying a socket type, .\"O it may include the bitwise OR of any of the following values, .\"O to modify the behavior of .\"O .BR socket (): Linux 2.6.27 以降では、 .I type 引数は二つ目の目的にも使用される。 ソケットの型を指定するのに加えて、 以下の値のいくつかをビット単位の論理和 (OR) で指定することで、 .BR socket () の振舞いを変更することができる。 .TP 16 .B SOCK_NONBLOCK .\"O Set the .\"O .BR O_NONBLOCK .\"O file status flag on the new open file description. .\"O Using this flag saves extra calls to .\"O .BR fcntl (2) .\"O to achieve the same result. 新しく生成されるオープンファイル記述 (open file description) の .B O_NONBLOCK ファイルステータスフラグをセットする。 このフラグを使うことで、 .B O_NONBLOCK をセットするために .BR fcntl (2) を追加で呼び出す必要がなくなる。 .TP .B SOCK_CLOEXEC .\"O Set the close-on-exec .\"O .RB ( FD_CLOEXEC ) .\"O flag on the new file descriptor. .\"O See the description of the .\"O .B O_CLOEXEC .\"O flag in .\"O .BR open (2) .\"O for reasons why this may be useful. 新しいファイル・ディスクリプターに対して close-on-exec .RB ( FD_CLOEXEC ) フラグをセットする。 このフラグが役に立つ理由については、 .BR open (2) の .B O_CLOEXEC フラグの説明を参照のこと。 .PP .\"O The .\"O .I protocol .\"O specifies a particular protocol to be used with the socket. .\"O Normally only a single protocol exists to support a particular .\"O socket type within a given protocol family, in which case .\"O .I protocol .\"O can be specified as 0. .\"O However, it is possible that many protocols may exist, in .\"O which case a particular protocol must be specified in this manner. .\"O The protocol number to use is specific to the \*(lqcommunication domain\*(rq .\"O in which communication is to take place; see .\"O .BR protocols (5). .\"O See .\"O .BR getprotoent (3) .\"O on how to map protocol name strings to protocol numbers. .I protocol はソケットによって使用される固有のプロトコルを指定する。通常それぞれの ソケットは、与えられたプロトコル・ファミリの種類ごとに一つのプロトコルのみを サポートする。 その場合は .I protocol に 0 を指定できる。 しかし、多くのプロトコルが存在してもかまわない。 この場合にはこの方法により固有のプロトコルを指定する必要がある。 使用されるプロトコル番号は通信の行なわれる\*(lq通信ドメイン\*(rqに 固有である; .BR protocols (5) を参照すること。 プロトコル名をどうやってプロトコル番号に対応させるかについては .BR getprotoent (3) を参照すること。 .PP .\"O Sockets of type .\"O .B SOCK_STREAM .\"O are full-duplex byte streams, similar to pipes. .\"O They do not preserve .\"O record boundaries. .\"O A stream socket must be in .\"O a .\"O .I connected .\"O state before any data may be sent or received on it. .\"O A connection to .\"O another socket is created with a .\"O .BR connect (2) .\"O call. .\"O Once connected, data may be transferred using .\"O .BR read (2) .\"O and .\"O .BR write (2) .\"O calls or some variant of the .\"O .BR send (2) .\"O and .\"O .BR recv (2) .\"O calls. .\"O When a session has been completed a .\"O .BR close (2) .\"O may be performed. .\"O Out-of-band data may also be transmitted as described in .\"O .BR send (2) .\"O and received as described in .\"O .BR recv (2). .B SOCK_STREAM 型のソケットはパイプのような全二重バイト・ストリームである。 これらはレコード境界を保存しない。 ストリームは、ソケットがデータを送ったり受けたりする前に .I 接続された 状態になってなければならない。他のソケットへの接続は .BR connect (2) コールによって行なわれる。一度接続したらデータは .BR read (2) と .BR write (2) コールや .BR send (2) と .BR recv (2) コールの変種を使用して転送できる。 セッションが完了したら .BR close (2) が行なわれる。帯域外データの転送も .BR send (2) に記述されており、 受信も .BR recv (2) に記述されている。 .PP .\"O The communications protocols which implement a .\"O .B SOCK_STREAM .\"O ensure that data is not lost or duplicated. .\"O If a piece of data for which .\"O the peer protocol has buffer space cannot be successfully transmitted .\"O within a reasonable length of time, then the connection is considered .\"O to be dead. .\"O When .\"O .B SO_KEEPALIVE .\"O is enabled on the socket the protocol checks in a protocol-specific .\"O manner if the other end is still alive. .B SOCK_STREAM を実装した通信プロトコルはデータに損失や重複がないことを保証する。 もし相手のプロトコルがバッファー空間を持つ データの断片を適当な時間のうちに転送できなければ、 接続は断たれたとみなす。そのソケット .B SO_KEEPALIVE が有効になっている場合、プロトコル独自の方法で接続の相手側がまだ 有効であるかをチェックする。 .\"O A .\"O .B SIGPIPE .\"O signal is raised if a process sends or receives .\"O on a broken stream; this causes naive processes, .\"O which do not handle the signal, to exit. もしプロセスが、壊れたストリームでデータを送受信しようとした場合には .B SIGPIPE シグナルが送られる; これは通常のそのシグナルを扱っていないプロセスを 終了させる。 .\"O .B SOCK_SEQPACKET .\"O sockets employ the same system calls as .\"O .B SOCK_STREAM .\"O sockets. .\"O The only difference is that .\"O .BR read (2) .\"O calls will return only the amount of data requested, .\"O and any data remaining in the arriving packet will be discarded. .\"O Also all message boundaries in incoming datagrams are preserved. .B SOCK_SEQPACKET ソケットは .B SOCK_STREAM ソケットと同じシステム・コールを使用する。 唯一の違いは .BR read (2) コールが要求された量のデータのみを返し、到着したパケットの残りの部分を 捨ててしまうことである。同様に入ってくるデータグラムの全てのメッセージ境界は 保存される。 .PP .\"O .B SOCK_DGRAM .\"O and .\"O .B SOCK_RAW .\"O sockets allow sending of datagrams to correspondents named in .\"O .BR sendto (2) .\"O calls. .\"O Datagrams are generally received with .\"O .BR recvfrom (2), .\"O which returns the next datagram along with the address of its sender. .B SOCK_DGRAM と .B SOCK_RAW ソケットは .BR sendto (2) コールで指定された相手へデータグラムを送ることが許されている。 データグラムは一般に .BR recvfrom (2) で受けとり、 このコールは次のデータグラムを送信者のアドレスと一緒に返す。 .PP .\"O .B SOCK_PACKET .\"O is an obsolete socket type to receive raw packets directly from the .\"O device driver. .\"O Use .\"O .BR packet (7) .\"O instead. .B SOCK_PACKET は古いソケット型で、生(raw)のパケットをデバイスドライバから 直接受信するためのものである。 今は代わりに .BR packet (7) を用いること。 .PP .\"O An .\"O .BR fcntl (2) .\"O .B F_SETOWN .\"O operation can be used to specify a process or process group to receive a .\"O .B SIGURG .\"O signal when the out-of-band data arrives or .\"O .B SIGPIPE .\"O signal when a .\"O .B SOCK_STREAM .\"O connection breaks unexpectedly. .BR fcntl (2) の .B F_SETOWN 操作を使って、シグナル .B SIGURG や .B SIGPIPE を受けとるプロセス・グループを指定できる。 .B SIGURG シグナルは帯域外データが到着した時に、 .B SIGPIPE シグナルは .B SOCK_STREAM 接続が予期せず切断された時に送られる。 .\"O This operation may also be used to set the process or process group .\"O that receives the I/O and asynchronous notification of I/O events via .\"O .BR SIGIO . また、 .B F_SETOWN 操作は、I/O や I/O イベントの非同期 (asynchronous) 通知を .B SIGIO を経由で受け取るプロセスやプロセス・グループを設定するのにも使用できる。 .\"O Using .\"O .B F_SETOWN .\"O is equivalent to an .\"O .BR ioctl (2) .\"O call with the .\"O .B FIOSETOWN .\"O or .\"O .B SIOCSPGRP .\"O argument. .B F_SETOWN を使用することは .B FIOSETOWN または .B SIOCSPGRP の引数で .BR ioctl (2) を使用することと等価である。 .PP .\"O When the network signals an error condition to the protocol module (e.g., .\"O using a ICMP message for IP) the pending error flag is set for the socket. .\"O The next operation on this socket will return the error code of the pending .\"O error. .\"O For some protocols it is possible to enable a per-socket error queue .\"O to retrieve detailed information about the error; see .\"O .B IP_RECVERR .\"O in .\"O .BR ip (7). ネットワークがプロトコル・モジュールにエラー状態を伝えた場合 (例えば、IP の ICMP メッセージを使用して)には、ソケットの ペンディング・エラー・フラグが設定される。次にこのソケットを操作した 時にペンディングされていたエラー・コードが返される。プロトコルによっては エラーについてのより詳しい情報を受け取るためにソケットごとのエラー・キューを 受け取ることが可能である。 .BR ip (7) の .B IP_RECVERR を参照すること。 .PP .\"O The operation of sockets is controlled by socket level .\"O .IR options . .\"O These options are defined in .\"O .IR . .\"O The functions .\"O .BR setsockopt (2) .\"O and .\"O .BR getsockopt (2) .\"O are used to set and get options, respectively. ソケットの操作はソケット・レベル .I options によって制御される。 これらのオプションは .I に定義されている。 .BR setsockopt (2) と .BR getsockopt (2) 関数はそれぞれオプションの設定と取得を行なう。 .\"O .SH "RETURN VALUE" .SH 返り値 .\"O On success, a file descriptor for the new socket is returned. .\"O On error, \-1 is returned, and .\"O .I errno .\"O is set appropriately. 成功した場合、新しいソケットのファイル・ディスクリプターを返す。 エラーが発生した場合は \-1 を返し、 .I errno を適切に設定する。 .\"O .SH ERRORS .SH エラー .TP .B EACCES .\"O Permission to create a socket of the specified type and/or protocol .\"O is denied. 指定されたタイプまたはプロトコルのソケットを作成する許可が与えられていない。 .TP .B EAFNOSUPPORT .\"O The implementation does not support the specified address family. 指定されたアドレスファミリーがサポートされていない。 .TP .B EINVAL .\"O Unknown protocol, or protocol family not available. 知らないプロトコル、または利用できないプロトコル・ファミリである。 .TP .B EINVAL .\" Since Linux 2.6.27 .\"O Invalid flags in .\"O .IR type . .I type に無効なフラグが指定されている。 .TP .B EMFILE .\"O Process file table overflow. プロセスのファイルテーブルが溢れている。 .TP .B ENFILE .\"O The system limit on the total number of open files has been reached. オープンされたファイルの総数がシステム上限に達している。 .TP .\"O .BR ENOBUFS " or " ENOMEM .BR ENOBUFS " または " ENOMEM .\"O Insufficient memory is available. .\"O The socket cannot be .\"O created until sufficient resources are freed. 十分なメモリがない。十分な資源が解放されるまではソケットを 作成することはできない。 .TP .B EPROTONOSUPPORT .\"O The protocol type or the specified protocol is not .\"O supported within this domain. このドメインでは指定されたプロトコルまたはプロトコル・タイプが サポートされていない。 .PP .\"O Other errors may be generated by the underlying protocol modules. 下位のプロトコル・モジュールから他のエラーが生成されるかもしれない。 .\"O .SH "CONFORMING TO" .SH 準拠 4.4BSD, POSIX.1-2001. .\"O The .\"O .B SOCK_NONBLOCK .\"O and .\"O .B SOCK_CLOEXEC .\"O flags are Linux-specific. フラグ .BR SOCK_NONBLOCK , .B SOCK_CLOEXEC は Linux 固有である。 .\"O .BR socket () .\"O appeared in 4.2BSD. .\"O It is generally portable to/from .\"O non-BSD systems supporting clones of the BSD socket layer (including .\"O System V variants). .BR socket () は 4.2BSD で登場した。一般に、(System V の変種を含めて) BSD のソケット層の互換性をサポートしている BSD 以外のシステムへの、 または、BSD 以外のシステムからの移植ができる。 .\"O .SH NOTES .SH 注意 .\"O POSIX.1-2001 does not require the inclusion of .\"O .IR , .\"O and this header file is not required on Linux. .\"O However, some historical (BSD) implementations required this header .\"O file, and portable applications are probably wise to include it. POSIX.1-2001 では .I のインクルードは必須とされておらず、 Linux ではこのヘッダファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。 .\"O The manifest constants used under 4.x BSD for protocol families .\"O are .\"O .BR PF_UNIX , .\"O .BR PF_INET , .\"O etc., while .\"O .B AF_UNIX .\"O etc. are used for address .\"O families. .\"O However, already the BSD man page promises: "The protocol .\"O family generally is the same as the address family", and subsequent .\"O standards use AF_* everywhere. 4.x BSD において定数を使用する場合、プロトコル・ファミリーには .BR PF_UNIX , .B PF_INET 等を使用している。一方でアドレス・ファミリーには .B AF_UNIX 等が使用されている。しかしながら BSD のマニュアルでは 「一般にプロトコル・ファミリーはアドレス・ファミリーと同じものである。」 と保証している。それでそれ以外の規格では全ての場所で AF_* が使用されている。 .\"O .SH EXAMPLE .SH 例 .\"O An example of the use of .\"O .BR socket () .\"O is shown in .\"O .BR getaddrinfo (3). .BR socket () の利用例が .BR getaddrinfo (3) に記載されている。 .\"O .SH "SEE ALSO" .SH 関連項目 .BR accept (2), .BR bind (2), .BR connect (2), .BR fcntl (2), .BR getpeername (2), .BR getsockname (2), .BR getsockopt (2), .BR ioctl (2), .BR listen (2), .BR read (2), .BR recv (2), .BR select (2), .BR send (2), .BR shutdown (2), .BR socketpair (2), .BR write (2), .BR getprotoent (3), .BR ip (7), .BR socket (7), .BR tcp (7), .BR udp (7), .BR unix (7) .PP .\"O \(lqAn Introductory 4.3BSD Interprocess Communication Tutorial\(rq .\"O is reprinted in .\"O .I UNIX Programmer's Supplementary Documents Volume 1. \(lqAn Introductory 4.3BSD Interprocess Communication Tutorial\(rq は .I UNIX Programmer's Supplementary Documents Volume 1 として再版された。 .PP .\"O \(lqBSD Interprocess Communication Tutorial\(rq .\"O is reprinted in .\"O .I UNIX Programmer's Supplementary Documents Volume 1. \(lqBSD Interprocess Communication Tutorial\(rq は .I UNIX Programmer's Supplementary Documents Volume 1 として再版された。