BIND(3POSIX)
π BIND(3POSIX) π
okay bestie, so this is part of the POSIX Programmer's Manual, and real talk: the Linux implementation of this interface may be different (so go peek at the matching Linux manual page for the actual Linux tea), or honestly the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
bind β bind a name to a socket, periodt π
πΈ SYNOPSIS πΈ
#include <sys/socket.h>
int bind(int socket, const struct sockaddr *address,
socklen_t address_len);
π¦ DESCRIPTION π¦
So bind() shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Sockets made with the socket() function start out unnamed, like, they're identified only by their address family. main character with no name tag fr fr π
The bind() function takes these arguments:
socketβ Specifies the file descriptor of the socket to be bound.addressβ Points to asockaddrstructure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket. iykyk πaddress_lenβ Specifies the length of thesockaddrstructure pointed to by the address argument.
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]. like it's still cooking, don't rush her π₯
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.
β RETURN VALUE β
Upon successful completion, bind() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error. simple as that bestie π«Ά
π ERRORS π
The bind() function shall fail if:
EADDRINUSEβ The specified address is already in use. (somebody already claimed that name, soz π)EADDRNOTAVAILβ The specified address is not available from the local machine.EAFNOSUPPORTβ The specified address is not a valid address for the address family of the specified socket.EALREADYβ An assignment request is already in progress for the specified socket.EBADFβ The socket argument is not a valid file descriptor.EINPROGRESSβO_NONBLOCKis set for the file descriptor for the socket and the assignment cannot be immediately performed; the assignment shall be performed asynchronously.EINVALβ 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.ENOBUFSβ Insufficient resources were available to complete the call.ENOTSOCKβ The socket argument does not refer to a socket.EOPNOTSUPPβ 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 bind() shall fail if:
EACCESβ 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.EDESTADDRREQorEISDIRβ The address argument is a null pointer.EIOβ An I/O error occurred.ELOOPβ A loop exists in symbolic links encountered during resolution of the pathname in address.ENAMETOOLONGβ The length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ A component of the path prefix of the pathname in address does not name an existing file or the pathname is an empty string.ENOENTorENOTDIRβ The pathname in address 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[ENOENT]error shall not occur.ENOTDIRβ A component of the path prefix of the pathname in address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in address 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.EROFSβ The name would reside on a read-only file system.
The bind() function may fail if:
EACCESβ The specified address is protected and the current user does not have permission to bind to it.EINVALβ The address_len argument is not a valid length for the address family.EISCONNβ The socket is already connected.ELOOPβ More than{SYMLOOP_MAX}symbolic links were encountered during resolution of the pathname in address.ENAMETOOLONGβ 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}.
heads up bestie: the following sections are informative. like, they're the vibe, not the law π
π§ EXAMPLES π§
This code segment shows how to create a socket and bind it to a name in the AF_UNIX domain. slay π
#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 */;
π APPLICATION USAGE π
An application program can retrieve the assigned socket name with the getsockname() function. so if you forgot what you named her, getsockname() has your back π
π RATIONALE π
None. (nothing to explain bestie, it just is what it is π)
π FUTURE DIRECTIONS π
None. (the future is bright and also empty β¨)
π₯ SEE ALSO π₯
connect()getsockname()listen()socket()
The Base Definitions volume of POSIX.1β2017, <sys_socket.h> π
π COPYRIGHT π
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 .
and that's the whole page bestie!! brought to you by IEEE/The Open Group, dated 2017, filed under BIND(3POSIX) β mwah, go bind something iconic π β¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms