[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Communications, Volume 2

bind Subroutine


Binds a name to a socket.


Standard C Library (libc.a)


#include <sys/socket.h>

int bind ( Socket, Name, NameLength)
int Socket;
const struct sockaddr *Name;
socklen_t NameLength;


The bind subroutine assigns a Name parameter to an unnamed socket. Sockets created by the socket subroutine are unnamed; they are identified only by their address family. Subroutines that connect sockets either assign names or use unnamed sockets.

In the case of a UNIX domain socket, a connect call only succeeds if the process that calls connect has read and write permissions on the socket file created by the bind call. Permissions are determined by the umask value of the process that created the file.

An application program can retrieve the assigned socket name with the getsockname subroutine.

The socket applications can be compiled with COMPAT_43 defined. This will make the sockaddr structure BSD 4.3 compatible. For more details refer to socket.h.

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.


Socket Specifies the socket descriptor (an integer) of the socket to be bound.
Name Points to an address structure that specifies the address to which the socket should be bound. The /usr/include/sys/socket.h file defines the sockaddr address structure. The sockaddr structure contains an identifier specific to the address format and protocol provided in the socket subroutine.
NameLength Specifies the length of the socket address structure.

Return Values

Upon successful completion, the bind subroutine returns a value of 0.

If the bind subroutine is unsuccessful, the subroutine handler performs the following actions:

Error Codes

The bind subroutine is unsuccessful if any of the following errors occurs:

Value Description
EBADF The Socket parameter is not valid.
ENOTSOCK The Socket parameter refers to a file, not a socket.
EADDRNOTAVAIL The specified address is not available from the local machine.
EADDRINUSE The specified address is already in use.
EINVAL The socket is already bound to an address.
EACCES The requested address is protected, and the current user does not have permission to access it.
EFAULT The Address parameter is not in a writable part of the UserAddress space.
ENODEV The specified device does not exist.


The following program fragment illustrates the use of the bind subroutine to bind the name "/tmp/zan/" to a UNIX domain socket.

#include <sys/un.h>

struct sockaddr_un addr;
strcpy(addr.sun_path, "/tmp/zan/");
addr.sun_len = strlen(addr.sun_path);
addr.sun_family = AF_UNIX; 
bind(s,(struct sockaddr*)&addr, SUN_LEN(&addr));

Related Information

The connect subroutine, getsockname subroutine, listen subroutine, socket subroutine.

Binding Names to Sockets, Reading UNIX Datagrams Example Program, Sockets Overview, Understanding Socket Connections, and Understanding Socket Creation in AIX 5L Version 5.2 Communications Programming Concepts.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]