[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Communications Technical Reference, Volume 2

bind Subroutine

Purpose

Binds a name to a socket.

Library

Standard C Library (libc.a)

Syntax

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ndd_var.h>  /*Needed for AF_NDD address family only*/
#include <sys/atmsock.h> /*Needed for SOCK_CONN_DGRAM socket type only*/
int bind (Socket, Name, NameLength)
int Socket;
const struct sockaddr *Name;
size_t NameLength;

Description

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.

Parameters

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:

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.

Examples

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));

Implementation Specifics

The bind subroutine is part of Base Operating System (BOS) Runtime.

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.

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 Version 4.3 Communications Programming Concepts.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]