Unix domain socket

1

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. For an Internet domain socket, the name is an. 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 and the server executes. For a Unix domain socket, the socket's address is a identifier. The server will create 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 system call. Its usage is: The parameter should be one of the following common ranges of communication: The Unix domain socket label is used when the parameter's value is. The Internet domain socket label is used when the parameter's value is either or. The parameter should be one of two common socket types: stream or datagram. A third socket type is available for experimental design: raw. For a Unix domain socket, data (network packets) are passed between two connected processes via the transport layer — either TCP or UDP. 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. The parameter should be set to zero for stream and datagram sockets. For raw sockets, the parameter should be set to IPPROTO_RAW.

socket return value

Like the regular-file system call, the system call returns a file descriptor. The return value's suffix 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. Because the socket address may be either a or an , the socket application programming interface requires the address to first be set into a structure. For a Unix domain socket, the structure is: The suffix stands for unix. For an Internet domain socket, the suffix will be either or. The prefix stands for socket unix. Computer program to create and bind a stream Unix domain socket: The second parameter for is a pointer to. However, the parameter passed to the function is the address of a. is a generic structure that is not used. It is defined in the formal parameter declaration for. 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. Its usage is: Snippet to listen: For a Unix domain socket, most likely will succeed and return. For an Internet domain socket, if the port is in use, returns. The parameter sets the queue size for pending connections. The server may be busy when a client executes a 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, which puts itself to sleep. The system call will return a file descriptor when a client process executes. Snippet to accept a connection:

Server I/O on a socket

When 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 and. However, more control is available if a stream socket executes the socket-specific system calls of and. Alternatively, datagram socket input/output should execute the socket-specific system calls of and. For a basic stream socket, the server receives data with and sends data with. Snippet to illustrate I/O on a basic stream socket:

Server close a connection

The algorithmic dialog ends when either the algorithm concludes or returns. To close the connection, execute the 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 returns zero, the client can engage in an algorithmic dialog with the server. The client may send stream data via and may receive stream data via. Snippet to illustrate client I/O on a stream socket:

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article