The Berkeley socket API typically provides the following functions: • creates a new socket of a certain type, identified by an integer number, and allocates system resources to it. • is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local IP address and a port number. • is used on the server side, and causes a bound TCP socket to enter listening state. • is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to establish a new TCP connection. • is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection. • , , , and are used for sending and receiving data. The standard functions and may also be used. • causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated. • and are used to resolve host names and addresses. IPv4 only. • and are used to resolve host names and addresses. IPv4, IPv6. • is used to suspend, waiting for one or more of a provided list of sockets to be ready to read, ready to write, or that have errors. • is used to check on the state of a socket in a set of sockets. The set can be tested to see if any socket can be written to, read from or if an error occurred. • is used to retrieve the current value of a particular socket option for the specified socket. • is used to set a particular socket option for the specified socket.
socket The function creates an endpoint for communication and returns a
file descriptor for the socket. It uses three arguments: • , which specifies the protocol family of the created socket. For example: • for network protocol
IPv4 (IPv4-only) • for
IPv6 (and in some cases,
backward compatible with IPv4) • for local socket (using a special filesystem node) • , one of: • (reliable stream-oriented service or
stream sockets) • (datagram service or
datagram sockets) • (reliable sequenced packet service) • (raw protocols atop the network layer) • specifying the actual transport protocol to use. The most common are Transmission Control Protocol|, SCTP|, User Datagram Protocol|, DCCP|. These protocols are specified in file . The value may be used to select a default protocol from the selected domain and type. The function returns if an error occurred. Otherwise, it returns an integer representing the newly assigned descriptor.
bind associates a socket with an address. When a socket is created with , it is only given a protocol family, but not assigned an address. This association must be performed before the socket can accept connections from other hosts. The function has three arguments: • sockfd, a descriptor representing the socket • my_addr, a pointer to a structure representing the address to bind to. • addrlen, a field of type specifying the size of the structure. returns on success and if an error occurs.
listen After a socket has been associated with an address, listen() prepares it for incoming connections. However, this is only necessary for the stream-oriented (connection-oriented) data modes, i.e., for socket types (SOCK_STREAM, SOCK_SEQPACKET). listen() requires two arguments: • sockfd, a valid socket descriptor. • backlog, an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value. Once a connection is accepted, it is dequeued. On success, 0 is returned. If an error occurs, -1 is returned.
accept When an application is listening for stream-oriented connections from other hosts, it is notified of such events (cf. Select (Unix)| function) and must initialize the connection using function . It creates a new socket for each connection and removes the connection from the listening queue. The function has the following arguments: • sockfd, the descriptor of the listening socket that has the connection queued. • cliaddr, a pointer to a sockaddr structure to receive the client's address information. • addrlen, a pointer to a location that specifies the size of the client address structure passed to . When returns, this location contains the size (in bytes) of the structure. returns the new socket descriptor for the accepted connection, or the value if an error occurs. All further communication with the remote host now occurs via this new socket. Datagram sockets do not require processing by since the receiver may immediately respond to the request using the listening socket.
connect establishes a direct communication link to a specific remote host identified by its address via a socket, identified by its file descriptor. When using a
connection-oriented protocol, this establishes a connection. Certain types of protocols are connectionless, most notably the
User Datagram Protocol. When used with connectionless protocols, defines the remote address for sending and receiving data, allowing the use of functions such as and . In these cases, the connect function prevents reception of datagrams from other sources. returns an integer representing the error code: represents success, while represents an error. Historically, in BSD-derived systems, the state of a socket descriptor is undefined if the call to fails (as it is specified in the
Single Unix Specification), thus, portable applications should close the socket descriptor immediately and obtain a new descriptor with , in the case the call to fails.
gethostbyname and gethostbyaddr The functions and are used to resolve host names and addresses in the
domain name system or the local host's other resolver mechanisms (e.g., /etc/hosts lookup). They return a pointer to an object of type struct hostent, which describes an
Internet Protocol host. The functions use the following arguments: • name specifies the name of the host. • addr specifies a pointer to a struct in_addr containing the address of the host. • len specifies the length, in bytes, of addr. • type specifies the address family type (e.g., AF_INET) of the host address. The functions return in case of error, in which case the external integer may be checked to see whether this is a temporary failure or an invalid or unknown host. Otherwise a valid struct hostent* is returned. These functions are not strictly a component of the BSD socket API, but are often used in conjunction with the API functions for looking up a host. These functions are now considered legacy interfaces for querying the domain name system. New functions that are completely protocol-agnostic (supporting IPv6) have been defined. These new functions are
and, and are based on a new
data structure. This pair of functions appeared at the same time as the BSD socket API proper in 4.2BSD (1983), the same year DNS was first created. Early versions did not query DNS and only performed /etc/hosts lookup. The 4.3BSD (1984) version added DNS in a crude way. The current implementation using
Name Service Switch derives Solaris and later
NetBSD 1.4 (1999). Initially defined for
NIS+, NSS makes DNS only one of the many options for lookup by these functions and its use can be disabled even today. ==Protocol and address families==