Provided by: manpages-ja-dev_0.5.0.0.20221215+dfsg-1_all 

名前
connect - ソケットの接続を行う
書式
#include <sys/types.h> /* 「注意」参照 */
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
説明
connect() システムコールは、ファイルディスクリプター sockfd が参照しているソケットを addr で指定されたア
ドレスに接続する。 addrlen 引数は addr の大きさを示す。 addr のアドレスのフォーマットはソケット sockfd の
アドレス空間により異なる。 さらなる詳細は socket(2) を参照のこと。
ソケット sockfd が SOCK_DGRAM 型であれば、 addr は、デフォルトのデータグラムの送信先のアドレスであり、
データグラムを受信する唯一のアドレスを示すに過ぎない。 ソケットが SOCK_STREAM 型もしくは SOCK_SEQPACKET
型であれば、このシステムコールは addr で指定されたアドレスに結び付けられたソケットに対する接続の 作成を試
みる。
Some protocol sockets (e.g., UNIX domain stream sockets) may successfully connect() only once.
Some protocol sockets (e.g., datagram sockets in the UNIX and Internet domains) may use connect()
multiple times to change their association.
Some protocol sockets (e.g., TCP sockets as well as datagram sockets in the UNIX and Internet domains)
may dissolve the association by connecting to an address with the sa_family member of sockaddr set to
AF_UNSPEC; thereafter, the socket can be connected to another address. (AF_UNSPEC is supported on Linux
since kernel 2.2.)
返り値
接続または対応づけに成功するとゼロを返す。 失敗すると -1 を返し、 errno に適切な値を設定する。
エラー
以下は一般的なソケットについてのエラーである。他にドメイン特有のエラー が発生する可能性がある。
EACCES UNIX ドメインソケットはパス名で識別される。 ソケットファイルへの書き込み許可がなかったか、パス名へ
到達するまでのディレクトリのいずれかに対する検索許可がなかった。 (path_resolution(7) も参照のこ
と)
EACCES, EPERM
ソケットのブロードキャストフラグが有効になっていないのに ユーザーがブロードキャストへ接続を試み
た。または、ローカルのファイアウォールの 規則により接続の要求が失敗した。
EACCES can also be returned if an SELinux policy denied a connection (for example, if there is a
policy saying that an HTTP proxy can only connect to ports associated with HTTP servers, and the
proxy tries to connect to a different port). dd
EADDRINUSE
ローカルアドレスが既に使用されている。
EADDRNOTAVAIL
(インターネットドメインソケットの場合) sockfd が参照するソケットがそれ以前にアドレスにバインドされ
ておらず、 そのソケットに一時ポートをバインドしようとした際に、 一時ポートとして使用する範囲のポー
ト番号がすべて使用中であった。 ip(7) の /proc/sys/net/ipv4/ip_local_port_range の議論を参照のこ
と。
EAFNOSUPPORT
渡されたアドレスの sa_family フィールドが正しいアドレスファミリーではない。
EAGAIN For nonblocking UNIX domain sockets, the socket is nonblocking, and the connection cannot be
completed immediately. For other socket families, there are insufficient entries in the routing
cache.
EALREADY
ソケットが非停止 (nonblocking) に設定されており、 前の接続が完了していない。
EBADF sockfd が有効なオープンされたファイルディスクリプターでない。
ECONNREFUSED
A connect() on a stream socket found no one listening on the remote address.
EFAULT ソケット構造体のアドレスがユーザーのアドレス空間外にある。
EINPROGRESS
The socket is nonblocking and the connection cannot be completed immediately. (UNIX domain
sockets failed with EAGAIN instead.) It is possible to select(2) or poll(2) for completion by
selecting the socket for writing. After select(2) indicates writability, use getsockopt(2) to
read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed
successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed
here, explaining the reason for the failure).
EINTR 捕捉されたシグナルによりシステムコールが中断された。 signal(7) 参照。
EISCONN
ソケットは既に接続 (connect) されている。
ENETUNREACH
到達できないネットワークである。
ENOTSOCK
ファイルディスクリプター sockfd がソケットを参照していない。
EPROTOTYPE
ソケットタイプが要求された通信プロトコルではサポートされていない。 このエラーは、 例えば UNIX ドメ
インデータグラムソケットをストリームソケットに接続しようとした場合などに起こり得る。
ETIMEDOUT
接続を試みている途中で時間切れ (timeout) になった。サーバーが混雑していて 新たな接続を受け入れられ
ないのかもしれない。 IP ソケットでは、 syncookie がサーバーで有効になっている場合、 タイムアウトが
非常に長くなる場合があるので注意すること。
準拠
accept(): POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD, (connect() は 4.2BSD で初めて実装された).
注意
POSIX.1 では <sys/types.h> のインクルードは必須とされておらず、 Linux ではこのヘッダーファイルは必要では
ない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダーファイルが 必要であり、移植性が必要なア
プリケーションではこのファイルを インクルードするのが賢明であろう。
For background on the socklen_t type, see accept(2).
connect() が失敗した場合、そのソケットの状態は不定だと考えること。 移植性を考慮したアプリケーションで
は、そのソケットをクローズし、再接続用に新しいソケットを作成すべきである。
例
connect() の利用例が getaddrinfo(3) に記載されている。
関連項目
accept(2), bind(2), getsockname(2), listen(2), socket(2), path_resolution(7), selinux(8)
この文書について
この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告
に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。
Linux 2020-04-11 CONNECT(2)