bind()--Set Local Address for Socket



Syntax

#include <sys/types.h>
#include <sys/socket.h>

int bind(int socket_descriptor,
         struct sockaddr *local_address,
         int address_length)


Threadsafe: Yes

The bind() function is used to associate a local address with a socket.

Parameters

socket_descriptor
(Input) The descriptor of the socket that is to be bound.

local_address
(Input) A pointer to a buffer of type struct sockaddr that contains the local address to which the socket is to be bound. The structure sockaddr is defined in <sys/socket.h>.
      struct sockaddr {
         u_short sa_family;
         char    sa_data[14];
      };

The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.

address_length
(Input) The length of the local_address.

Return Value

bind() returns an integer. Possible values are:

Error Conditions

When a bind() fails, errno can be set to one of the following:

[EACCES]
Permission denied.

The process does not have the appropriate privileges to bind local_address to the socket pointed to by socket_descriptor (for example, if socket_descriptor is a socket with an address family of AF_INET, and the sockaddr_in structure (pointed to by local_address) specified a port that was restricted for use).

[EADDRINUSE]
Address already in use.

This error code indicates one of the following:

[EADDRNOTAVAIL]
Address not available. This error code indicates one of the following:
[EAFNOSUPPORT]
The type of socket is not supported in this protocol family.

The address family specified in the address structure pointed to by local_address parameter cannot be used with the socket pointed to by the socket_descriptor parameter.

[EBADF]
Descriptor not valid.
[EFAULT]
Bad address.

The system detected an address which was not valid while attempting to access the local_address parameter.

[EINVAL]
Parameter not valid. This error code indicates one of the following:
[EIO]
Input/output error.
[ELOOP]
A loop exists in symbolic links encountered during pathname resolution.

This error code is only returned on sockets that use the AF_UNIX address family.

[ENAMETOOLONG]
File name too long.

This error code is only returned on sockets that use the AF_UNIX address family.

[ENOBUFS]
There is not enough buffer space for the requested operation.
[ENOENT]
No such file or directory.

This error code is only returned on sockets that use the AF_UNIX address family.

[ENOSYS]
Function not implemented.

This error code is only returned on sockets that use the AF_UNIX address family.

[ENOTDIR]
Not a directory.

This error code is only returned on sockets that use the AF_UNIX address family.

[ENOTSOCK]
The specified descriptor does not reference a socket.
[EUNKNOWN]
Unknown system state.
[EUNATCH]
The protocol required to support the specified address family is not available at this time.

Error Messages

CPE3418 E
Possible APAR condition or hardware failure.
CPF9872 E
Program or service program &1 in library &2 ended. Reason code &3.
CPFA081 E
Unable to set return value or error code.

Usage Notes

  1. For sockets that use an address family of AF_UNIX, the following is applicable:

  2. For sockets that use an address family of AF_INET, the following is applicable:

  3. For sockets that use an address family of AF_NS, the following is applicable:

  4. For sockets that use an address family of AF_TELEPHONY, the following is applicable:

Related Information


Top | Sockets APIs | APIs by category


[Information Center Home Page | Feedback ] [Legal | AS/400 Glossary]