CONNECT(2) | Linux Programmer's Manual | CONNECT(2) |
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 に適切な値を設定する。
以下は一般的なソケットについてのエラーである。他にドメイン特有のエラー が発生する可能性がある。
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/ に書かれている。
2020-04-11 | Linux |