Use AF_UNIX address family

The AF_UNIX address family (sockets using the AF_UNIX or AF_UNIX_CCSID address families) can be connection-oriented (type SOCK_STREAM) or they can be connectionless (type SOCK_DGRAM).

Both types are reliable because there are no external communication functions connecting the two processes.

UNIX® domain datagram sockets act differently than UDP datagram sockets. With UDP datagram sockets, the client program does not need to call the bind() function because the system assigns an unused port number automatically. The server can then send a datagram back to that port number. However, with UNIX domain datagram sockets, the system does not automatically assign a path name for the client. Thus, all client programs using UNIX domain datagrams must call the bind() function. The exact path name specified on the client's bind() is what is passed to the server. Thus, if the client specifies a relative path name (that is, a path name that is not fully qualified by starting with /), the server cannot send the client a datagram unless it is running with the same current directory.

An example path name that an application might use for this address family is /tmp/myserver or servers/thatserver. With servers/thatserver, you have a path name that is not fully qualified (no / was specified). This means that the location of the entry in the file system hierarchy should be determined relative to the current working directory.
Note: Path names in the file system are NLS-enabled.

The following figure illustrates the client/server relationship of the AF_UNIX address family.


Socket flow of events used in server and client AF_UNIX address family example programs.

Socket flow of events: Server application that uses AF_UNIX address family

The first example uses the following sequence of function calls:

  1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies the UNIX address family with the stream transport (SOCK_STREAM) being used for this socket. The function returns a socket descriptor representing an endpoint. You can also use the socketpair() function to initialize a UNIX socket.

    AF_UNIX or AF_UNIX_CCSID are the only address families to support the socketpair() function. The socketpair() function returns two socket descriptors that are unnamed and connected.

  2. After the socket descriptor is created, the bind() function gets a unique name for the socket.

    The name space for UNIX domain sockets consists of path names. When a sockets program calls the bind() function, an entry is created in the file system directory. If the path name already exists, the bind() fails. Thus, a UNIX domain socket program should always call an unlink() functions to remove the directory entry when it ends.

  3. The listen() allows the server to accept incoming client connections. In this example, the backlog is set to 10. This means that the system queues 10 incoming connection requests before the system starts rejecting the incoming requests.
  4. The recv() function receives data from the client application. In this example, the client sends 250 bytes of data over. Thus SO_RCVLOWAT socket option can be used, specifying that recv() is not required to wake up until all 250 bytes of data have arrived.
  5. The send() function echoes the data back to the client.
  6. The close() function closes any open socket descriptors.
  7. The unlink() function removes the UNIX path name from the file system.

Socket flow of events: Client application that uses AF_UNIX address family

The second example uses the following sequence of function calls:

  1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies the UNIX address family with the stream transport (SOCK_STREAM) being used for this socket. The function returns a socket descriptor representing an endpoint. You can also use the socketpair() function to initialize a UNIX socket.

    AF_UNIX or AF_UNIX_CCSID are the only address families to support the socketpair() function. The socketpair() function returns two socket descriptors that are unnamed and connected.

  2. After the socket descriptor is received, the connect() function is used to establish a connection to the server.
  3. The send() function sends 250 bytes of data that are specified in the server application with the SO_RCVLOWAT socket option.
  4. The recv() function loops until all 250 bytes of the data have arrived.
  5. The close() function closes any open socket descriptors.
Related reference
AF_UNIX address family
Prerequisites for socket programming
Use AF_UNIX_CCSID address family
Related information
close()
socket()
bind()
unlink()
listen()
send()
recv()
socketpair()
connect()