CONNECT(2) | System Calls Manual | CONNECT(2) |
connect
— initiate
a connection on a socket
#include
<sys/types.h>
#include <sys/socket.h>
int
connect
(int socket,
const struct sockaddr *address,
socklen_t address_len);
The parameter socket is a socket. If it is
of type SOCK_DGRAM
, this call specifies the peer
with which the socket is to be associated; this address is that to which
datagrams are to be sent, and the only address from which datagrams are to
be received. If the socket is of type SOCK_STREAM
,
this call attempts to make a connection to another socket. The other socket
is specified by address, which is an address in the
communications space of the socket.
Each communications space interprets the
address parameter in its own way. Generally, stream
sockets may successfully
connect
()
only once; datagram sockets may use connect
()
multiple times to change their association. Datagram sockets may dissolve
the association by calling
disconnectx(2), or by connecting
to an invalid address, such as a null address or an address with the address
family set to AF_UNSPEC
(the error
EAFNOSUPPORT
will be harmlessly returned).
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to indicate the error.
The connect
() system call will fail
if:
EACCES
]SO_BROADCAST
is not set.EADDRINUSE
]EADDRNOTAVAIL
]EAFNOSUPPORT
]EALREADY
]EBADF
]ECONNREFUSED
]EFAULT
]EHOSTUNREACH
]EINPROGRESS
]EINTR
]EINVAL
]EISCONN
]ENETDOWN
]ENETUNREACH
]ENOBUFS
]ENOTSOCK
]EOPNOTSUPP
]EPROTOTYPE
]ETIMEDOUT
]ECONNRESET
]The following errors are specific to connecting names in the UNIX domain. These errors may not apply in future versions of the UNIX IPC domain.
EACCES
]EACCES
]EIO
]ELOOP
]ENAMETOOLONG
]{NAME_MAX}
characters, or an entire path name exceeded
{PATH_MAX}
characters.ENOENT
]ENOTDIR
]#include
<sys/types.h>
#include
<sys/socket.h>
The include file
<sys/types.h>
is
necessary.
accept(2), connectx(2), disconnectx(2), getsockname(2), select(2), socket(2), compat(5)
The connect
() function call appeared in
4.2BSD.
March 18, 2015 | BSD 4.2 |