[ Stream I/O | Reference Manual | Alphabetic Index ]

# bind(+Stream, ?Address)

Associates an address with a given socket stream.

Stream
  Stream handle or alias (atom)
Address
  Atom, structure or variable.

## Description

bind/2 is a direct link to the bind(2) socket system call. Stream must be a socket stream created with socket/3. If the socket was created in the unix domain, Address must be an atom which identifies the file associated with the socket. This file name can then be used by other processes to connect with this socket using the predicate connect/2.

If the socket is in the internet domain, the address is in the form HostName/Port, where any of Address or HostName or Port may be uninstantiated. When the port is already in use, the predicate raises an error and so it is always safest to call bind/2 with Address uninstantiated (this corresponds to the INADDR_ANY value for the system call), and upon success it will be instantiated to the hostname and selected port number.

Stream sockets are connected using the standard sequence, i.e. socket/3, bind/2, listen/2 and accept/3 on the server and socket/3 and connect/2 on the client. After the sockets are connected, both processes can use them for reading and writing.

Datagram sockets require a connect/2 call from the process that wants to write on the socket and bind/2 from the one that reads from it.

### Modes and Determinism

- bind(+, +) is det
- bind(+, -) is det

### Exceptions

(4) instantiation fault
  Stream is not instantiated.
(5) type error
  Stream is instantiated, but not to an atom or a stream handle.
(5) type error
  Address is instantiated but not to the form accepted by the socket domain.
(170) system interface error
  It was not possible to bind the socket.

## Examples

    Success:

    Error:
          bind(s, Host/p)               (Error 5).
          bind(s, '/usr/bin')           (Error 170).

## See Also

socket / 3, listen / 2, accept / 3, connect / 2, new_socket_server / 3, current_stream / 1, get_stream_info / 3