.\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) .\" Portions extracted from /usr/include/sys/socket.h, which does not have .\" any authorship information in it. It is probably available under the GPL. .\" .\" %%%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 .\" .\" .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page: .\" .\" Copyright (c) 1983 The Regents of the University of California. .\" All rights reserved. .\" .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB) .\" 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. .\" %%%LICENSE_END .\" .\" Modified 1997-01-31 by Eric S. Raymond .\" Modified 1998, 1999 by Andi Kleen .\" Modified 2004-06-23 by Michael Kerrisk .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara .\" all rights reserved. .\" Translated 1996-06-25, Yosiaki Yanagihara .\" Updated & Modified 1997-11-06, HANATAKA Shinya .\" Updated & Modified 1999-08-14, HANATAKA Shinya .\" Updated & Modified 2000-01-13, Kentaro Shirakata .\" Updated & Modified 2005-02-23, Akihiro MOTOKI .\" Updated & Modified 2005-10-05, Akihiro MOTOKI .\" .TH CONNECT 2 2014\-09\-06 Linux "Linux Programmer's Manual" .SH 名前 connect \- ソケットの接続を行う .SH 書式 .nf \fB#include \fP /* 「注意」参照 */ .br \fB#include \fP .sp \fBint connect(int \fP\fIsockfd\fP\fB, const struct sockaddr *\fP\fIaddr\fP\fB,\fP \fB socklen_t \fP\fIaddrlen\fP\fB);\fP .fi .SH 説明 \fBconnect\fP() システムコールは、ファイルディスクリプター \fIsockfd\fP が参照しているソケットを \fIaddr\fP で指定されたアドレスに接続する。 \fIaddrlen\fP 引き数は \fIaddr\fP の大きさを示す。 \fIaddr\fP のアドレスのフォーマットはソケット \fIsockfd\fP のアドレス空間により異なる。 さらなる詳細は \fBsocket\fP(2) を参照のこと。 ソケット \fIsockfd\fP が \fBSOCK_DGRAM\fP 型であれば、 \fIaddr\fP は、デフォルトのデータグラムの送信先のアドレスであり、 データグラムを受信する唯一のアドレスを示すに過ぎない。 ソケットが \fBSOCK_STREAM\fP 型もしくは \fBSOCK_SEQPACKET\fP 型であれば、このシステムコールは \fIaddr\fP で指定されたアドレスに結び付けられたソケットに対する接続の 作成を試みる。 .PP 一般的に、接続指向 (connection\-oriented) プロトコルでは一度だけ \fBconnect\fP() が成功する。 コネクションレス (connectionless) プロトコルでは対応を変更するために何度も \fBconnect\fP() を使用できる。 非接続ソケットは \fIsockaddr\fP の \fIsa_family\fP メンバに \fBAF_UNSPEC\fP を設定することで、接続アドレスの対応を解消することができる (\fBAF_UNSPEC\fP はカーネル 2.2 以降の Linux でサポート)。 .SH 返り値 接続または対応づけに成功するとゼロを返す。 失敗すると \-1 を返し、 \fIerrno\fP に適切な値を設定する。 .SH エラー 以下は一般的なソケットについてのエラーである。他にドメイン特有のエラー が発生する可能性がある。 .TP \fBEACCES\fP UNIX ドメインソケットはパス名で識別される。 ソケットファイルへの書き込み許可がなかったか、パス名へ 到達するまでのディレクトリのいずれかに対する検索許可がなかった。 (\fBpath_resolution\fP(7) も参照のこと) .TP \fBEACCES\fP, \fBEPERM\fP ソケットのブロードキャストフラグが有効になっていないのに ユーザーがブロードキャストへ接続を試みた。または、ローカルのファイアウォールの 規則により接続の要求が失敗した。 .TP \fBEADDRINUSE\fP ローカルアドレスが既に使用されている。 .TP \fBEADDRNOTAVAIL\fP (インターネットドメインソケットの場合) \fIsockfd\fP が参照するソケットがそれ以前にアドレスにバインドされておらず、 そのソケットに一時ポートをバインドしようとした際に、 一時ポートとして使用する範囲のポート番号がすべて使用中であった。 \fBip\fP(7) の \fI/proc/sys/net/ipv4/ip_local_port_range\fP の議論を参照のこと。 .TP \fBEAFNOSUPPORT\fP 渡されたアドレスの \fIsa_family\fP フィールドが正しいアドレスファミリーではない。 .TP \fBEAGAIN\fP ルーティングキャッシュにエントリーが十分にない。 .TP \fBEALREADY\fP ソケットが非停止 (nonblocking) に設定されており、 前の接続が完了していない。 .TP \fBEBADF\fP ファイルディスクリプターがディスクリプターテーブルの 有効なインデックスではない。 .TP \fBECONNREFUSED\fP リモートアドレスで接続を待っているプログラムがない。 .TP \fBEFAULT\fP ソケット構造体のアドレスがユーザーのアドレス空間外にある。 .TP \fBEINPROGRESS\fP ソケットが非停止 (nonblocking) に設定されていて、接続をすぐに 完了することができない。その場合、 \fBselect\fP(2) や \fBpoll\fP(2) を使ってそのソケットが書き込み可能になるのを待つことで、 接続の完了を知ることができる。 \fBselect\fP(2) で書き込み可能になった後に、 \fBgetsockopt\fP(2) を使って \fBSOL_SOCKET\fP レベルで \fBSO_ERROR\fP オプションを読み出すこ とにより、 \fBconnect\fP() が成功したか、失敗したかを判断できる。 成功の場合 \fBSO_ERROR\fP が 0 であり、 失敗の場合 \fBSO_ERROR\fP がここのリストにあるいずれかのエラーコードであり、 それにより失敗の原因が分かる。 .TP \fBEINTR\fP .\" For TCP, the connection will complete asynchronously. .\" See http://lkml.org/lkml/2005/7/12/254 捕捉されたシグナルによりシステムコールが中断された。 \fBsignal\fP(7) 参照。 .TP \fBEISCONN\fP ソケットは既に接続 (connect) されている。 .TP \fBENETUNREACH\fP 到達できないネットワークである。 .TP \fBENOTSOCK\fP ファイルディスクリプターがソケットと関連付けられていない。 .TP \fBEPROTOTYPE\fP ソケットタイプが要求された通信プロトコルではサポートされていない。 このエラーは、 例えば UNIX ドメインデータグラムソケットをストリームソケットに接続しようとした場合などに起こり得る。 .TP \fBETIMEDOUT\fP 接続を試みている途中で時間切れ (timeout) になった。サーバーが混雑していて 新たな接続を受け入れられないのかもしれない。 IP ソケットでは、 syncookie がサーバーで有効になっている場合、 タイムアウトが非常に長くなる場合があるので注意すること。 .SH 準拠 .\" SVr4 documents the additional .\" general error codes .\" .BR EADDRNOTAVAIL , .\" .BR EINVAL , .\" .BR EAFNOSUPPORT , .\" .BR EALREADY , .\" .BR EINTR , .\" .BR EPROTOTYPE , .\" and .\" .BR ENOSR . .\" It also .\" documents many additional error conditions not described here. SVr4, 4.4BSD, (\fBconnect\fP() 関数は 4.2BSD で最初に登場した), POSIX.1\-2001. .SH 注意 POSIX.1\-2001 では \fI\fP のインクルードは必須とされておらず、 Linux ではこのヘッダーファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダーファイルが 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。 \fBconnect\fP() の三番目の引き数は 4.x BSD や libc4, libc5 と同様に実際には \fIint\fP である。 POSIX では紆余曲折を経て現在の \fIsocklen_t\fP になっており、 glibc でも \fIsocklen_t\fP を使っている。 \fBaccept\fP(2) も参照のこと。 \fBconnect\fP() が失敗した場合、そのソケットの状態は不定だと考えること。 移植性を考慮したアプリケーションでは、そのソケットをクローズし、再接続用に新しいソケットを作成すべきである。 .SH 例 \fBconnect\fP() の利用例が \fBgetaddrinfo\fP(3) に記載されている。 .SH 関連項目 \fBaccept\fP(2), \fBbind\fP(2), \fBgetsockname\fP(2), \fBlisten\fP(2), \fBsocket\fP(2), \fBpath_resolution\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。