DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ftp Client Asking For Files

10.30.2008
| 2022 views |
  • submit to reddit
        // ftp client 


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define PORT 73061 // the port number for connection
#define BUFLEN 1024 // max number of bytes we can get at once

int main(void )
{
  FILE *fil;
  char data[20], rec[20];
  int sockdesc, numbytes;
  char buf[BUFLEN];
  struct hostent *hName;
  struct sockaddr_in address; // connector.s address information

  struct hostent *he;
  struct sockaddr_in their_addr; // connector.s address information

  char host[30];

  printf("Enter the Host Name you want to connect :\t");
  gets(host);

  if (host == "")
  {
    printf("\nyou have entered an invalid host name\n");
    exit(1);
  }

  if ((he=gethostbyname(host)) == NULL)
  {
    printf("Error Resolving host");
    exit(1);
  }

  while(1)
  {
    // Create a Socket
    sockdesc = socket (PF_INET , SOCK_STREAM , 0);

    // check whether socket is created or not
    if (sockdesc == -1)
    {
      perror("\n\n\t\tCan't create Socket\n\n");
      exit(1);
    }

    address.sin_family = AF_INET; // host byte order
    address.sin_port = htons(PORT); // short, network byte order
    address.sin_addr = *((struct in_addr *)he->h_addr);
    //address.sin_addr.s_addr = ((struct in_addr *)he->h_addr); //INADDR_ANY;
    printf("\n\t\tThe Client IP is :\t%s", inet_ntoa(address.sin_addr));
    memset( &( address.sin_zero ), '\0', 8 ); // zero the rest of the struct

    if ( connect( sockdesc, ( struct sockaddr * ) &address, sizeof( struct sockaddr ) ) == -1 )
    {
      perror("\n\n\t\tCan't connect\n\n");
      exit(1);
    }

    printf("\n\n\tEnter the commnad to the server :\t");
    gets(data);

    if(strcmp(data , "exit") == 0)
    {
        if (send(sockdesc, data, 14, 0) == -1)
          perror("send");
       break;
    }

    if( (data[0]=='g' && data[1]=='e' && data[2]=='t') || (strcmp(data, "list")== 0) )
    {
        if (send(sockdesc, data, 14, 0) == -1)
          perror("send");

        // Recieve data from PORT in buffer
        if ( ( numbytes = recv( sockdesc, rec, 7, 0 ) ) == -1 )
        {
            perror("\n\tCan't recieve data properly\n\n");
            exit(1);
        }

        rec[6]='\0';
        printf("%s",rec);

        if ( ( numbytes = recv( sockdesc, buf, BUFLEN-1, 0 ) ) == -1 )
        {
          perror("\n\tCan't recieve data properly\n\n");
          exit(1);
        }

        // Enter a null character at the end of the String that we have read
        buf[numbytes] = '\0';

        // Display the recieved data
        printf("\n\n\t Data Recieved From Server is :\t %s\n\n",buf);

        if( buf[0]=='2' && buf[1]=='0' && buf[2]=='0' )
        {
          if(strcmp (data, "get file1.txt"))
            fil=fopen("file1.txt","wb+");

          if(strcmp (data, "get file2.txt"))
            fil=fopen("file2.txt","wb+");

          if(strcmp (data, "get file3.txt"))
            fil=fopen("file3.txt","wb+");

          if(fil == NULL)
            printf("\n\t\tThe required file can't be saved\n");

          fwrite(buf,25,1,fil);
          fclose(fil);
        }

        data[0] = '\0';
    }
    else
    {
      printf("\n\t\tBad Command is entered ");
    }

    fflush(stdin);

    // Close the socket
    close(sockdesc);
  }
  return 0;
}