{"id":4932,"date":"2022-12-20T18:36:59","date_gmt":"2022-12-20T21:36:59","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/bind-man3p\/"},"modified":"2022-12-20T18:36:59","modified_gmt":"2022-12-20T21:36:59","slug":"bind-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/bind-man3p\/","title":{"rendered":"BIND (man3p)"},"content":{"rendered":"<h1 align=\"center\">BIND<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">bind \u2014 bind a name to a socket<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <sys\/socket.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int bind(int <i>socket<\/i>, const struct sockaddr *<i>address<\/i>, <br \/> socklen_t <i>address_len<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>bind<\/i>() function shall assign a local socket address <i>address<\/i> to a socket identified by descriptor <i>socket<\/i> that has no local socket address assigned. Sockets created with the <i>socket<\/i>() function are initially unnamed; they are identified only by their address family.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>bind<\/i>() function takes the following arguments:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"17%\">\n<p><i>socket<\/i><\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"71%\">\n<p>Specifies the file descriptor of the socket to be bound.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"17%\">\n<p><i>address<\/i><\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"71%\">\n<p>Points to a <b>sockaddr<\/b> 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.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"17%\">\n<p><i>address_len<\/i><\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"71%\">\n<p>Specifies the length of the <b>sockaddr<\/b> structure pointed to by the <i>address<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The socket specified by <i>socket<\/i> may require the process to have appropriate privileges to use the <i>bind<\/i>() function.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the address family of the socket is AF_UNIX and the pathname in <i>address<\/i> names a symbolic link, <i>bind<\/i>() shall fail and set <i>errno<\/i> to <b>[EADDRINUSE]<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the socket address cannot be assigned immediately and O_NONBLOCK is set for the file descriptor for the socket, <i>bind<\/i>() shall fail and set <i>errno<\/i> to <b>[EINPROGRESS]<\/b>, but the assignment request shall not be aborted, and the assignment shall be completed asynchronously. Subsequent calls to <i>bind<\/i>() for the same socket, before the assignment is completed, shall fail and set <i>errno<\/i> to <b>[EALREADY]<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When the assignment has been performed asynchronously, <i>pselect<\/i>(), <i>select<\/i>(), and <i>poll<\/i>() shall indicate that the file descriptor for the socket is ready for reading and writing.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, <i>bind<\/i>() shall return 0; otherwise, \u22121 shall be returned and <i>errno<\/i> set to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>bind<\/i>() function shall fail if: <b><br \/> EADDRINUSE<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified address is already in use.<\/p>\n<p style=\"margin-left:11%;\"><b>EADDRNOTAVAIL<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified address is not available from the local machine.<\/p>\n<p style=\"margin-left:11%;\"><b>EAFNOSUPPORT<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified address is not a valid address for the address family of the specified socket.<\/p>\n<p style=\"margin-left:11%;\"><b>EALREADY<\/b><\/p>\n<p style=\"margin-left:22%;\">An assignment request is already in progress for the specified socket.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EBADF<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>The <i>socket<\/i> argument is not a valid file descriptor.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EINPROGRESS<\/b><\/p>\n<p style=\"margin-left:22%;\">O_NONBLOCK is set for the file descriptor for the socket and the assignment cannot be immediately performed; the assignment shall be performed asynchronously.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>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.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOBUFS<\/b><\/p>\n<p style=\"margin-left:22%;\">Insufficient resources were available to complete the call.<\/p>\n<p style=\"margin-left:11%;\"><b>ENOTSOCK<\/b><\/p>\n<p style=\"margin-left:22%;\">The <i>socket<\/i> argument does not refer to a socket.<\/p>\n<p style=\"margin-left:11%;\"><b>EOPNOTSUPP<\/b><\/p>\n<p style=\"margin-left:22%;\">The socket type of the specified socket does not support binding to an address.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the address family of the socket is AF_UNIX, then <i>bind<\/i>() shall fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>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.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EDESTADDRREQ<\/b> or <b>EISDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">The <i>address<\/i> argument is a null pointer.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EIO<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>An I\/O error occurred.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>A loop exists in symbolic links encountered during resolution of the pathname in <i>address<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">The length of a component of a pathname is longer than {NAME_MAX}.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A component of the path prefix of the pathname in <i>address<\/i> does not name an existing file or the pathname is an empty string.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOENT<\/b> or <b>ENOTDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">The pathname in <i>address<\/i> contains at least one non-<slash> character and ends with one or more trailing <slash> characters. If the pathname without the trailing <slash> characters would name an existing file, an <b>[ENOENT]<\/b> error shall not occur.<\/p>\n<p style=\"margin-left:11%;\"><b>ENOTDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">A component of the path prefix of the pathname in <i>address<\/i> names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in <i>address<\/i> 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.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EROFS<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"75%\">\n<p>The name would reside on a read-only file system.<\/p>\n<\/td>\n<td width=\"3%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>bind<\/i>() function may fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\"><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">The specified address is protected and the current user does not have permission to bind to it.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The <i>address_len<\/i> argument is not a valid length for the address family.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EISCONN<\/b><\/p>\n<p style=\"margin-left:22%;\">The socket is already connected.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in <i>address<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">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}.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following code segment shows how to create a socket and bind it to a name in the AF_UNIX domain.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#define MY_SOCK_PATH &#8220;\/somepath&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int sfd; <br \/> struct sockaddr_un my_addr;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">sfd = socket(AF_UNIX, SOCK_STREAM, 0); <br \/> if (sfd == -1) <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Handle error *\/;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">memset(&#038;my_addr, &#8216;\u0000&#8217;, sizeof(struct sockaddr_un)); <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Clear structure bodies\/ usr\/ <br \/> my_addr.sun_family = AF_UNIX; <br \/> strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) -1);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">if (bind(sfd, (struct sockaddr *) &#038;my_addr, <br \/> sizeof(struct sockaddr_un)) == -1) <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Handle error *\/;<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">An application program can retrieve the assigned socket name with the <i>getsockname<\/i>() function.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>connect<\/i>(), <i>getsockname<\/i>(), <i>listen<\/i>(), <i>socket<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><sys_socket.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. 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.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  bind \u2014 bind a name to a socket <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1826,1594],"class_list":["post-4932","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-bind","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4932"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4932\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}