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.[1] 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),[2] 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 (computing)|path]]/[[filename]]
. For an Internet domain socket, the name is an [[IP address]]:[[Port (computer networking)|Port number]]
. In either case, the name is called an address.[3]
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 — spin waiting for 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
[4] and the server executes close
.[5]
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.[6]
Sockets first appeared in Berkeley Software Distribution 4.2 (1983).[7] It became a POSIX standard in 2000.[7] The application programming interface has been ported to virtually every Unix implementation and most other operating systems.[7]
Both the server and the client must instantiate a socket object by executing the socket
system call. Its usage is:[8]
The domain
parameter should be one of the following ranges of communication:[9]
AF_UNIX
AF_INET
AF_INET6
SOCK_SEQPACKET
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
.[10]
The type
parameter will probably be one of two common socket types: stream or datagram.[9] A third socket type is available for experimental design: raw.
SOCK_STREAM
will create a stream socket. A stream socket provides a reliable, bidirectional, and connection-oriented communication channel between two processes. Data are carried using the Transmission Control Protocol (TCP).[9]SOCK_DGRAM
will create a datagram socket. A Datagram socket does not guarantee reliability and is connectionless. As a result, the transmission is faster. Data are carried using the User Datagram Protocol (UDP).[11]SOCK_RAW
will create an Internet Protocol (IP) datagram socket. A Raw socket skips the TCP/UDP transport layer and sends the packets directly to the network layer.[12]For a Unix domain socket, data (network packets) are passed between two connected processes via the transport layer — either TCP or UDP.[13] For an Internet domain socket, data are passed between two connected processes via the transport layer and the Internet Protocol (IP) of the network layer — either TCP/IP or UDP/IP.[13]
The protocol
parameter should be set to zero for stream and datagram sockets.[2] For raw sockets, the protocol
parameter should be set to IPPROTO_RAW.[14]
Like the regular-file [[open (system call)|open]]
system call, the socket
system call returns a file descriptor.[2] The return value's suffix _fd
stands for file descriptor.
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:[15]
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.[15]
Computer program to create and bind a stream Unix domain socket:[6]
/* Should be 91 characters or less. Some Unix-like are slightly more. *//* Use /tmp directory for demonstration only. */ char *socket_address = "/tmp/mysocket.sock";
void main(void)
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.[16]
After binding to an address, the server opens a listen channel to a port by executing listen
. Its usage is:[17]
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
.[17]
The backlog
parameter sets the queue size for pending connections.[18] 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.[17]
After opening a listen channel, the server enters a spin wait. The accept
system call will return a file descriptor when a client process executes connect
.[19]
Snippet to accept a connection:
while (1)
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 (system call)|read]]
and [[write (system call)|write]]
.[5] 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
.[20]
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:
while (1)
void server_algorithmic_dialog(int accept_socket_fd)
The algorithmic dialog ends when either the algorithm concludes or read(accept_socket_fd)
returns < 1
.[5] To close the connection, execute the close
system call:[5]
Snippet to close a connection:
while (1)
Snippet to illustrate the end of a dialog:
void server_algorithmic_dialog(int accept_socket_fd)
Computer program for the client to instantiate and connect a socket:[4]
/* Must match the server's socket_address. */char *socket_address = "/tmp/mysocket.sock";
void main(void)
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:
void client_algorithmic_dialog(int client_socket_fd)