Berkeley sockets

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.

Contents

The Header Files

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

<sys/socket.h>

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

<sys/types.h>

 Basic data types associated with structures within the BSD socket API

<netinet/in.h>

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

<sys/un.h>

 Definitions and data type declarations for SOCK_UNIX streams

The Client Side

socket()

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.

Prototype:

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

gethostbyname() and gethostbyaddr()

Prototypes:

struct hostent *gethostbyname(const char *name);

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

connect()

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.

Prototype:

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

The Server Side

socket()

Refer to description above

bind()

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.

Prototype:

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

listen()

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.

Prototype:

int listen(int sockfd, int backlog);

accept()

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).

Prototype:

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: (http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?Unix+manual+pages) *accept(2) (http://www.die.net/doc/linux/man/man2/accept.2.html) /connect(2) (http://www.die.net/doc/linux/man/man2/connect.2.html)
This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.
Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools