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

230 lines
10 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="Example: Write an iterative server program" />
<meta name="abstract" content="Use this example to create a single server job that handles all incoming connections. When the accept() API is completed, the server handles the entire transaction." />
<meta name="description" content="Use this example to create a single server job that handles all incoming connections. When the accept() API is completed, the server handles the entire transaction." />
<meta name="DC.Relation" scheme="URI" content="xcodesigns.htm" />
<meta name="DC.Relation" scheme="URI" content="generic.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/recv.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/bind.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/socket.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/listen.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/accept.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/send.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/close.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="xiterative" />
<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>Example: Write an iterative server program</title>
</head>
<body id="xiterative"><a name="xiterative"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Write an iterative server program</h1>
<div><p>Use this example to create a single server job that handles all
incoming connections. When the <span class="apiname">accept()</span> API is completed,
the server handles the entire transaction.</p>
<div class="section"><p>The figure illustrates how the server and client jobs interact
when the system used the iterative server design. </p>
</div>
<div class="section"><p><br /><img src="rxab6505.gif" alt="Server and client job interaction when using iterative server design." /><br /></p>
</div>
<div class="section"></div>
<div class="section"><h4 class="sectiontitle">Flow of socket events: Iterative server</h4><p>The
following sequence of the socket calls provide a description of the graphic.
It also describes the relationship between the server and worker applications.
Each set of flows contain links to usage notes on specific APIs. If you need
more details on the use of a particular API, you can use these links. The
following sequence shows the function calls for the iterative server application:</p>
<ol><li>The <span class="apiname">socket()</span> function returns a socket descriptor representing
an endpoint. The statement also identifies that the INET (Internet Protocol)
address family with the TCP transport (SOCK_STREAM) is used for this socket. </li>
<li>After the socket descriptor is created, the <span class="apiname">bind()</span> function
gets a unique name for the socket. </li>
<li>The <span class="apiname">listen()</span> allows the server to accept incoming client
connections. </li>
<li>The server uses the <span class="apiname">accept()</span> function to accept an
incoming connection request. The <span class="apiname">accept()</span> call blocks indefinitely
waiting for the incoming connection to arrive.</li>
<li>The <span class="apiname">recv()</span> function receives data from the client application. </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>
</ol>
</div>
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
<pre>/**************************************************************************/
/* Application creates an iterative server design */
/**************************************************************************/
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#define SERVER_PORT 12345
main (int argc, char *argv[])
{
int i, len, num, rc, on = 1;
int listen_sd, accept_sd;
char buffer[80];
struct sockaddr_in addr;
/*************************************************/
/* If an argument was specified, use it to */
/* control the number of incoming connections */
/*************************************************/
if (argc &gt;= 2)
num = atoi(argv[1]);
else
num = 1;
/*************************************************/
/* Create an AF_INET stream socket to receive */
/* incoming connections on */
/*************************************************/
listen_sd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sd &lt; 0)
{
perror("socket() failed");
exit(-1);
}
/*************************************************/
/* Allow socket descriptor to be reuseable */
/*************************************************/
rc = setsockopt(listen_sd,
SOL_SOCKET, SO_REUSEADDR,
(char *)&amp;on, sizeof(on));
if (rc &lt; 0)
{
perror("setsockopt() failed");
close(listen_sd);
exit(-1);
}
/*************************************************/
/* Bind the socket */
/*************************************************/
memset(&amp;addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(SERVER_PORT);
rc = bind(listen_sd,
(struct sockaddr *)&amp;addr, sizeof(addr));
if (rc &lt; 0)
{
perror("bind() failed");
close(listen_sd);
exit(-1);
}
/*************************************************/
/* Set the listen back log */
/*************************************************/
rc = listen(listen_sd, 5);
if (rc &lt; 0)
{
perror("listen() failed");
close(listen_sd);
exit(-1);
}
/*************************************************/
/* Inform the user that the server is ready */
/*************************************************/
printf("The server is ready\n");
/*************************************************/
/* Go through the loop once for each connection */
/*************************************************/
for (i=0; i &lt; num; i++)
{
/**********************************************/
/* Wait for an incoming connection */
/**********************************************/
printf("Interation: %d\n", i+1);
printf(" waiting on accept()\n");
accept_sd = accept(listen_sd, NULL, NULL);
if (accept_sd &lt; 0)
{
perror("accept() failed");
close(listen_sd);
exit(-1);
}
printf(" accept completed successfully\n");
/**********************************************/
/* Receive a message from the client */
/**********************************************/
printf(" wait for client to send us a message\n");
rc = recv(accept_sd, buffer, sizeof(buffer), 0);
if (rc &lt;= 0)
{
perror("recv() failed");
close(listen_sd);
close(accept_sd);
exit(-1);
}
printf(" &lt;%s&gt;\n", buffer);
/**********************************************/
/* Echo the data back to the client */
/**********************************************/
printf(" echo it back\n");
len = rc;
rc = send(accept_sd, buffer, len, 0);
if (rc &lt;= 0)
{
perror("send() failed");
close(listen_sd);
close(accept_sd);
exit(-1);
}
/**********************************************/
/* Close down the incoming connection */
/**********************************************/
close(accept_sd);
}
/*************************************************/
/* Close down the listen socket */
/*************************************************/
close(listen_sd);
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="xcodesigns.htm" title="There are a number of ways that you can design a connection-oriented socket server on the iSeries. These example programs can be used to create your own connection-oriented designs.">Examples: Connection-oriented designs</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="generic.htm" title="This code example contains the code for a common client job.">Example: Generic client</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../apis/recv.htm">recv()</a></div>
<div><a href="../apis/bind.htm">bind()</a></div>
<div><a href="../apis/socket.htm">socket()</a></div>
<div><a href="../apis/listen.htm">listen()</a></div>
<div><a href="../apis/accept.htm">accept()</a></div>
<div><a href="../apis/send.htm">send()</a></div>
<div><a href="../apis/close.htm">close()</a></div>
</div>
</div>
</body>
</html>