bind(2) CLIX bind(2)
NAME
bind - Binds a name to a socket
LIBRARY
Berkeley Software Distribution Library (libbsd.a)
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int bind(
int s ,
struct sockaddr *name ,
int namelen );
PARAMETERS
s Represents a socket.
name Contains the address to which the socket should be bound.
namelen Specifies the amount of space pointed to by addr.
DESCRIPTION
The bind() function assigns a name to unnamed socket s. When a socket is
created with the socket() function, it exists in a name space (address
family) but has no name assigned. The bind() function requests that name
be assigned to the socket. The exact format of the name parameter is
determined by the domain in which the communication will occur. The
namelen parameter contains the amount of space pointed to by name.
EXAMPLES
To let the system pick the address and port in the Internet domain:
#include <sys/types.h>
#include <netinet/in.h>
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = 0;
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
perror("Bind failed");
2/94 - Intergraph Corporation 1
bind(2) CLIX bind(2)
NOTES
Binding a name in the UNIX domain creates a socket in the file system that
must be deleted by the caller when it is no longer needed (using the
unlink() function).
RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value
of -1 is returned and errno is set to indicate the error.
ERRORS
The bind() function fails if one or more of the following is true:
[EBADF]
The descriptor is not valid.
[ENOTSOCK]
The descriptor references a file, not a socket.
[EINVAL]
The namelen parameter is not the expected size or the socket is
already bound to an address.
[EFAULT]
The name parameter is not in a valid part of the user address
space.
[EADDRNOTAVAIL]
The specified address is not available from the local machine.
[EADDRINUSE]
The specified address is in use.
The following errors are specific to binding names in the UNIX domain:
[ENOTDIR]
A component of the path prefix is not a directory.
[ENOENT]
A prefix component of the pathname does not exist.
[EIO] An I/O error occurred while making the directory entry or
allocating the inode.
[EROFS]
The name would reside on a read-only file system.
[EISDIR]
A null pathname was specified.
2 Intergraph Corporation - 2/94
bind(2) CLIX bind(2)
[EACCES]
The requested address is protected, and the current user has
inadequate permission to access it.
RELATED INFORMATION
Functions: connect(2), listen(2), socket(2), getsockname(2)
2/94 - Intergraph Corporation 3