# Unix domain socket

In client-server computing, a Unix domain socket is a Berkeley socket that allows data to be exchanged between two processes executing on the same Unix or Unix-like host computer. This is similar to an Internet domain socket that allows data to be exchanged between two processes executing on different host computers.
Regardless of the range of communication (same host or different host), Unix computer programs that perform socket communication are similar. The only range of communication difference is the method to convert a name to the address parameter needed to bind the socket's connection. For a Unix domain socket, the name is a /path/filename. For an Internet domain socket, the name is an IP address:Port number. In either case, the name is called an address.
Two processes may communicate with each other if each obtains a socket. The server process binds its socket to an address, opens a listen channel, and then continuously loops. Inside the loop, the server process is put to sleep while waiting to accept a client connection. Upon accepting a client connection, the server then executes a read system call that will block wait. The client connects to the server's socket via the server's address. The client process then writes a message for the server process to read. The application's algorithm may entail multiple read/write interactions. Upon completion of the algorithm, the client executes exit and the server executes close.
For a Unix domain socket, the socket's address is a /path/filename identifier. The server will create /path/filename on the filesystem to act as a lock file semaphore. No I/O occurs on this file when the client and server send messages to each other.

## History

Sockets first appeared in Berkeley Software Distribution 4.2 (1983). It became a POSIX standard in 2000. The application programming interface has been ported to virtually every Unix implementation and most other operating systems.

## Socket instantiation

Both the server and the client must instantiate a socket object by executing the socket system call. Its usage is
The domain parameter should be one of the following common ranges of communication:
* Within the same host by using the constant AF_UNIX
* Between two hosts via the IPv4 protocol by using the constant AF_INET
* Between two hosts via the IPv6 protocol by using the constant AF_INET6
The Unix domain socket label is used when the domain parameter's value is AF_UNIX. The Internet domain socket label is used when the domain parameter's value is either AF_INET or AF_INET6.
The type parameter should be one of following common socket types:
* SOCK_STREAM will create a stream socket. A stream socket provides a reliable, bidirectional, and connection-oriented communication channel between two processes. For internet domain sockets, data is carried using the Transmission Control Protocol (TCP).
* SOCK_DGRAM will create a datagram socket. A datagram socket is connectionless and preserves message boundaries. For internet domain sockets, data is carried using the User Datagram Protocol (UDP).
* SOCK_SEQPACKET will create a sequenced-packet socket. Similar to a stream socket, it is connection-oriented, but message boundaries are preserved, just like datagram sockets. For internet domain sockets, the Stream Control Transmission Protocol is used.
* SOCK_RAW will create a raw Internet Protocol (IP) datagram socket. A raw socket bypasses the transport layer and allows applications to interface directly with the network layer. This option is only available for internet domain sockets.
The protocol parameter should be set to zero, except for raw sockets, where the protocol parameter should be set to IPPROTO_RAW.

### socket return value

Like the regular-file open system call, the socket system call returns a file descriptor. The return value's suffix _fd stands for file descriptor.

## Server bind to /path/filename

After instantiating a new socket, the server binds the socket to an address. For a Unix domain socket, the address is a /path/filename.
Because the socket address may be either a /path/filename or an IP_address:Port_number, the socket application programming interface requires the address to first be set into a structure. For a Unix domain socket, the structure is
The _un suffix stands for unix. For an Internet domain socket, the suffix will be either _in or _in6. The sun_ prefix stands for socket unix.
Computer program to create and bind a stream Unix domain socket:
The second parameter for bind is a pointer to struct sockaddr. However, the parameter passed to the function is the address of a struct sockaddr_un. struct sockaddr is a generic structure that is not used. It is defined in the formal parameter declaration for bind. Because each range of communication has its own actual parameter, this generic structure was created as a cast placeholder.

## Server listen for a connection

After binding to an address, the server opens a listen channel to a port by executing listen. Its usage is
Snippet to listen:
For a Unix domain socket, listen most likely will succeed and return 0. For an Internet domain socket, if the port is in use, listen returns -1.
The backlog parameter sets the queue size for pending connections. The server may be busy when a client executes a connect request. Connection requests up to this limit will succeed. If the backlog value passed in exceeds the default maximum, then the maximum value is used.

## Server accept a connection

After opening a listen channel, the server enters an infinite loop. Inside the loop is a system call to accept, which puts itself to sleep. The accept system call will return a file descriptor when a client process executes connect.
Snippet to accept a connection:

## Server I/O on a socket

When accept returns a positive integer, the server engages in an algorithmic dialog with the client.
Stream socket input/output may execute the regular-file system calls of read and write. However, more control is available if a stream socket executes the socket-specific system calls of send and recv. Alternatively, datagram socket input/output should execute the socket-specific system calls of sendto and recvfrom.
For a basic stream socket, the server receives data with read( accept_socket_fd ) and sends data with write( accept_socket_fd ).
Snippet to illustrate I/O on a basic stream socket:

## Server close a connection

The algorithmic dialog ends when either the algorithm concludes or read( accept_socket_fd ) returns < 1. To close the connection, execute the close system call:
Snippet to close a connection:
Snippet to illustrate the end of a dialog:

## Client instantiate and connect to /path/filename

Computer program for the client to instantiate and connect a socket:

## Client I/O on a socket

If connect returns zero, the client can engage in an algorithmic dialog with the server. The client may send stream data via write( client_socket_fd ) and may receive stream data via read( client_socket_fd ).
Snippet to illustrate client I/O on a stream socket: