Berkeley sockets

From Academic Kids

The Berkeley sockets application programming interface (API) comprises a library for developing applications written in the C programming language that access a computer network.

Berkeley sockets (also known as the BSD socket API) originated with the 4.2BSD system (released in 1983) as an API, covered under the BSD license, for development of sockets. Only in 1989, however, could UC Berkeley release versions of its operating system and networking library free from the licensing constraints of the Open Group's copyright-protected UNIX operating system.

The Berkeley socket API forms the de facto standard abstraction for network sockets. Most other programing languages use a similar interface as the C API.


The Header Files

The Berkeley socket development library has many associated header files. They include:


 Definitions for the most basic of socket structures with the BSD socket API


 Basic data types associated with structures within the BSD socket API


 Definitions for the socketaddr_in{} and other base data structures.


 Definitions and data type declarations for SOCK_UNIX streams

The Client Side


socket() creates an endpoint for communication and returns a descriptor. Socket takes three arguments:

  • domain, which specifies the address family of the socket that is created (AF_INET, for example).
  • type, which is one of SOCK_STREAM (TCP), SOCK_DGRAM (UDP), or SOCK_RAW (RAW).
  • protocol, which is usually set to 0 to represent the default protocol for the specified domain and type.

The function returns -1 if an error occurred. Otherwise, it returns an integer representing the newly-assigned descriptor.


int socket(int domain, int type, int protocol);

gethostbyname() and gethostbyaddr()


struct hostent *gethostbyname(const char *name);

struct hostent *gethostbyaddr(const void *addr, int len, int type);


connect() attempts to connect a client socket to a local or remote server socket in the listening state. connect takes three arguments:

  • sockfd, a valid descriptor created with socket().
  • serv_addr, a pointer to a sockaddr structure describing the address and port that the socket should attempt to connect to.
  • addrlen, a socklen_t (typically an unsigned integral type) containing the length of the given sockaddr structure in bytes.

It returns an integer representing the error code: 0 represents success, while -1 represents an error.

Certain types of sockets are connectionless, the most common being user datagram protocol sockets. For these sockets, connect takes on a special meaning: The default target for sending and receiving data will be set to the given address, allowing the use of functions such as send() and recv() on connectionless sockets.


int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

The Server Side


Refer to description above


bind() assigns a socket an address. When a socket is created using socket(), it is given an address family, but not assigned an address. Before a socket may accept incoming connections, it must be bound. Bind takes three arguments:

  • sockfd, a descriptor representing the socket to perform the bind on
  • my_addr, a pointer to a sockaddr structure representing the address to bind to.
  • addrlen, a socklen_t field representing the length of the sockaddr structure.

It returns 0 on success and -1 if an error occurs.


int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);


listen() prepares a bound socket to accept incoming connections. This function is only applicable to the SOCK_STREAM and SOCK_SEQPACKET socket types. It takes 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.

Once a connection is accepted, it is dequeued. The operating system usually places a cap on this value. On success, 0 is returned. If an error occurs, -1 is returned.


int listen(int sockfd, int backlog);


Programmers use accept() to satisfy a connection request from a remote host. A specified socket on the local host (which must have the capability of accepting the connection) connects to the requesting socket on the remote host. The code returns the remote socket's descriptor (or -1 if an error occurs).


int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Blocking vs. Nonblocking

Berkeley sockets can operate in one of two modes: blocking or non-blocking. A blocking socket will not "return" until it has sent (or received) all the data specified for the operation. This may cause problems if a socket continues to listen: a program may hang as the socket waits for data that may never arrive.

A socket is typically set to blocking or nonblocking mode using the fcntl() or ioctl() functions.

See also: Computer network

External links

  • Unix Manual Pages: ( *accept(2) ( /connect(2) (
This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.

Academic Kids Menu

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools