<?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 accept_and_recv() and send_file() APIs to send contents of a file" /> <meta name="abstract" content="This example enables a server to communicate with a client by using the send_file() and accept_and_recv() APIs." /> <meta name="description" content="This example enables a server to communicate with a client by using the send_file() and accept_and_recv() APIs." /> <meta name="DC.Relation" scheme="URI" content="xsendfile.htm" /> <meta name="DC.Relation" scheme="URI" content="../apis/sendfile.htm" /> <meta name="DC.Relation" scheme="URI" content="../apis/acceptr.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="x1sendfile" /> <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 accept_and_recv() and send_file() APIs to send contents of a file</title> </head> <body id="x1sendfile"><a name="x1sendfile"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Example: Use <span class="apiname">accept_and_recv()</span> and <span class="apiname">send_file()</span> APIs to send contents of a file</h1> <div><p>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.</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>/*************************************************/ /* Server example send file data to client */ /*************************************************/ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #define SERVER_PORT 12345 main (int argc, char *argv[]) { int i, num, rc, flag = 1; int fd, listen_sd, accept_sd = -1; size_t local_addr_length; size_t remote_addr_length; size_t total_sent; struct sockaddr_in addr; struct sockaddr_in local_addr; struct sockaddr_in remote_addr; struct sf_parms parms; char buffer[255]; /*************************************************/ /* If an argument is specified, use it to */ /* control the number of incoming connections */ /*************************************************/ if (argc >= 2) num = atoi(argv[1]); else num = 1; /*************************************************/ /* Create an AF_INET stream socket to receive */ /* incoming connections on */ /*************************************************/ listen_sd = socket(AF_INET, SOCK_STREAM, 0); if (listen_sd < 0) { perror("socket() failed"); exit(-1); } /*************************************************/ /* Set the SO_REUSEADDR bit so that you do not */ /* need to wait 2 minutes before restarting */ /* the server */ /*************************************************/ rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(flag)); if (rc < 0) { perror("setsockop() failed"); close(listen_sd); exit(-1); } /*************************************************/ /* Bind the socket */ /*************************************************/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(SERVER_PORT); rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr)); if (rc < 0) { perror("bind() failed"); close(listen_sd); exit(-1); } /*************************************************/ /* Set the listen backlog */ /*************************************************/ rc = listen(listen_sd, 5); if (rc < 0) { perror("listen() failed"); close(listen_sd); exit(-1); } /*************************************************/ /* Initialize the local and remote addr lengths */ /*************************************************/ local_addr_length = sizeof(local_addr); remote_addr_length = sizeof(remote_addr); /*************************************************/ /* Inform the user that the server is ready */ /*************************************************/ printf("The server is ready\n"); /*************************************************/ /* Go through the loop once for each connection */ /*************************************************/ for (i=0; i < num; i++) { /**********************************************/ /* Wait for an incoming connection */ /**********************************************/ printf("Iteration: %d\n", i+1); printf(" waiting on accept_and_recv()\n"); rc = accept_and_recv(listen_sd, &accept_sd, (struct sockaddr *)&remote_addr, &remote_addr_length, (struct sockaddr *)&local_addr, &local_addr_length, &buffer, sizeof(buffer)); if (rc < 0) { perror("accept_and_recv() failed"); close(listen_sd); close(accept_sd); exit(-1); } printf(" Request for file: %s\n", buffer); /**********************************************/ /* Open the file to retrieve */ /**********************************************/ fd = open(buffer, O_RDONLY); if (fd < 0) { perror("open() failed"); close(listen_sd); close(accept_sd); exit(-1); } /**********************************************/ /* Initialize the sf_parms structure */ /**********************************************/ memset(&parms, 0, sizeof(parms)); parms.file_descriptor = fd; parms.file_bytes = -1; /**********************************************/ /* Initialize the counter of the total number */ /* of bytes sent */ /**********************************************/ total_sent = 0; /**********************************************/ /* Loop until the entire file has been sent */ /**********************************************/ do { rc = send_file(&accept_sd, &parms, SF_CLOSE); if (rc < 0) { perror("send_file() failed"); close(fd); close(listen_sd); close(accept_sd); exit(-1); } total_sent += parms.bytes_sent; } while (rc == 1); printf(" Total number of bytes sent: %d\n", total_sent); /**********************************************/ /* Close the file that is sent out */ /**********************************************/ close(fd); } /*************************************************/ /* Close the listen socket */ /*************************************************/ close(listen_sd); /*************************************************/ /* Close the accept socket */ /*************************************************/ if (accept_sd != -1) close(accept_sd); }</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 class="relinfo"><strong>Related information</strong><br /> <div><a href="../apis/sendfile.htm">send_file()</a></div> <div><a href="../apis/acceptr.htm">accept_and_recv()</a></div> </div> </div> </body> </html>