ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzab6_5.4.0.1/designrec.htm

214 lines
16 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="Socket application design recommendations" />
<meta name="abstract" content="Before working with socket applications, assess the functional requirements, goals, and needs of the socket application." />
<meta name="description" content="Before working with socket applications, assess the functional requirements, goals, and needs of the socket application." />
<meta name="DC.Relation" scheme="URI" content="rzab6soxoverview.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/dossrmfh.htm" />
<meta name="DC.Relation" scheme="URI" content="xasynchi0.htm" />
<meta name="DC.Relation" scheme="URI" content="asynchi0.htm" />
<meta name="DC.Relation" scheme="URI" content="xnonblock.htm" />
<meta name="DC.Relation" scheme="URI" content="xdescriptors.htm" />
<meta name="DC.Relation" scheme="URI" content="xnonblock.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="designrec" />
<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>Socket application design recommendations</title>
</head>
<body id="designrec"><a name="designrec"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Socket application design recommendations</h1>
<div><p>Before working with socket applications, assess the functional
requirements, goals, and needs of the socket application.</p>
<div class="section"><p>Consider performance requirements and system resource impacts
of the application too. The following list of recommendations helps you address
some of these issues for your socket application and points out
better ways to use sockets and to design your socket applications:</p>
</div>
<div class="section">
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="border" border="1" rules="all"><caption>Table 1. Socket design applications</caption><thead align="left"><tr><th valign="top" width="33.33333333333333%" id="d0e28">Recommendation</th>
<th valign="top" width="33.33333333333333%" id="d0e30">Reason</th>
<th valign="top" width="33.33333333333333%" id="d0e32">Best used in</th>
</tr>
</thead>
<tbody><tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use asynchronous I/O</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Asynchronous I/O used in a threaded server model is
preferable over the more conventional select() model.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Socket server applications which handle numerous
concurrent clients.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">When using asynchronous I/O, adjust the number of threads
in the process to an optimum number for the number of clients to be processed.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">If too few threads are defined then some clients might
time out before being handled. If too many threads are defined then some system
resource are not used efficiently. <div class="note"><span class="notetitle">Note:</span> It is better to have too many
threads than too few threads. </div>
</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Socket applications using asynchronous I/O. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Design socket application to avoid the use of the postflag
on all start operations for asynchronous I/O.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Avoids the performance overhead of transition to a completion
port if the operation has already been satisfied synchronously.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Socket applications using asynchronous I/O. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use <span class="apiname">send()</span> and <span class="apiname">recv()</span> over <span class="apiname">read()</span> and <span class="apiname">write()</span>.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 "><span class="apiname">send()</span> and <span class="apiname">recv()</span> APIs
provide a small performance and serviceability improvement over <span class="apiname">read()</span> and <span class="apiname">write()</span>.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket program that knows it uses a socket descriptor
and not a file descriptor.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use the receive low water (SO_RCVLOWAT) socket option
to avoid looping on a receive operation until all data has arrived. </td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Allows your application to wait for a minimum amount
of data to be received on the socket before satisfying a blocked receive operation.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application that receives data</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use the MSG_WAITALL flag to avoid looping on a receive
operation until all data has arrived.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Allows your application to wait for the entire buffer
provided on the receive operation to be received before satisfying a blocked
receive operation.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application that receives data and knows
in advance how much it expects to arrive.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use <span class="apiname">sendmsg()</span> and <span class="apiname">recvmsg()</span> over <span class="apiname">givedescriptor()</span> and <span class="apiname">takedescriptor()</span>.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">See <a href="cdescriptors.htm#cdescriptors">Descriptor passing between processes—sendmsg() and recvmsg()</a> for
the advantages. </td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application passing socket or file descriptors
between processes. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">When using <span class="apiname">select()</span>, try to avoid
a large number of descriptors in the read, write or exception set. <div class="note"><span class="notetitle">Note:</span> If
you have a large number of descriptors being used for <span class="apiname">select()</span> processing
see the asynchronous I/O recommendation above.</div>
</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">When there are a large number of descriptors in a read,
write or exception set, considerable redundant work occurs each time <span class="apiname">select()</span> is
called. As soon as a <span class="apiname">select()</span> is satisfied the actual socket
function must still be done, that is, a read or write or accept must still
be performed. Asynchronous I/O APIs combine the notification that something
has occurred on a socket with the actual I/O operation.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Applications that have a large(&gt; 50) number of
descriptors active for <span class="apiname">select()</span>. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Save your copy of the read, write and exception sets
before using <span class="apiname">select()</span> to avoid rebuilding the sets for
every time you must reissue the <span class="apiname">select()</span>. </td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">This saves the overhead of rebuilding the read, write,
or exception sets every time you plan to issue the <span class="apiname">select()</span>.
</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application where you are using <span class="apiname">select()</span> with
a large number of socket descriptors enabled for read, write or exception
processing. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Do not use <span class="apiname">select()</span> as a timer. Use <span class="apiname">sleep()</span> instead.
<div class="note"><span class="notetitle">Note:</span> If granularity of the <span class="apiname">sleep()</span> timer
is not adequate, you might need to use <span class="apiname">select()</span> as a timer.
In this case, set maximum descriptor to 0 and the read, write, and exception
set to NULL.</div>
</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Better timer response and less system overhead.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application where you are using <span class="apiname">select()</span> just
as a timer. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">If your socket application has increased the maximum
number of file and socket descriptors allowed per process using DosSetRelMaxFH()
and you are using <span class="apiname">select()</span> in this same application, be
careful of the affect this new maximum value has on the size of the read,
write and exception sets used for <span class="apiname">select()</span> processing. </td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">If you allocate a descriptor outside the range of the
read, write or exception set, as specified by FD_SETSIZE, then you can overwrite
and destroy storage. Ensure your set sizes are at least large enough to handle
whatever the maximum number of descriptors are set for the process and the
maximum descriptor value specified on the <span class="apiname">select()</span> API.
</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any application or process where you use DosSetRelMaxFH()
and <span class="apiname">select()</span>. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Set all socket descriptors in the read or write sets
to nonblocking. When a descriptor becomes enabled for read or write, loop
and consume or send all of the data until EWOULDBLOCK is returned. </td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">This allows you to minimize the number of <span class="apiname">select()</span> calls
when data is still available to be processed or read on a descriptor. </td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application where you are using <span class="apiname">select()</span>.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Only specify the sets that you need to use for <span class="apiname">select()</span> processing.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Most applications do not need to specify the exception
set or write set.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application where you are using <span class="apiname">select()</span>.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 "><p>Use GSKit APIs instead of SSL_APIs.</p>
</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 "><p>Both the Global Secure Toolkit (GSKit)
and i5/OS™ SSL_
APIs allow you to develop secure AF_INET or AF_INET6, SOCK_STREAM socket applications.
Because the GSKit APIs are supported across IBM<sup>®</sup> systems, they are the preferred APIs
to secure an application. The SSL_ APIs only exist in the i5/OS operating
system.</p>
</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application which needs to be enabled
for SSL/TLS processing.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Avoid using signals.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">The performance overhead of signals (on all platforms,
not just the iSeries™)
is expensive. It is better to design your socket application to use Asynchronous
I/O or <span class="apiname">select()</span> APIs.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any programmer considering the use of signals in
their socket application. </td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use protocol independent routines when available, such
as <span class="apiname">inet_ntop()</span>, <span class="apiname">inet_pton()</span>, <span class="apiname">getaddrinfo()</span>,
and <span class="apiname">getnameinfo()</span>.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Even if you are not yet ready to support IPv6, use these
APIs, (instead of <span class="apiname">inet_ntoa()</span>, <span class="apiname">inet_addr()</span>, <span class="apiname">gethostbyname()</span> and <span class="apiname">gethostbyaddr</span>()) to prepare you for easier migration.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any AF_INET or AF_INET6 application that uses network
routines.</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e28 ">Use sockaddr_storage to declare storage for any address
family address.</td>
<td valign="top" width="33.33333333333333%" headers="d0e30 ">Simplifies writing code portable across multiple address
families and platforms. Declares enough storage to hold the largest address
family and ensures the correct boundary alignment.</td>
<td valign="top" width="33.33333333333333%" headers="d0e32 ">Any socket application that stores addresses.</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzab6soxoverview.htm" title="A socket is a communications connection point (endpoint) that you can name and address in a network.">Socket programming</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="asynchi0.htm" title="Asynchronous I/O APIs provide a method for threaded client server models to perform highly concurrent and memory efficient I/O.">Asynchronous I/O</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="xasynchi0.htm" title="An application creates an I/O completion port using the QsoCreateIOCompletionPort() API.">Example: Use asynchronous I/O</a></div>
<div><a href="xnonblock.htm" title="This sample program uses nonblocking and the select() API.">Example: Nonblocking I/O and select()</a></div>
<div><a href="xdescriptors.htm" title="The sendmsg() and recvmsg() examples show how to design a server program that uses these APIs to handle incoming connections.">Example: Pass descriptors between processes</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../apis/dossrmfh.htm">DosSetRelMaxFH()</a></div>
</div>
</div>
</body>
</html>