int bind(int socket, const struct sockaddr *address, socklen_t address_len);
DESCRIPTION bind() assigns a name to an unnamed socket. When a socket is created with socket(2) it exists in a name space (address family) but has no name assigned. bind() requests that address be assigned to the socket.
NOTES Binding a name in the UNIX domain creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)).
The rules used in name binding vary between communication domains. Consult the manual entries in section 4 for detailed information.
RETURN VALUES Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to indicate the error.
man 2 listen LISTEN(2) BSD System Calls Manual LISTEN(2)
NAME listen -- listen for connections on a socket
SYNOPSIS #include <sys/socket.h>
int listen(int socket, int backlog);
DESCRIPTION Creation of socket-based connections requires several operations. First, a socket is created with socket(2). Next, a willingness to accept incoming connections and a queue limitfor incoming connections are specified with listen(). Finally, the connections are accepted with accept(2). The listen() call applies only to sockets of type SOCK_STREAM.
The backlog parameter defines the maximum length for the queue of pending connections. If a connection request arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED. Alternatively, if the underlying protocol supports retransmission, the request may be ignored so that retries may succeed.
RETURN VALUES The listen() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
man 2 accept ACCEPT(2) BSD System Calls Manual ACCEPT(2)
NAME accept -- accept a connection on a socket
SYNOPSIS #include <sys/socket.h>
int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
DESCRIPTION The argument socket is a socket that has been created with socket(2), bound to an address with bind(2), and is listen- ing for connections after a listen(2). accept() extracts the first connection request on the queue of pending connec- tions, creates a new socket with the same properties of socket, and allocates a new file descriptor for the socket. If no pending connections are present on the queue, and the socket is not marked as non-blocking, accept() blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, accept() returns an error as described below. The accepted socket may not be used to accept more connec- tions. The original socket socket, remains open.
The argument address is a result parameter that is filled in with the address of the connecting entity, as known to the communications layer. The exact format of the address parameter is determined by the domain inwhich the communi- cation is occurring. The address_len is a value-result parameter; it should initially contain the amount of space pointed to by address; on return it will contain the actual length (in bytes) of the address returned. This call is used with connection-based socket types, currently with SOCK_STREAM.
It is possible to select(2) a socket for the purposes of doing an accept() by selecting it forread.
For certain protocols which require an explicit confirmation, such as ISO or DATAKIT, accept() can be thought of as merely dequeuing the next connection request and not implying confirmation. Confirmation can be implied by a normal read or write on the new file descriptor, and rejection can be implied by closing the new socket.
One can obtain user connection request data without confirming the connection by issuing a recvmsg(2) call with an msg_iovlen of 0 and a non-zero msg_controllen, or by issuing a getsockopt(2) request. Similarly, one can provide user connection rejection information by issuing a sendmsg(2) call with providing only the control information, or by call- ing setsockopt(2).
RETURN VALUES The call returns -1 on error and the global variable errno is set to indicate the error. If it succeeds, it returns a non-negative integer that is a descriptor for the accepted socket.
RETURN VALUES Upon successful completion, the number of bytes which were sent is returned. Otherwise, -1 is returned and the global variable errno is set to indicate the error.