initiate a connection on a socket
#include <sys/types.h>
#include <sys/socket.h>
int connect( int s,
const struct sockaddr *name,
int namelen );
This function establishes the connection according to the socket type
specified by s:
- SOCK_DGRAM
- This call specifies the peer that the
socket is to be associated with; this address is the one
that datagrams are to be sent to, and the only one that
datagrams are to be received from.
- SOCK_STREAM
- This call attempts
to make a connection to another socket. The other socket is
specified by name, which is an address in the
communications space of that socket. Each communications
space interprets name in its own way.
Stream sockets may successfully do a
connect() only once, whereas datagram sockets may
use connect() multiple times to change their
association. Datagram sockets may dissolve the association
by connecting to an invalid address, such as a null address.
- 0
- Success.
- -1
- An error occurred; errno is set.
If an error occurred, errno could contain one of the
following:
- EADDRINUSE
- The address is already in use.
- EADDRNOTAVAIL
- The specified address isn't available on this machine.
- EAFNOSUPPORT
- Addresses in the specified address family cannot be used
with this socket.
- EALREADY
- The socket is nonblocking; a previous connection
attempt hasn't yet been completed.
- EBADF
- s isn't a valid descriptor.
- ECONNREFUSED
- The attempt to connect was forcefully rejected.
- EFAULT
- The name parameter specifies an area outside
the process address space.
- EHOSTUNREACH
- No route to host; the host system can't be reached.
- EINPROGRESS
- The socket is nonblocking; the connection can't be
completed immediately. It's possible to do a
select() for completion by selecting the socket
for writing.
- EISCONN
- The socket is already connected.
- ENETUNREACH
- The network isn't reachable from this host.
- ETIMEDOUT
- The attempt to establish a connection timed out; no connection
was made.
POSIX 1003.1g (draft)
Safety: | |
Interrupt handler |
No |
Signal handler |
No |
Thread |
Yes |
ICMP,
IP,
TCP, and
UDP protocols,
accept(),
bind(),
getsockname(),
socket()
select()
in the C Library Reference