162 lines
11 KiB
HTML
162 lines
11 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Use AF_UNIX address family" />
|
||
|
<meta name="abstract" content="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)." />
|
||
|
<meta name="description" content="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)." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="uaddrfam.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xafunixsrv.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xafunixcl.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="cafunix.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="prerequisites.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/close.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/socket.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/bind.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/unlink.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/listen.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/send.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/recv.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/socketp.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/connec.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="uafunixccsid.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2001, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2001, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="uafunix" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Use AF_UNIX address family</title>
|
||
|
</head>
|
||
|
<body id="uafunix"><a name="uafunix"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Use AF_UNIX address family</h1>
|
||
|
<div><p>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). </p>
|
||
|
<div class="section"><p>Both types are reliable because there are no external communication
|
||
|
functions connecting the two processes. </p>
|
||
|
</div>
|
||
|
<div class="section"><p>UNIX<sup>®</sup> domain datagram sockets act differently
|
||
|
than UDP datagram sockets. With UDP datagram sockets, the client program does
|
||
|
not need to call the <span class="apiname">bind()</span> 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 <span class="apiname">bind()</span> function.
|
||
|
The exact path name specified on the client's <span class="apiname">bind()</span> 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.</p>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">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. <div class="note"><span class="notetitle">Note:</span> Path names in the file system
|
||
|
are NLS-enabled. </div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>The following figure illustrates the client/server relationship
|
||
|
of the AF_UNIX address family. </p>
|
||
|
</div>
|
||
|
<div class="section"><p><br /><img src="rzab6503.gif" alt="Socket flow of events used in server and client AF_UNIX address family example programs." /><br /></p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Server application that uses AF_UNIX
|
||
|
address family</h4><p>The first example uses the following sequence of
|
||
|
function calls:</p>
|
||
|
<ol><li>The <span class="apiname">socket()</span> 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 <span class="apiname">socketpair()</span> function
|
||
|
to initialize a UNIX socket. <p>AF_UNIX or AF_UNIX_CCSID are the only
|
||
|
address families to support the <span class="apiname">socketpair()</span> function.
|
||
|
The <span class="apiname">socketpair()</span> function returns two socket descriptors
|
||
|
that are unnamed and connected.</p>
|
||
|
</li>
|
||
|
<li>After the socket descriptor is created, the <span class="apiname">bind()</span> function
|
||
|
gets a unique name for the socket. <p>The name space for UNIX domain sockets
|
||
|
consists of path names. When a sockets program calls the <span class="apiname">bind()</span> function,
|
||
|
an entry is created in the file system directory. If the path name already
|
||
|
exists, the <span class="apiname">bind()</span> fails. Thus, a UNIX domain socket
|
||
|
program should always call an <span class="apiname">unlink()</span> functions to remove
|
||
|
the directory entry when it ends. </p>
|
||
|
</li>
|
||
|
<li>The <span class="apiname">listen()</span> 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.</li>
|
||
|
<li>The <span class="apiname">recv()</span> 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 <span class="apiname">recv()</span> is
|
||
|
not required to wake up until all 250 bytes of data have arrived. </li>
|
||
|
<li>The <span class="apiname">send()</span> function echoes the data back to the client. </li>
|
||
|
<li>The <span class="apiname">close()</span> function closes any open socket descriptors.</li>
|
||
|
<li>The <span class="apiname">unlink()</span> function removes the UNIX path name
|
||
|
from the file system. </li>
|
||
|
</ol>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Client application that uses AF_UNIX
|
||
|
address family</h4><p>The second example uses the following sequence of
|
||
|
function calls:</p>
|
||
|
<ol><li>The <span class="apiname">socket()</span> 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 <span class="apiname">socketpair()</span> function
|
||
|
to initialize a UNIX socket. <p>AF_UNIX or AF_UNIX_CCSID are the only
|
||
|
address families to support the <span class="apiname">socketpair()</span> function.
|
||
|
The <span class="apiname">socketpair()</span> function returns two socket descriptors
|
||
|
that are unnamed and connected.</p>
|
||
|
</li>
|
||
|
<li>After the socket descriptor is received, the <span class="apiname">connect()</span> function
|
||
|
is used to establish a connection to the server.</li>
|
||
|
<li>The <span class="apiname">send()</span> function sends 250 bytes of data that are
|
||
|
specified in the server application with the SO_RCVLOWAT socket option. </li>
|
||
|
<li>The <span class="apiname">recv()</span> function loops until all 250 bytes of the
|
||
|
data have arrived. </li>
|
||
|
<li>The <span class="apiname">close()</span> function closes any open socket descriptors.</li>
|
||
|
</ol>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<ul class="ullinks">
|
||
|
<li class="ulchildlink"><strong><a href="xafunixsrv.htm">Example: Server application that uses AF_UNIX address family</a></strong><br />
|
||
|
This example provides a sample program that uses the AF_UNIX address family. The AF_UNIX address family uses many of the same socket calls as other address families, except that it uses the path name structure to identify the server application.</li>
|
||
|
<li class="ulchildlink"><strong><a href="xafunixcl.htm">Example: Client application that uses AF_UNIX address family</a></strong><br />
|
||
|
This example provides a sample client application for the AF_UNIX address family. The AF_UNIX address family uses many of the same socket calls as other address families, except that it uses the path name structure to identify the server application.</li>
|
||
|
</ul>
|
||
|
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="uaddrfam.htm" title="This topic includes sample programs that illustrate each of the socket address families.">Design applications with address families</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="cafunix.htm" title="This address family provides interprocess communications on the same system that uses the socket APIs. The address is really a path name to an entry in the file system.">AF_UNIX address family</a></div>
|
||
|
<div><a href="prerequisites.htm" title="Before writing socket applications, you must first complete these steps.">Prerequisites for socket programming</a></div>
|
||
|
<div><a href="uafunixccsid.htm" title="AF_UNIX_CCSID address family sockets have the same specifications as AF_UNIX address family sockets. They can be used for connection-oriented or connectionless and provide communications on the same system.">Use AF_UNIX_CCSID address family</a></div>
|
||
|
</div>
|
||
|
<div class="relinfo"><strong>Related information</strong><br />
|
||
|
<div><a href="../apis/close.htm">close()</a></div>
|
||
|
<div><a href="../apis/socket.htm">socket()</a></div>
|
||
|
<div><a href="../apis/bind.htm">bind()</a></div>
|
||
|
<div><a href="../apis/unlink.htm">unlink()</a></div>
|
||
|
<div><a href="../apis/listen.htm">listen()</a></div>
|
||
|
<div><a href="../apis/send.htm">send()</a></div>
|
||
|
<div><a href="../apis/recv.htm">recv()</a></div>
|
||
|
<div><a href="../apis/socketp.htm">socketpair()</a></div>
|
||
|
<div><a href="../apis/connec.htm">connect()</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|