bind

Associates local address information with a socket.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <sys/socket.h> 
   
  int bind (
     int                     s,
     const struct sockaddr  *addr,
     size_t                  len);
  

Parameters

s

(IN) Specifies the socket file handle to which address information is to be assigned.

addr

(IN) Points to the location for the address that is assigned to an unbound socket. For IPv4, addr should point to a sockaddr structure; for IPv6, to a sockaddr_in6 structure.

len

(IN) Specifies the amount of space (in bytes) pointed to by addr.

Return Values

If successful, returns 0. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

The s parameter is not a valid socket file handle.

6

EACCES

The requested address is protected, and the current client lacks permission to access it.

9

EINVAL

The socket is already bound to an address, or an invalid address length is specified.

27

EFAULT

An address is not specified.

38

ENOTSOCK

The s parameter is not a valid socket file handle.

42

ENOPROTOOPT

The option is unknown at the level indicated.

48

EADDRINUSE

The specified address is already in use.

49

EADDRNOTAVAIL

The specified address is not available from the local computer.

Remarks

When a socket is created, the socket exists in an addr space (address family) but has no address assigned. The bind function requests that an address be assigned to the socket. Once addr is bound to a socket, another socket can establish communication by referring to that address.

If you want to specify the local address, pass INADDR_ANY (zero) as the IP address to addr. The protocol code then assigns the local address. You can still specify the TCP port in the sin_port or sin6_port field.

See Also

listen