138 lines
9.4 KiB
HTML
138 lines
9.4 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="Examples: Transfer file data using send_file() and accept_and_recv() APIs" />
|
|
<meta name="abstract" content="These examples enable a server to communicate with a client by using the send_file() and accept_and_recv() APIs." />
|
|
<meta name="description" content="These examples enable a server to communicate with a client by using the send_file() and accept_and_recv() APIs." />
|
|
<meta name="DC.Relation" scheme="URI" content="example.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="x1sendfile.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="x2sendfile.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="ctransfer.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/accept.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/recv.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/send.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/listen.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/close.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/socket.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/bind.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/gsockn.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/open.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/read.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/connec.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="xsendfile" />
|
|
<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>Examples: Transfer file data using send_file() and accept_and_recv() APIs</title>
|
|
</head>
|
|
<body id="xsendfile"><a name="xsendfile"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Examples: Transfer file data using <span class="apiname">send_file()</span> and <span class="apiname">accept_and_recv()</span> APIs</h1>
|
|
<div><p>These examples enable a server to communicate with a client by
|
|
using the <span class="apiname">send_file()</span> and <span class="apiname">accept_and_recv()</span> APIs.</p>
|
|
<div class="section"><p><br /><img src="rzab6500.gif" alt="This graphic shows the socket calls that are used to send file data between processes." /><br /></p>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Server send contents of a file</h4><p>The
|
|
following sequence of the socket calls provides a description of the graphic.
|
|
It also describes the relationship between two applications that send and
|
|
receive files. The first example uses the following sequence of function calls:</p>
|
|
<ol><li>The server calls <span class="apiname">socket()</span>, <span class="apiname">bind()</span>,
|
|
and <span class="apiname">listen()</span> to create a listening socket.</li>
|
|
<li>The server initializes the local and remote address structures.</li>
|
|
<li>The server calls <span class="apiname">accept_and_recv()</span> to wait for an incoming
|
|
connection and to wait for the first data buffer to arrive over this connection.
|
|
This call returns the number of bytes that is received and the local and remote
|
|
addresses that are associated with this connection. This call is a combination
|
|
of the <span class="apiname">accept()</span>, <span class="apiname">getsockname()</span>, and <span class="apiname">recv()</span> APIs.</li>
|
|
<li>The server calls <span class="apiname">open()</span> to open the file whose name
|
|
was obtained as data on the <span class="apiname">accept_and_recv()</span> from the
|
|
client application.</li>
|
|
<li>The <span class="apiname">memset()</span> function is used to set all of the fields
|
|
of the sf_parms structure to an initial value of 0. The server sets the file
|
|
descriptor field to the value that <span class="apiname">open()</span> returned. The
|
|
server then sets the file bytes field to -1 to indicate that the server should
|
|
send the entire file. The system is sending the entire file, so you do not
|
|
need to assign the file offset field.</li>
|
|
<li>The server calls <span class="apiname">send_file()</span> to transmit
|
|
the contents of the file. <span class="apiname">send_file()</span> does not complete
|
|
until the entire file has been sent or an interruption occurs. The <span class="apiname">send_file()</span> function
|
|
is more efficient because the application does not need to go
|
|
into a <span class="apiname">read()</span> and <span class="apiname">send()</span> loop until
|
|
the file finishes.</li>
|
|
<li>The server specifies the <samp class="codeph">SF_CLOSE</samp> flag on the <span class="apiname">send_file()</span> API.
|
|
The <samp class="codeph">SF_CLOSE</samp> flag informs the <span class="apiname">send_file()</span> API
|
|
that it should automatically close the socket connection when the last byte
|
|
of the file and the trailer buffer (if specified) have been sent successfully.
|
|
The application does not need to call <span class="apiname">close()</span> if the <samp class="codeph">SF_CLOSE</samp> flag
|
|
is specified.</li>
|
|
</ol>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Client request for file</h4><p>The
|
|
second example uses the following sequence of function calls:</p>
|
|
<ol><li>This client program takes from zero to two parameters. <p>The first parameter
|
|
(if specified) is the dotted-decimal IP address or the host name where the
|
|
server application is located.</p>
|
|
<p>The second parameter (if specified) is
|
|
the name of the file that the client attempts to obtain from the server. A
|
|
server application sends the contents of the specified file to the client.
|
|
If the user does not specify any parameters, then the client uses INADDR_ANY
|
|
for the server's IP address. If the user does not specify a second parameter,
|
|
the program prompts the user to enter a file name.</p>
|
|
</li>
|
|
<li>The client calls <span class="apiname">socket()</span> to create a socket descriptor.</li>
|
|
<li>The client calls <span class="apiname">connect()</span> to establish a connection
|
|
to the server. Step one obtained the IP address of the server.</li>
|
|
<li>The client calls <span class="apiname">send()</span> to inform the server what file
|
|
name it wants to obtain. Step one obtained the name of the file.</li>
|
|
<li>The client goes into a "do" loop calling <span class="apiname">recv()</span> until
|
|
the end of the file is reached. A return code of 0 on the <span class="apiname">recv()</span> means
|
|
that the server closed the connection.</li>
|
|
<li>The client calls <span class="apiname">close()</span> to close the socket.</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<ul class="ullinks">
|
|
<li class="ulchildlink"><strong><a href="x1sendfile.htm">Example: Use accept_and_recv() and send_file() APIs to send contents of a file</a></strong><br />
|
|
This example enables a server to communicate with a client by using
|
|
the <span class="apiname">send_file()</span> and <span class="apiname">accept_and_recv()</span> APIs.</li>
|
|
<li class="ulchildlink"><strong><a href="x2sendfile.htm">Example: Client request for a file</a></strong><br />
|
|
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.</li>
|
|
</ul>
|
|
|
|
<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="ctransfer.htm" title="i5/OS sockets provide the send_file() and accept_and_recv() APIs that enable faster and easier file transfers over connected sockets.">File data transfer—send_file() and accept_and_recv()</a></div>
|
|
</div>
|
|
<div class="relinfo"><strong>Related information</strong><br />
|
|
<div><a href="../apis/accept.htm">accept()</a></div>
|
|
<div><a href="../apis/recv.htm">recv()</a></div>
|
|
<div><a href="../apis/send.htm">send()</a></div>
|
|
<div><a href="../apis/listen.htm">listen()</a></div>
|
|
<div><a href="../apis/close.htm">close()</a></div>
|
|
<div><a href="../apis/socket.htm">socket()</a></div>
|
|
<div><a href="../apis/bind.htm">bind()</a></div>
|
|
<div><a href="../apis/gsockn.htm">getsockname()</a></div>
|
|
<div><a href="../apis/open.htm">open()</a></div>
|
|
<div><a href="../apis/read.htm">read()</a></div>
|
|
<div><a href="../apis/connec.htm">connect()</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |