Bind a socket to a port.
Bind is usually used by servers so that it may listen on that port and accept incoming connections. When the server replies, the packets will be sendt from the port.
Connectionless sockets may use a single socket to accept packets from any host
in the network. Specify an address structure to recvfrom to get the address
of the host that sendt the packet, and use the same address structure with
sendto in order to reply to the same host.
Syntax
#include <sys/socket.h>
int bind (int fd, const struct sockaddr *addr, socklen_t addrlen);
Parameters
-
fdThe file descriptor of the socket, allocated using
socket. -
addrPointer to a
sockaddrstructure. Usually a structure specific to the socket type is used, with thesockaddrtype only used for typecast reasons. -
addrlenLength of the supplied address structure.
Return Value
The return value is null if successful. Otherwise, the return value is negative and errno is set:
-
EBADF
Bad file descriptor.
-
EINVAL
The file descriptor is not a socket.
The
addrargument is invalid.The length of the address structure (
addrlen) is not of the expected size. -
ENOMEM
Out of memory.
Remarks
See the documentation for the correct socket address family (network stack) for details related to it.