connect - initiate a connection on a socket
#include <sys/types.h> #include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
The file descriptor sockfd must refer to a socket. If the socket is of type SOCK_DGRAM then the serv_addr address is the address to which datagrams are sent by default, and the only address from which datagrams are received. If the socket is of type SOCK_STREAM or SOCK_SEQPACKET, this call attempts to make a connection to another socket. The other socket is specified by serv_addr, which is an address (of length addrlen) in the communications space of the socket. Each communications space interprets the serv_addr parameter in its own way.
Generally, connection-based protocol sockets may successfully connect only once; connectionless protocol sockets may use connect multiple times to change their association. Connectionless sockets may dissolve the association by connecting to an address with the sa_family member of sockaddr set to AF_UNSPEC.
If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.
The following are general socket errors only. There may be other domain-specific error codes.
SVr4, 4.4BSD (the connect function first appeared in BSD 4.2). SVr4 documents the additional general error codes EADDRNOTAVAIL, EINVAL, EALREADY, EINTR, EPROTOTYPE, and ENOSR?. It also documents many additional error conditions not described here.
The third argument of connect is in reality an int (and this is what BSD 4.* and libc4 and libc5 have). Some POSIX confusion resulted in the present socklen_t. The draft standard has not been adopted yet, but glibc2 already follows it and also has socklen_t. See also accept(2).
Unconnecting a socket by calling connect with a AF_UNSPEC address is not yet implemented.
accept(2), bind(2), listen(2), socket(2), getsockname(2), CategorySocketSysCalls
36 pages link to connect(2):