Example: Client request for a file

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.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/*************************************************/
/* Client example requests file data from server */
/*************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#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(&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 < 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 *)&addr.sin_addr.s_addr,
             host_ent->h_addr_list[0],
             host_ent->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 < 0)
   {
      perror("socket() failed");
      exit(-1);
   }
   printf("Socket completed.\n");

   /*************************************************/
   /* Connect to the server                         */
   /*************************************************/
   rc = connect(sockfd,
                (struct sockaddr *)&addr,
                sizeof(struct sockaddr_in));
   if (rc < 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 < 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 < 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 > 0);

   /*************************************************/
   /* Close the socket                              */
   /*************************************************/
   close(sockfd);
}