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

175 lines
6.3 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: Client request for a file" />
<meta name="abstract" content="This example enables a client to request a file from the server and to wait for the server to send the contents of that file back." />
<meta name="description" content="This example enables a client to request a file from the server and to wait for the server to send the contents of that file back." />
<meta name="DC.Relation" scheme="URI" content="xsendfile.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="x2sendfile" />
<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: Client request for a file</title>
</head>
<body id="x2sendfile"><a name="x2sendfile"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Client request for a file</h1>
<div><p>This example enables a client to request a file from the server
and to wait for the server to send the contents of that file back.</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>/*************************************************/
/* Client example requests file data from server */
/*************************************************/
#include &lt;ctype.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;netdb.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#define SERVER_PORT 12345
main (int argc, char *argv[])
{
int rc, sockfd;
char filename[256];
char buffer[32 * 1024];
struct sockaddr_in addr;
struct hostent *host_ent;
/*************************************************/
/* Initialize the socket address structure */
/*************************************************/
memset(&amp;addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVER_PORT);
/*************************************************/
/* Determine the host name and IP address of the */
/* machine the server is running on */
/*************************************************/
if (argc &lt; 2)
{
addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
else if (isdigit(*argv[1]))
{
addr.sin_addr.s_addr = inet_addr(argv[1]);
}
else
{
host_ent = gethostbyname(argv[1]);
if (host_ent == NULL)
{
printf("Host not found!\n");
exit(-1);
}
memcpy((char *)&amp;addr.sin_addr.s_addr,
host_ent-&gt;h_addr_list[0],
host_ent-&gt;h_length);
}
/**************************************************/
/* Check to see if the user specified a file name */
/* on the command line */
/**************************************************/
if (argc == 3)
{
strcpy(filename, argv[2]);
}
else
{
printf("Enter the name of the file:\n");
gets(filename);
}
/*************************************************/
/* Create an AF_INET stream socket */
/*************************************************/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd &lt; 0)
{
perror("socket() failed");
exit(-1);
}
printf("Socket completed.\n");
/*************************************************/
/* Connect to the server */
/*************************************************/
rc = connect(sockfd,
(struct sockaddr *)&amp;addr,
sizeof(struct sockaddr_in));
if (rc &lt; 0)
{
perror("connect() failed");
close(sockfd);
exit(-1);
}
printf("Connect completed.\n");
/*************************************************/
/* Send the request over to the server */
/*************************************************/
rc = send(sockfd, filename, strlen(filename) + 1, 0);
if (rc &lt; 0)
{
perror("send() failed");
close(sockfd);
exit(-1);
}
printf("Request for %s sent\n", filename);
/*************************************************/
/* Receive the file from the server */
/*************************************************/
do
{
rc = recv(sockfd, buffer, sizeof(buffer), 0);
if (rc &lt; 0)
{
perror("recv() failed");
close(sockfd);
exit(-1);
}
else if (rc == 0)
{
printf("End of file\n");
break;
}
printf("%d bytes received\n", rc);
} while (rc &gt; 0);
/*************************************************/
/* Close the socket */
/*************************************************/
close(sockfd);
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="xsendfile.htm" title="These examples enable a server to communicate with a client by using the send_file() and accept_and_recv() APIs.">Examples: Transfer file data using send_file() and accept_and_recv() APIs</a></div>
</div>
</div>
</body>
</html>