ArticleslgStudy

computer science

Berkeley sockets

Berkeley sockets is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Berkeley sockets rather than just read about it. In short: Berkeley sockets is an application programming interface (API) for Internet domain sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules.

Berkeley sockets — main illustration
Berkeley sockets — illustration

Key takeaways

  • Berkeley sockets belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Berkeley sockets to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Berkeley sockets from memory before moving on to harder problems.

Reference excerpt

Berkeley sockets is an application programming interface (API) for Internet domain sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules. It originated with the 4.2BSD Unix operating system, which was released in 1983. A socket is an abstract representation (handle) for the local endpoint of a network communication path. The Berkeley sockets API represents it as a file descriptor in the Unix philosophy that provides a common interface for input and output to streams of data. Berkeley sockets evolved with little modification from a de facto standard into a component of the POSIX specification. The term POSIX sockets is essentially synonymous with Berkeley sockets, but they are also known as BSD sockets, acknowledging the first implementation in the Berkeley Software Distribution.

History and implementations Berkeley sockets originated with the 4.2BSD Unix operating system, released in 1983, as a programming interface. Not until 1989, however, could the University of California, Berkeley release versions of the operating system and networking library free from the licensing constraints of AT&T Corporation's proprietary Unix. All modern operating systems implement a version of the Berkeley socket interface. It became the standard interface for applications running in the Internet. Even the Winsock implementation for Microsoft Windows, created by unaffiliated developers, closely follows the standard. The BSD sockets API is written in the C programming language. Most other programming languages provide similar interfaces, typically written as a wrapper library based on the C API.

BSD and POSIX sockets As the Berkeley socket API evolved and ultimately yielded the POSIX socket API, certain functions were deprecated or removed and replaced by others. The POSIX API is also designed to be reentrant and supports IPv6.

Alternatives The STREAMS-based Transport Layer Interface (TLI) API offers an alternative to the socket API. Many systems that provide the TLI API also provide the Berkeley socket API. Non-Unix systems often expose the Berkeley socket API with a translation layer to a native networking API. Plan 9 and Genode use file-system APIs with control files rather than file-descriptors.

Header files The Berkeley socket interface is defined in several header files. The names and content of these files differ slightly between implementations. In general, they include:

Socket API functions

The Berkeley socket API typically provides the following functions:

socket() creates a new socket of a certain type, identified by an integer number, and allocates system resources to it. bind() 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. listen() is used on the server side, and causes a bound TCP socket to enter listening state. connect() 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. accept() 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. send(), recv(), sendto(), and recvfrom() are used for sending and receiving data. The standard functions write() and read() may also be used. close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated. gethostbyname() and gethostbyaddr() are used to resolve host names and addresses. IPv4 only. getaddrinfo() and freeaddrinfo() are used to resolve host names and addresses. IPv4, IPv6. select() 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. poll() 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. getsockopt() is used to retrieve the current value of a particular socket option for the specified socket. setsockopt() is used to set a particular socket option for the specified socket.

socket The function socket() creates an endpoint for communication and returns a file descriptor for the socket. It uses three arguments:

domain, which specifies the protocol family of the created socket. For example: PF_INET for network protocol IPv4 (IPv4-only) PF_INET6 for IPv6 (and in some cases, backward compatible with IPv4) PF_UNIX for local socket (using a special filesystem node) type, one of: SOCK_STREAM (reliable stream-oriented service or stream sockets) SOCK_DGRAM (datagram service or datagram sockets) SOCK_SEQPACKET (reliable sequenced packet service) SOCK_RAW (raw protocols atop the network layer) protocol specifying the actual transport protocol to use. The most common are IPPROTO_TCP, IPPROTO_SCTP, IPPROTO_UDP, IPPROTO_DCCP. These protocols are specified in file <netinet/in.h>. The value 0 may be used to select a default protocol from the selected domain and type. The function returns -1 if an error occurred. Otherwise, it returns an integer representing the newly assigned descriptor.

bind bind() associates a socket with an address. When a socket is created with socket(), 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 sockaddr structure representing the address to bind to. addrlen, a field of type socklen_t specifying the size of the sockaddr structure. bind() returns 0 on success and -1 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.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Berkeley sockets

Start with the simplest possible case. Write down what Berkeley sockets claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Berkeley sockets before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Berkeley sockets ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Berkeley sockets

In research
Berkeley sockets appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Berkeley sockets in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Berkeley sockets is common in secondary-school and first-year university syllabi. It links to neighbouring topics Application programming interfaces, Berkeley Software Distribution, Network socket, so understanding it makes those chapters shorter.
In everyday life
Look for Berkeley sockets outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Berkeley sockets” →

How to study Berkeley sockets in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Berkeley sockets means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Berkeley sockets out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Berkeley sockets in simple terms?

Berkeley sockets is an application programming interface (API) for Internet domain sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules.

Why does Berkeley sockets matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Berkeley sockets?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Berkeley sockets.

Tags

  • Application programming interfaces
  • Berkeley Software Distribution
  • Network socket

Keep exploring