connect()

Initiate a connection on a socket

Synopsis:

#include <sys/types.h>
#include <sys/socket.h>

int connect( int s,
             const struct sockaddr * name,
             socklen_t namelen );

Arguments:

s
The descriptor of the socket on which to initiate the connection.
name
The name of the socket to connect to for a SOCK_STREAM connection.
namelen
The length of the name, in bytes.

Library:

libsocket

Use the -l socket option to qcc to link against this library.

Description:

The connect() function establishes the connection according to the socket type specified by s:

SOCK_DGRAM
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 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.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

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
Invalid descriptor s.
ECONNABORTED
The connect() was terminated under software control.
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.
Note: Protocols such as TCP do not allow further connection requests on a socket after an ECONNREFUSED error. In such a situation, the socket must be closed and a new one created before a subsequent attempt for connection is made.

Classification:

POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes