'\"macro stdmacro
.if n .pH g3n.connect @(#)connect	40.10 of 1/29/90
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} connect 3N "" "\&"
.if \nX=1 .ds x} connect 3N ""
.if \nX=2 .ds x} connect 3N "" "\&"
.if \nX=3 .ds x} connect "" "" "\&"
.TH \*(x}
.SH NAME
\f4connect\fP \- initiate a connection on a socket
.SH SYNOPSIS
.nf
.ft 4
#include <sys/types.h>
#include <sys/socket.h>
.LP
.ft 4
connect(s, name, namelen)
int s;
struct sockaddr *name;
int namelen;
.fi
.SH DESCRIPTION
.LP
The parameter
.I s
is a socket.
If it is of type \f4SOCK_DGRAM\f1, \f4connect()\fP specifies the peer
with which the socket is to be associated; this address is the address
to which datagrams are to be sent if a receiver is not explicitly
designated; it is the only address from which datagrams are to be received.
If the socket \f2s\f1 is of type \f4SOCK_STREAM\f1, \f4connect()\fP
attempts to make a connection to another socket.
The other socket is specified by \f2name\f1\h'-.03c'.
.I name
is an address in the communications space of the socket.
Each communications space interprets the
.I name
parameter in its own way. If
.I s
is not bound, then it will be bound to an address selected by the
underlying transport provider.
Generally, stream sockets may successfully
\f4connect()\fP
only once; datagram sockets may use
\f4connect()\fP
multiple times to change their association.
Datagram sockets may dissolve the association
by connecting to a null address.
.SH "RETURN VALUE"
If the connection or binding succeeds, then
0 is returned.
Otherwise a \-1 is returned and a more specific error
code is stored in
\f4errno\fP.
.SH ERRORS
The call fails if:
.TP 20
\f4EBADF\f1
.I s
is not a valid descriptor.
.TP
\f4ENOTSOCK\f1
.I s
is a descriptor for a file, not a socket.
.TP
\f4EINVAL\f1
.I namelen
is not the size of a valid address for the specified address family.
.TP
\f4EADDRNOTAVAIL\f1
The specified address is not available on the remote machine.
.TP
\f4EAFNOSUPPORT\f1
Addresses in the specified address family cannot be used with this socket.
.TP
\f4EISCONN\f1
The socket is already connected.
.TP
\f4ETIMEDOUT\f1
Connection establishment timed out without establishing a connection.
.TP
\f4ECONNREFUSED\f1
The attempt to connect was forcefully rejected.
The calling program should
\f4close\fP(2)
the socket descriptor, and issue another
\f4socket\fP(3N)
call to obtain a new descriptor before attempting another
\f4connect()\fP
call.
.TP
\f4ENETUNREACH\f1
The network is not reachable from this host.
.TP
\f4EADDRINUSE\f1
The address is already in use.
.TP
\f4EINPROGRESS\f1
The socket is non-blocking and the connection cannot be completed
immediately.
It is possible to \f4select\fP(3N) for completion by selecting the
socket for writing.
However, this is only possible if the socket \s-1STREAMS\s+1 module
is the topmost module on the protocol stack with a write service
procedure.
This will be the normal case.
.TP
\f4EALREADY\f1
The socket is non-blocking
and a previous connection attempt
has not yet been completed.
.TP
\f4EINTR\f1
The connection attempt was interrupted before
any data arrived by the delivery of a signal.
.TP
\f4ENOTSOCK\f1
The file referred to by
.I name
is not a socket.
.TP
\f4EPROTOTYPE\f1
The file referred to by
.I name
is a socket of a type other than type
.I s
(for example,
.I s
is a
\f4SOCK_DGRAM\fP
socket, while
.I name
refers to a
\f4SOCK_STREAM\fP
socket).
.TP
\f4ENOSR\f1
There were insufficient
\f4STREAMS\f1
resources available to complete
the operation.
.LP
The following errors are specific to connecting names in the
.SM UNIX
domain. 
These errors may not apply in future versions of the
.SM UNIX IPC
domain.
.TP 20
\f4ENOTDIR\f1
A component of the path prefix of the pathname in
.I name
is not a directory.
.TP
\f4ENOENT\f1
A component of the path prefix of the pathname in
.I name
does not exist.
.TP
\f4ENOENT\f1
The socket referred to by the pathname in
.I name
does not exist.
.TP
\f4EACCES\f1
Search permission is denied for a component of the path prefix of the
pathname in
.IR name .
.TP
\f4ELOOP\f1
Too many symbolic links were encountered in translating the pathname in
.IR name .
.TP
\f4EIO\f1
An I/O error occurred while reading from or writing to the file system.
.SH SEE ALSO
\f4accept\fP(3N),
\f4connect\fP(3N),
\f4getsockname\fP(3N),
\f4socket\fP(3N).
