BIND(3)                    POSIX Programmer's Manual                   BIND(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       bind -- bind a name to a socket

SSYYNNOOPPSSIISS
       #include <sys/socket.h>

       int bind(int _s_o_c_k_e_t, const struct sockaddr *_a_d_d_r_e_s_s,
           socklen_t _a_d_d_r_e_s_s___l_e_n);

DDEESSCCRRIIPPTTIIOONN
       The _b_i_n_d() function shall assign a local socket address _a_d_d_r_e_s_s to a
       socket identified by descriptor _s_o_c_k_e_t that has no local socket address
       assigned. Sockets created with the _s_o_c_k_e_t() function are initially
       unnamed; they are identified only by their address family.

       The _b_i_n_d() function takes the following arguments:

       _s_o_c_k_e_t      Specifies the file descriptor of the socket to be bound.

       _a_d_d_r_e_s_s     Points to a ssoocckkaaddddrr structure containing the address to be
                   bound to the socket. The length and format of the address
                   depend on the address family of the socket.

       _a_d_d_r_e_s_s___l_e_n Specifies the length of the ssoocckkaaddddrr structure pointed to
                   by the _a_d_d_r_e_s_s argument.

       The socket specified by _s_o_c_k_e_t may require the process to have
       appropriate privileges to use the _b_i_n_d() function.

       If the address family of the socket is AF_UNIX and the pathname in
       _a_d_d_r_e_s_s names a symbolic link, _b_i_n_d() shall fail and set _e_r_r_n_o to
       [[EEAADDDDRRIINNUUSSEE]].

       If the socket address cannot be assigned immediately and O_NONBLOCK is
       set for the file descriptor for the socket, _b_i_n_d() shall fail and set
       _e_r_r_n_o to [[EEIINNPPRROOGGRREESSSS]], but the assignment request shall not be
       aborted, and the assignment shall be completed asynchronously.
       Subsequent calls to _b_i_n_d() for the same socket, before the assignment
       is completed, shall fail and set _e_r_r_n_o to [[EEAALLRREEAADDYY]].

       When the assignment has been performed asynchronously, _p_s_e_l_e_c_t(),
       _s_e_l_e_c_t(), and _p_o_l_l() shall indicate that the file descriptor for the
       socket is ready for reading and writing.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _b_i_n_d() shall return 0; otherwise, -1 shall
       be returned and _e_r_r_n_o set to indicate the error.

EERRRROORRSS
       The _b_i_n_d() function shall fail if:

       EEAADDDDRRIINNUUSSEE
              The specified address is already in use.

       EEAADDDDRRNNOOTTAAVVAAIILL
              The specified address is not available from the local machine.

       EEAAFFNNOOSSUUPPPPOORRTT
              The specified address is not a valid address for the address
              family of the specified socket.

       EEAALLRREEAADDYY
              An assignment request is already in progress for the specified
              socket.

       EEBBAADDFF  The _s_o_c_k_e_t argument is not a valid file descriptor.

       EEIINNPPRROOGGRREESSSS
              O_NONBLOCK is set for the file descriptor for the socket and the
              assignment cannot be immediately performed; the assignment shall
              be performed asynchronously.

       EEIINNVVAALL The socket is already bound to an address, and the protocol does
              not support binding to a new address; or the socket has been
              shut down.

       EENNOOBBUUFFSS
              Insufficient resources were available to complete the call.

       EENNOOTTSSOOCCKK
              The _s_o_c_k_e_t argument does not refer to a socket.

       EEOOPPNNOOTTSSUUPPPP
              The socket type of the specified socket does not support binding
              to an address.

       If the address family of the socket is AF_UNIX, then _b_i_n_d() shall fail
       if:

       EEAACCCCEESS A component of the path prefix denies search permission, or the
              requested name requires writing in a directory with a mode that
              denies write permission.

       EEDDEESSTTAADDDDRRRREEQQ or EEIISSDDIIRR
              The _a_d_d_r_e_s_s argument is a null pointer.

       EEIIOO    An I/O error occurred.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the pathname in _a_d_d_r_e_s_s.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of the path prefix of the pathname in _a_d_d_r_e_s_s does
              not name an existing file or the pathname is an empty string.

       EENNOOEENNTT or EENNOOTTDDIIRR
              The pathname in _a_d_d_r_e_s_s contains at least one non-<slash>
              character and ends with one or more trailing <slash> characters.
              If the pathname names an existing file, an [[EENNOOEENNTT]] error shall
              not occur.

       EENNOOTTDDIIRR
              A component of the path prefix of the pathname in _a_d_d_r_e_s_s names
              an existing file that is neither a directory nor a symbolic link
              to a directory, or the pathname in _a_d_d_r_e_s_s contains at least one
              non-<slash> character and ends with one or more trailing <slash>
              characters and the last pathname component names an existing
              file that is neither a directory nor a symbolic link to a
              directory.

       EERROOFFSS  The name would reside on a read-only file system.

       The _b_i_n_d() function may fail if:

       EEAACCCCEESS The specified address is protected and the current user does not
              have permission to bind to it.

       EEIINNVVAALL The _a_d_d_r_e_s_s___l_e_n argument is not a valid length for the address
              family.

       EEIISSCCOONNNN
              The socket is already connected.

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the pathname in _a_d_d_r_e_s_s.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       The following code segment shows how to create a socket and bind it to
       a name in the AF_UNIX domain.

           ##ddeeffiinnee MMYY__SSOOCCKK__PPAATTHH ""//ssoommeeppaatthh""

           int sfd;
           struct sockaddr_un my_addr;

           sfd = socket(AF_UNIX, SOCK_STREAM, 0);
           if (sfd == -1)
               /* Handle error */;

           memset(&my_addr, '\0', sizeof(struct sockaddr_un));
                                /* Clear structure */
           my_addr.sun_family = AF_UNIX;
           strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) -1);

           if (bind(sfd, (struct sockaddr *) &my_addr,
               sizeof(struct sockaddr_un)) == -1)
               /* Handle error */;

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       An application program can retrieve the assigned socket name with the
       _g_e_t_s_o_c_k_n_a_m_e() function.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _c_o_n_n_e_c_t(), _g_e_t_s_o_c_k_n_a_m_e(), _l_i_s_t_e_n(), _s_o_c_k_e_t()

       The Base Definitions volume of POSIX.1-2008, <<ssyyss__ssoocckkeett..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                              BIND(3)
