bind(2) System Calls Manual bind(2) NOMBRE bind - enlaza un nombre a un conector (socket) BIBLIOTECA Biblioteca Estandar C (libc, -lc) SINOPSIS #include int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); DESCRIPCION When a socket is created with socket(2), it exists in a name space (address family) but has no address assigned to it. bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called "assigning a name to a socket". Normalmente, es necesario asignar una direccion local usando bind() a un conector SOCK_STREAM antes de que este pueda recibir conexiones (vea accept(2)). Las reglas usadas en el enlace de nombres varian entre familias de direcciones. Consulte las entradas de manual de la Seccion 7 para obtener una informacion mas detallada. Para AF_INET vea ip(7), para AF_INET6 vea ipv6(7), para AF_UNIX vea unix(7), para AF_APPLETALK vea ddp(7), para AF_PACKET vea packet(7), para AF_X25 vea x25(7) y para AF_NETLINK vea netlink(7). The actual structure passed for the addr argument will depend on the address family. The sockaddr structure is defined as something like: struct sockaddr { sa_family_t sa_family; char sa_data[14]; } The only purpose of this structure is to cast the structure pointer passed in addr in order to avoid compiler warnings. See EXAMPLES below. VALOR DEVUELTO En caso de exito se devuelve cero. En caso de error se devuelve -1, y errno se configura para indicar el error. ERRORES EACCES La direccion esta protegida y el usuario no es el superusuario. EADDRINUSE The given address is already in use. EADDRINUSE (Internet domain sockets) The port number was specified as zero in the socket address structure, but, upon attempting to bind to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range ip(7). EBADF sockfd no es un descriptor de archivo valido. EINVAL The socket is already bound to an address. EINVAL addrlen is wrong, or addr is not a valid address for this socket's domain. ENOTSOCK El descriptor de archivo sockfd no se refiere a un conector. Los siguientes errores son especificos a los conectores del dominio UNIX (AF_UNIX): EACCES El permiso de busqueda ha sido denegado en uno de los componentes de la ruta. Consulte path_resolution(7). EADDRNOTAVAIL Se requirio una interfaz inexistente o una direccion no local. EFAULT addr senala fuera del espacio de direcciones accesible por el usuario. ELOOP Se han encontrado demasiados enlaces simbolicos al resolver addr. ENAMETOOLONG addr es demasiado larga. ENOENT No existe algun componente del prefijo del directorio del nombre de ruta del conector. ENOMEM No hay disponible suficiente memoria del nucleo. ENOTDIR Un componente del prefijo de la ruta no es un directorio. EROFS El nodo-i del conector reside en un sistema de ficheros de `solo lectura'. ESTANDARES POSIX.1-2008. HISTORIAL POSIX.1-2001, SVr4, 4.4BSD (bind() first appeared in 4.2BSD). ERRORES No estan descritas las opciones de proxy transparente. EJEMPLOS An example of the use of bind() with Internet domain sockets can be found in getaddrinfo(3). The following example shows how to bind a stream socket in the UNIX (AF_UNIX) domain, and accept connections: #include #include #include #include #include #include #define MY_SOCK_PATH "/somepath" #define LISTEN_BACKLOG 50 #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(void) { int sfd, cfd; socklen_t peer_addr_size; struct sockaddr_un my_addr, peer_addr; sfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sfd == -1) handle_error("socket"); memset(&my_addr, 0, sizeof(my_addr)); 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(my_addr)) == -1) handle_error("bind"); if (listen(sfd, LISTEN_BACKLOG) == -1) handle_error("listen"); /* Now we can accept incoming connections one at a time using accept(2). */ peer_addr_size = sizeof(peer_addr); cfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size); if (cfd == -1) handle_error("accept"); /* Code to deal with incoming connection(s)... */ if (close(sfd) == -1) handle_error("close"); if (unlink(MY_SOCK_PATH) == -1) handle_error("unlink"); } VEASE TAMBIEN accept(2), connect(2), getsockname(2), listen(2), socket(2), getaddrinfo(3), getifaddrs(3), ip(7), ipv6(7), path_resolution(7), socket(7), unix(7) TRADUCCION La traduccion al espanol de esta pagina del manual fue creada por Miguel Angel Sepulveda , Juan Piernas y Marcos Fouces Esta traduccion es documentacion libre; lea la GNU General Public License Version 3 o posterior con respecto a las condiciones de copyright. No existe NINGUNA RESPONSABILIDAD. Si encuentra algun error en la traduccion de esta pagina del manual, envie un correo electronico a . Paginas de manual de Linux 6.06 31 Octubre 2023 bind(2)