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

160 lines
8.2 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: Use gethostbyaddr_r() for threadsafe network routines" />
<meta name="abstract" content="Here is an example of a program that uses gethostbyaddr_r()." />
<meta name="description" content="Here is an example of a program that uses gethostbyaddr_r()." />
<meta name="DC.Relation" scheme="URI" content="example.htm" />
<meta name="DC.Relation" scheme="URI" content="cthread.htm" />
<meta name="DC.Relation" scheme="URI" content="routines.htm" />
<meta name="DC.Relation" scheme="URI" content="../apis/tsghosta.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="xthread" />
<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: Use gethostbyaddr_r() for threadsafe network
routines</title>
</head>
<body id="xthread"><a name="xthread"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Use <span class="apiname">gethostbyaddr_r()</span> for threadsafe network
routines</h1>
<div><p>Here is an example of a program that uses <span class="apiname">gethostbyaddr_r()</span>.</p>
<div class="section"><p>All other routines with names that end in "_r" have similar semantics
and are also threadsafe. This example program takes an IP address in the dotted-decimal
notation and prints the host name.</p>
</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>
</div>
<div class="example"> <pre>/********************************************************/
/* Header files */
/********************************************************/
#include &lt;/netdb.h&gt;
#include &lt;sys/param.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;sys/socket.h&gt;
#define HEX00 '\x00'
#define NUMPARMS 2
/********************************************************/
/* Pass one parameter that is the IP address in */
/* dotted decimal notation. The host name will be */
/* displayed if found; otherwise, a message states */
/* host not found. */
/********************************************************/
int main(int argc, char *argv[])
{
int rc;
struct in_addr internet_address;
struct hostent hst_ent;
struct hostent_data hst_ent_data;
char dotted_decimal_address [16];
char host_name[MAXHOSTNAMELEN];
/**********************************************************/
/* Verify correct number of arguments have been passed */
/**********************************************************/
if (argc != NUMPARMS)
{
printf("Wrong number of parms passed\n");
exit(-1);
}
/**********************************************************/
/* Obtain addressability to parameters passed */
/**********************************************************/
strcpy(dotted_decimal_address, argv[1]);
/**********************************************************/
/* Initialize the structure-field */
/* hostent_data.host_control_blk with hexadecimal zeros */
/* before its initial use. If you require compatibility */
/* with other platforms, then you must initialize the */
/* entire hostent_data structure with hexadecimal zeros. */
/**********************************************************/
/* Initialize to hex 00 hostent_data structure */
/**********************************************************/
memset(&amp;hst_ent_data,HEX00,sizeof(struct hostent_data));
/**********************************************************/
/* Translate an IP address from dotted decimal */
/* notation to 32-bit IP address format. */
/**********************************************************/
internet_address.s_addr=inet_addr(dotted_decimal_address);
/**********************************************************/
/* Obtain host name */
/**********************************************************/
/**********************************************************/
/* NOTE: The gethostbyaddr_r() returns an integer. */
/* The following are possible values: */
/* -1 (unsuccessful call) */
/* 0 (successful call) */
/**********************************************************/
rc=gethostbyaddr_r((char *) &amp;internet_address,
sizeof(struct in_addr), AF_INET,
&amp;hst_ent, &amp;hst_ent_data);
if (rc== -1)
{
printf("Host name not found\n");
exit(-1);
}
else
{
/*****************************************************/
/* Copy the host name to an output buffer */
/*****************************************************/
(void) memcpy((void *) host_name,
/****************************************************/
/* You must address all the results through the */
/* hostent structure hst_ent. */
/* NOTE: Hostent_data structure hst_ent_data is just */
/* a data repository that is used to support the */
/* hostent structure. Applications should consider */
/* hostent_data a storage area to put host level data */
/* that the application does not need to access. */
/****************************************************/
(void *) hst_ent.h_name,
MAXHOSTNAMELEN);
/*****************************************************/
/* Print the host name */
/*****************************************************/
printf("The host name is %s\n", host_name);
}
exit(0);
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="example.htm" title="These examples provide many sample programs that illustrate the more advanced socket concepts. You can use these sample programs to create your own applications that complete a similar task.">Examples: Socket application designs</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="cthread.htm" title="A function is considered threadsafe if you can start it simultaneously in multiple threads within the same process.">Thread safety</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="routines.htm" title="Socket network functions allow application programs to obtain information from the host, protocol, service, and network files.">Socket network functions</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../apis/tsghosta.htm">gethostbyaddr_r()</a></div>
</div>
</div>
</body>
</html>