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

210 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: A connection-oriented client" />
<meta name="abstract" content="This example shows how to create a socket client program to connect to a connection-oriented server in a connection-oriented design." />
<meta name="description" content="This example shows how to create a socket client program to connect to a connection-oriented server in a connection-oriented design." />
<meta name="DC.Relation" scheme="URI" content="connectionor.htm" />
<meta name="DC.Relation" scheme="URI" content="uafinet.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="xconoclient" />
<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: A connection-oriented client</title>
</head>
<body id="xconoclient"><a name="xconoclient"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: A connection-oriented client</h1>
<div><p>This example shows how to create a socket client program to connect
to a connection-oriented server in a connection-oriented design.</p>
<div class="section"><p>The client of the service (the client program) must request the
service of the server program. You can use this code example to write your
own client application.</p>
<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="section"> <pre>/**************************************************************************/
/* This sample program provides a code for a connection-oriented client. */
/**************************************************************************/
/**************************************************************************/
/* Header files needed for this sample program */
/**************************************************************************/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
/**************************************************************************/
/* Constants used by this program */
/**************************************************************************/
#define SERVER_PORT 3005
#define BUFFER_LENGTH 250
#define FALSE 0
#define SERVER_NAME "ServerHostName"
/* Pass in 1 parameter which is either the */
/* address or host name of the server, or */
/* set the server name in the #define */
/* SERVER_NAME. */
void main(int argc, char *argv[])
{
/***********************************************************************/
/* Variable and structure definitions. */
/***********************************************************************/
int sd=-1, rc, bytesReceived;
char buffer[BUFFER_LENGTH];
char server[NETDB_MAX_HOST_NAME_LENGTH];
struct sockaddr_in serveraddr;
struct hostent *hostp;
/***********************************************************************/
/* A do/while(FALSE) loop is used to make error cleanup easier. The */
/* close() of the socket descriptor is only done once at the very end */
/* of the program. */
/***********************************************************************/
do
{
/********************************************************************/
/* The socket() 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) will be used for this socket. */
/********************************************************************/
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd &lt; 0)
{
perror("socket() failed");
break;
}
/********************************************************************/
/* If an argument was passed in, use this as the server, otherwise */
/* use the #define that is located at the top of this program. */
/********************************************************************/
if (argc &gt; 1)
strcpy(server, argv[1]);
else
strcpy(server, SERVER_NAME);
memset(&amp;serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVER_PORT);
serveraddr.sin_addr.s_addr = inet_addr(server);
if (serveraddr.sin_addr.s_addr == (unsigned long)INADDR_NONE)
{
/*****************************************************************/
/* The server string that was passed into the inet_addr() */
/* function was not a dotted decimal IP address. It must */
/* therefore be the hostname of the server. Use the */
/* gethostbyname() function to retrieve the IP address of the */
/* server. */
/*****************************************************************/
hostp = gethostbyname(server);
if (hostp == (struct hostent *)NULL)
{
printf("Host not found --&gt; ");
printf("h_errno = %d\n", h_errno);
break;
}
memcpy(&amp;serveraddr.sin_addr,
hostp-&gt;h_addr,
sizeof(serveraddr.sin_addr));
}
/********************************************************************/
/* Use the connect() function to establish a connection to the */
/* server. */
/********************************************************************/
rc = connect(sd, (struct sockaddr *)&amp;serveraddr, sizeof(serveraddr));
if (rc &lt; 0)
{
perror("connect() failed");
break;
}
/********************************************************************/
/* Send 250 bytes of a's to the server */
/********************************************************************/
memset(buffer, 'a', sizeof(buffer));
rc = send(sd, buffer, sizeof(buffer), 0);
if (rc &lt; 0)
{
perror("send() failed");
break;
}
/********************************************************************/
/* In this example we know that the server is going to respond with */
/* the same 250 bytes that we just sent. Since we know that 250 */
/* bytes are going to be sent back to us, we can use the */
/* SO_RCVLOWAT socket option and then issue a single recv() and */
/* retrieve all of the data. */
/* */
/* The use of SO_RCVLOWAT is already illustrated in the server */
/* side of this example, so we will do something different here. */
/* The 250 bytes of the data may arrive in separate packets, */
/* therefore we will issue recv() over and over again until all */
/* 250 bytes have arrived. */
/********************************************************************/
bytesReceived = 0;
while (bytesReceived &lt; BUFFER_LENGTH)
{
rc = recv(sd, &amp; buffer[bytesReceived],
BUFFER_LENGTH - bytesReceived, 0);
if (rc &lt; 0)
{
perror("recv() failed");
break;
}
else if (rc == 0)
{
printf("The server closed the connection\n");
break;
}
/*****************************************************************/
/* Increment the number of bytes that have been received so far */
/*****************************************************************/
bytesReceived += rc;
}
} while (FALSE);
/***********************************************************************/
/* Close down any open socket descriptors */
/***********************************************************************/
if (sd != -1)
close(sd);
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="connectionor.htm" title="These server and client examples illustrate socket APIs written for a connection-oriented protocol such as Transmission Control Protocol (TCP).">Create a connection-oriented socket</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="uafinet.htm" title="AF_INET address family sockets can be either connection-oriented (type SOCK_STREAM) or they can be connectionless (type SOCK_DGRAM).">Use AF_INET address family</a></div>
</div>
</div>
</body>
</html>