.TH connect() "" "" "Sockets Function (libsocket)"
.PC "Connect to a socket"
.B "#include <sys/types.h>"
.B "#include <sys/socket.h>"
\fBint connect(\fIsocket\^\fB, \fIname\^\fB, \fInamelen\^\fB)\fR
\fBint \fIsocket\^\fB, \fInamelen\^\fB; struct sockaddr *\fIname\^\fB;\fR
.PP
The function
.B connect()
establishes a connection for a socket.
.PP
.I socket
is a file identifier that describes a socket possessed by the current process.
It must have been returned by a call to
.BR socket() .
If it is of type
.BR SOCK_DGRAM ,
.B 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
.BR SOCK_STREAM ,
.B connect()
attempts to connect it with another socket.
The other socket is identified by
.IR 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
.BR bind() .
.I namelen
gives the length, in bytes, of the file name to which
.I name
points.
.\"Each communications space interprets the
.\".B name
.\"parameter in its own way.
.PP
As a rule, a socket of type
.B SOCK_STREAM
can successfully connect only once; however, those of type
.B SOCK_DGRAM
sockets can call
.B connect()
multiple times to change their association.
Datagram sockets can dissolve the association
by connecting to an invalid address, such as a null address.
.PP
If the connection or binding succeeds,
.B connect()
returns zero.
If an error occurs, it returns \-1 and sets
.B errno
to an appropriate value.
The following lists the errors that can occur, by the value to which
.B connect()
sets
.BR errno :
.IP \fBEBADF\fR
.I socket
is somehow invalid.
.IP \fBENOTSOCK\fR
.I socket
references a file, not a socket.
.IP \fBEADDRNOTAVAIL\fR
The address is not available on this machine.
.IP \fBEAFNOSUPPORT\fR
Addresses in the specified address family cannot be used with
.IR socket .
.IP \fBEISCONN\fR
.I socket
is already connected to an address or socket.
.IP \fBETIMEDOUT\fR
.B connect()
timed out without establishing a connection.
.IP \fBECONNREFUSED\fR
The attempt to connect was forcefully rejected.
.IP \fBENETUNREACH\fR
The network is not reachable from this host.
.IP \fBEADDRINUSE\fR
The address is already in use.
.IP \fBEFAULT\fR
.I name
gives an illegal address.
.IP \fBEINPROGRESS\fR
.I socket
is non-blocking yet the connection cannot be completed immediately.
.\"It is possible to
.\".B select()
.\"for completion by selecting the socket for writing.
.IP \fBEALREADY\fR
The socket is non-blocking and a previous call to
.B connect()
has not yet been completed.
.\".PP
.\"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.
.\".IP ENOTDIR
.\"A component of the path prefix is not a directory.
.\".IP EINVAL
.\"The pathname contains a character with the high-order bit set.
.\".IP ENAMETOOLONG
.\"A component of a pathname exceeded 255 characters,
.\"or an entire path name exceeded 1023 characters.
.\".IP ENOENT
.\"The named socket does not exist.
.\".IP EACCES
.\"Search permission is denied for a component of the path prefix.
.\".IP EACCES
.\"Write access to the named socket is denied.
.\".IP ELOOP
.\"Too many symbolic links were encountered in translating the pathname.
.SH Example
For an example of this function, see the Lexicon entry for
.BR libsocket .
.SH "See Also"
.Xr "accept()," accept
.Xr "getsockname()," getsockna
.Xr "libsocket," libsocket
.Xr "select()," select
.Xr "socket()" socket
