197 lines
11 KiB
HTML
197 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="Example: Generic client" />
|
||
|
<meta name="abstract" content="This code example contains the code for a common client job." />
|
||
|
<meta name="description" content="This code example contains the code for a common client job." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xcodesigns.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xip6client.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xcodesigns.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xiterative.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xdescriptors.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="x1descriptors.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xmultiaccept.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="x1mulitaccept.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/socket.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/connec.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/close.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="xasynchi0.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="generic" />
|
||
|
<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: Generic client</title>
|
||
|
</head>
|
||
|
<body id="generic"><a name="generic"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: Generic client</h1>
|
||
|
<div><p>This code example contains the code for a common client job.</p>
|
||
|
<div class="section"><p>The client job does a <span class="apiname">socket()</span>, <span class="apiname">connect()</span>, <span class="apiname">send()</span>, <span class="apiname">recv()</span>, and <span class="apiname">close()</span> operation. The client job is not aware that
|
||
|
the data buffer it sent and received is going to a worker job rather than
|
||
|
the server. If you want to create a client application that works whether
|
||
|
the server uses the AF_INET address family or AF_INET6 address family, use
|
||
|
the IPv4 or IPv6 client example.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>This client job works with each of these common connection-oriented
|
||
|
server designs:</p>
|
||
|
</div>
|
||
|
<div class="section"><ul><li>An iterative server. See <a href="xiterative.htm#xiterative">Example:
|
||
|
Write an iterative server program</a> for a sample program.</li>
|
||
|
<li>A spawn server and worker. See <a href="xspawn.htm#xspawn">Example:
|
||
|
Use the spawn() API to create child processes</a> for a sample program.</li>
|
||
|
<li>A sendmsg() server and rcvmsg() worker. See <a href="x1descriptors.htm#x1descriptors">Example:
|
||
|
Server program used for sendmsg() and recvmsg()</a> for a sample program.</li>
|
||
|
<li>A multiple accept() design. See <a href="x1mulitaccept.htm#x1mulitaccept">Example:
|
||
|
Server program to create a pool of multiple accept() worker jobs </a> for
|
||
|
a sample program.</li>
|
||
|
<li>A nonblocking I/O and select() design. See <a href="xnonblock.htm#xnonblock">Example:
|
||
|
Nonblocking I/O and select()</a> for a sample program.</li>
|
||
|
<li>A server that accepts connections from either an IPv4 or IPv6 client..
|
||
|
See <a href="xacceptboth.htm#xacceptboth">Example: Accept connections
|
||
|
from both IPv6 and IPv4 clients</a> for a sample program.</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Generic client</h4><p>The following
|
||
|
sample program 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 that the INET (Internet Protocol)
|
||
|
address family with the TCP transport (SOCK_STREAM) being used for this socket.</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 data buffer
|
||
|
to the worker job(s). </li>
|
||
|
<li>The <span class="apiname">recv()</span> function receives data buffer from the worker
|
||
|
job(s).</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>/**************************************************************************/
|
||
|
/* Generic client example is used with connection-oriented server designs */
|
||
|
/**************************************************************************/
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <netinet/in.h>
|
||
|
|
||
|
#define SERVER_PORT 12345
|
||
|
|
||
|
main (int argc, char *argv[])
|
||
|
{
|
||
|
int len, rc;
|
||
|
int sockfd;
|
||
|
char send_buf[80];
|
||
|
char recv_buf[80];
|
||
|
struct sockaddr_in addr;
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Create an AF_INET stream socket */
|
||
|
/*************************************************/
|
||
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
if (sockfd < 0)
|
||
|
{
|
||
|
perror("socket");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Initialize the socket address structure */
|
||
|
/*************************************************/
|
||
|
memset(&addr, 0, sizeof(addr));
|
||
|
addr.sin_family = AF_INET;
|
||
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||
|
addr.sin_port = htons(SERVER_PORT);
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Connect to the server */
|
||
|
/*************************************************/
|
||
|
rc = connect(sockfd,
|
||
|
(struct sockaddr *)&addr,
|
||
|
sizeof(struct sockaddr_in));
|
||
|
if (rc < 0)
|
||
|
{
|
||
|
perror("connect");
|
||
|
close(sockfd);
|
||
|
exit(-1);
|
||
|
}
|
||
|
printf("Connect completed.\n");
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Enter data buffer that is to be sent */
|
||
|
/*************************************************/
|
||
|
printf("Enter message to be sent:\n");
|
||
|
gets(send_buf);
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Send data buffer to the worker job */
|
||
|
/*************************************************/
|
||
|
len = send(sockfd, send_buf, strlen(send_buf) + 1, 0);
|
||
|
if (len != strlen(send_buf) + 1)
|
||
|
{
|
||
|
perror("send");
|
||
|
close(sockfd);
|
||
|
exit(-1);
|
||
|
}
|
||
|
printf("%d bytes sent\n", len);
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Receive data buffer from the worker job */
|
||
|
/*************************************************/
|
||
|
len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
|
||
|
if (len != strlen(send_buf) + 1)
|
||
|
{
|
||
|
perror("recv");
|
||
|
close(sockfd);
|
||
|
exit(-1);
|
||
|
}
|
||
|
printf("%d bytes received\n", len);
|
||
|
|
||
|
/*************************************************/
|
||
|
/* Close down the socket */
|
||
|
/*************************************************/
|
||
|
close(sockfd);
|
||
|
}</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="xip6client.htm" title="This sample program can be used with the server application that accepts requests from either IPv4 or IPv6 clients.">Example: IPv4 or IPv6 client</a></div>
|
||
|
<div><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><a href="xiterative.htm" title="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.">Example: Write an iterative server program</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><a href="x1descriptors.htm" title="This example shows how to use the sendmsg() API to create a pool of worker jobs.">Example: Server program used for sendmsg() and recvmsg()</a></div>
|
||
|
<div><a href="xmultiaccept.htm" title="These examples show how to design a server program that uses the multiple accept() model for handling incoming connection requests.">Examples: Use multiple accept() APIs to handle incoming requests</a></div>
|
||
|
<div><a href="x1mulitaccept.htm" title="This example shows how to use the multiple accept() model to create a pool of worker jobs.">Example: Server program to create a pool of multiple accept() worker jobs</a></div>
|
||
|
<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>
|
||
|
<div class="relinfo"><strong>Related information</strong><br />
|
||
|
<div><a href="../apis/socket.htm">socket()</a></div>
|
||
|
<div><a href="../apis/connec.htm">connect()</a></div>
|
||
|
<div><a href="../apis/close.htm">close()</a></div>
|
||
|
<div><a href="../apis/send.htm">send()</a></div>
|
||
|
<div><a href="../apis/recv.htm">recv()</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|