BIND(3POSIX) | POSIX Programmer's Manual | BIND(3POSIX) |
This manual page is part of the POSIX Programmer's 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.
bind — bind a name to a socket
#include <sys/socket.h>
int bind(int socket, const struct sockaddr *address, socklen_t address_len);
The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Sockets created with the socket() function are initially unnamed; they are identified only by their address family.
The bind() function takes the following arguments:
The socket specified by socket may require the process to have appropriate privileges to use the bind() function.
If the address family of the socket is AF_UNIX and the pathname in address names a symbolic link, bind() shall fail and set errno to [EADDRINUSE].
If the socket address cannot be assigned immediately and O_NONBLOCK is set for the file descriptor for the socket, bind() shall fail and set errno to [EINPROGRESS], but the assignment request shall not be aborted, and the assignment shall be completed asynchronously. Subsequent calls to bind() for the same socket, before the assignment is completed, shall fail and set errno to [EALREADY].
When the assignment has been performed asynchronously, pselect(), select(), and poll() shall indicate that the file descriptor for the socket is ready for reading and writing.
Upon successful completion, bind() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
The bind() function shall fail if:
If the address family of the socket is AF_UNIX, then bind() shall fail if:
The bind() function may fail if:
The following sections are informative.
The following code segment shows how to create a socket and bind it to a name in the AF_UNIX domain.
#define MY_SOCK_PATH "/somepath"
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 */;
An application program can retrieve the assigned socket name with the getsockname() function.
None.
None.
connect(), getsockname(), listen(), socket()
The Base Definitions volume of POSIX.1‐2017, <sys_socket.h>
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- 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 .
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 .
2017 | IEEE/The Open Group |