COHERENT manpages

This page displays the COHERENT manpage for connect() [Connect to a socket].

List of available manpages
Index


connect() -- Sockets Function (libsocket)

Connect to a socket
#include <sys/types.h>
#include <sys/socket.h>
int connect(socket, name, namelen)
int socket, namelen; struct sockaddr *name;

The function connect() establishes a connection for a socket.

socket  is a  file  identifier that  describes  a socket  possessed by  the
current process.  It  must have been returned by a  call to socket(). If it
is of  type SOCK_DGRAM, connect() specifies the peer  with which the socket
is to be connected; this address is that to which datagrams are to be sent,
and the only address from which datagrams are to be received.  If, however,
it is  of type SOCK_STREAM,  connect() attempts to connect  it with another
socket.  The other  socket is identified by name, which  points to the full
path name of the file to  which the other socket is bound.  This connection
must have been established by a  call to function bind(). namelen gives the
length, in bytes, of the file name to which name points.

As a rule, a socket of type SOCK_STREAM can successfully connect only once;
however, those of type SOCK_DGRAM sockets can call connect() multiple times
to change their association.  Datagram sockets can dissolve the association
by connecting to an invalid address, such as a null address.

If the connection or binding succeeds, connect() returns zero.  If an error
occurs,  it  returns  -1 and  sets  errno  to  an  appropriate value.   The
following lists the errors that can  occur, by the value to which connect()
sets errno:

EBADF
     socket is somehow invalid.

ENOTSOCK
     socket references a file, not a socket.

EADDRNOTAVAIL
     The address is not available on this machine.

EAFNOSUPPORT
     Addresses in the specified address family cannot be used with socket.

EISCONN
     socket is already connected to an address or socket.

ETIMEDOUT
     connect() timed out without establishing a connection.

ECONNREFUSED
     The attempt to connect was forcefully rejected.

ENETUNREACH
     The network is not reachable from this host.

EADDRINUSE
     The address is already in use.

EFAULT
     name gives an illegal address.

EINPROGRESS
     socket  is  non-blocking   yet  the  connection  cannot  be  completed
     immediately.

EALREADY
     The socket  is non-blocking and  a previous call to  connect() has not
     yet been completed.

Example

For an example of this function, see the Lexicon entry for libsocket.

See Also

accept(),
getsockname(),
libsocket,
select(),
socket()