226 lines
11 KiB
HTML
226 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: IPv4 or IPv6 client" />
|
|
<meta name="abstract" content="This sample program can be used with the server application that accepts requests from either IPv4 or IPv6 clients." />
|
|
<meta name="description" content="This sample program can be used with the server application that accepts requests from either IPv4 or IPv6 clients." />
|
|
<meta name="DC.Relation" scheme="URI" content="ip6scen.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="xacceptboth.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="generic.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="xip6client" />
|
|
<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: IPv4 or IPv6 client </title>
|
|
</head>
|
|
<body id="xip6client"><a name="xip6client"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: IPv4 or IPv6 client </h1>
|
|
<div><p>This sample program can be used with the server application that
|
|
accepts requests from either IPv4 or IPv6 clients. </p>
|
|
<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>/**************************************************************************/
|
|
/* This is an IPv4 or IPv6 client. */
|
|
/**************************************************************************/
|
|
|
|
/**************************************************************************/
|
|
/* Header files needed for this sample program */
|
|
/**************************************************************************/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
|
|
/**************************************************************************/
|
|
/* Constants used by this program */
|
|
/**************************************************************************/
|
|
#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=0;
|
|
char buffer[BUFFER_LENGTH];
|
|
char server[NETDB_MAX_HOST_NAME_LENGTH];
|
|
char servport[] = "3005";
|
|
struct in6_addr serveraddr;
|
|
struct addrinfo hints, *res=NULL;
|
|
|
|
/***********************************************************************/
|
|
/* 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 along with the free of the list of addresses. */
|
|
/***********************************************************************/
|
|
do
|
|
{
|
|
/********************************************************************/
|
|
/* 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 > 1)
|
|
strcpy(server, argv[1]);
|
|
else
|
|
strcpy(server, SERVER_NAME);
|
|
|
|
memset(&hints, 0x00, sizeof(hints));
|
|
hints.ai_flags = AI_NUMERICSERV;
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
/********************************************************************/
|
|
/* Check if we were provided the address of the server using */
|
|
/* inet_pton() to convert the text form of the address to binary */
|
|
/* form. If it is numeric then we want to prevent getaddrinfo() */
|
|
/* from doing any name resolution. */
|
|
/********************************************************************/
|
|
rc = inet_pton(AF_INET, server, &serveraddr);
|
|
if (rc == 1) /* valid IPv4 text address? */
|
|
{
|
|
hints.ai_family = AF_INET;
|
|
hints.ai_flags |= AI_NUMERICHOST;
|
|
}
|
|
else
|
|
{
|
|
rc = inet_pton(AF_INET6, server, &serveraddr);
|
|
if (rc == 1) /* valid IPv6 text address? */
|
|
{
|
|
|
|
hints.ai_family = AF_INET6;
|
|
hints.ai_flags |= AI_NUMERICHOST;
|
|
}
|
|
}
|
|
/********************************************************************/
|
|
/* Get the address information for the server using getaddrinfo(). */
|
|
/********************************************************************/
|
|
rc = getaddrinfo(server, servport, &hints, &res);
|
|
if (rc != 0)
|
|
{
|
|
printf("Host not found --> %s\n", gai_strerror(rc));
|
|
if (rc == EAI_SYSTEM)
|
|
perror("getaddrinfo() failed");
|
|
break;
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* The socket() function returns a socket descriptor representing */
|
|
/* an endpoint. The statement also identifies the address family, */
|
|
/* socket type, and protocol using the information returned from */
|
|
/* getaddrinfo(). */
|
|
/********************************************************************/
|
|
sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
if (sd < 0)
|
|
{
|
|
perror("socket() failed");
|
|
break;
|
|
}
|
|
/********************************************************************/
|
|
/* Use the connect() function to establish a connection to the */
|
|
/* server. */
|
|
/********************************************************************/
|
|
rc = connect(sd, res->ai_addr, res->ai_addrlen);
|
|
if (rc < 0)
|
|
{
|
|
/*****************************************************************/
|
|
/* Note: the res is a linked list of addresses found for server. */
|
|
/* If the connect() fails to the first one, subsequent addresses */
|
|
/* (if any) in the list can be tried if required. */
|
|
/*****************************************************************/
|
|
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 < 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. */
|
|
/********************************************************************/
|
|
while (bytesReceived < BUFFER_LENGTH)
|
|
{
|
|
rc = recv(sd, & buffer[bytesReceived],
|
|
BUFFER_LENGTH - bytesReceived, 0);
|
|
if (rc < 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);
|
|
/***********************************************************************/
|
|
/* Free any results returned from getaddrinfo */
|
|
/***********************************************************************/
|
|
if (res != NULL)
|
|
freeaddrinfo(res);
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="ip6scen.htm" title="This topic describes a typical situation in which you might want to use the AF_INET6 address family.">Socket scenario: Create an application to accept IPv4 and IPv6 clients</a></div>
|
|
</div>
|
|
<div class="relref"><strong>Related reference</strong><br />
|
|
<div><a href="xacceptboth.htm" title="Use this sample program to create a server/client model that accepts requests from both IPv4 (those socket applications that use the AF_INET address family) and IPv6 (those applications that use the AF_INET6 address family).">Example: Accept connections from both IPv6 and IPv4 clients</a></div>
|
|
<div><a href="generic.htm" title="This code example contains the code for a common client job.">Example: Generic client</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |