.TH bind() "" "" "Sockets Function (libsocket)"
.PC "Bind a name to a socket"
.B "#include <sys/types.h>"
.B "#include <sys/socket.h>"
\fBint bind (\fIsocket\^\fB, \fIname\^\fB, \fInamelen\^\fB)\fR
\fBint \fIsocket\^\fB, \fInamelen\^\fB; struct sockaddr *\fIname\^\fB;\fR
.PP
Function
.B bind()
binds a name to an unnamed socket.
.PP
When function
.BR socket()
creates a socket, that socket exists but has no name.
.B bind()
creates a special file, assigns it a name, and binds that file to a socket.
Thereafter, the socket can be accessed by reading or writing the file.
.PP
.I socket
is a file descriptor that identifies the socket in question.
It must have been returned by a call to
.BR socket() .
.I name
points to the full path name of the file to which
.I socket
is to be bound.
The calling process must unlink
.I name
when it no longer needs it.
.I namelen
gives the number of bytes in the path name name to which
.I name
points.
Under \*(CO, no element of
.I name
can exceed 14 characters (not including separating `/' characters).
.PP
If all goes well,
.B bind()
returns zero.
If something goes wrong,
.B bind()
returns \-1 and sets
.B errno
to an appropriate value.
The following lists the errors that can occur, by the value to which
.B bind()
sets
.BR errno :
.IP \fBEBADF\fR
.I socket
is somehow not a valid descriptor.
.IP \fBENOTSOCK\fR
.I socket
is not a socket.
.IP \fBEADDRNOTAVAIL\fR
.I name
is not available from the local machine.
.IP \fBEADDRINUSE\fR
.I name
is already bound to another socket.
.IP \fBEINVAL\fR
.I socket
is already bound to a name.
.IP \fBEACCES\fR
The memory to which
.I name
points is protected and the user lacks permission to access it.
.IP \fBEFAULT\fR
.I name
points to an illegal address.
.IP \fBENOTDIR\fR
The path name to which
.I name
points contains an element that is not a directory.
.IP \fBEINVAL\fR
The path name to which
.I name
points contains a character with the high-order bit set.
.\".IP \fBENAMETOOLONG\fR
.\"An element of the path name to which
.\".I name
.\"points exceeds
.\"255 characters,
.\"or the entire path name exceeds 1,023 characters.
.IP \fBENOENT\fR
A prefix component of the path name does not exist.
.\".IP \fBELOOP\fR
.\"Too many symbolic links were encountered in translating the path name.
.IP \fBEIO\fR
An I/O error occurred while creating the directory entry for
.I name
or allocating its inode.
.IP \fBEROFS\fR
.I name
would reside on a read-only file system.
.IP \fBEISDIR\fR
.I name
points to an empty path name.
.SH Example
For an example of this function, see the Lexicon entry for
.BR libsocket .
.SH "See Also"
.Xr connect(), connect
.Xr getsockname(), getsockna
.Xr libsocket, libsocket
.Xr listen(), listen
.Xr socket() socket
