Implementation Of Simple FTP Client C Program

Algorithm Steps:
    Step 1: start
    Step 2: Create a socket with address family AE_INET type   SOCK_STREAM and default protocol.
    Step 3: Initialize the socket and set its attribute set required port no.
    Step 4: Connect to server using connect () function to initiate the request.
    Step 5: Receive the string from the server and print it at the console.
    Step 6: stop.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#define PORT_TIME       13              /* "time" (not available on RedHat) */
#define PORT_FTP        21              /* FTP connection port */
#define SERVER_ADDR     "127.0.0.1"     /* localhost */
#define MAXBUF          1024

int main()
{   int sockfd;
    struct sockaddr_in dest;
    char buffer[MAXBUF];

    /*---Open socket for streaming---*/
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        perror("Socket");
        exit(errno);
    }

    /*---Initialize server address/port struct---*/
    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(PORT_FTP);
    if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0 )
    {
        perror(SERVER_ADDR);
        exit(errno);
    }

    /*---Connect to server---*/
    if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
    {
        perror("Connect ");
        exit(errno);
    }

    /*---Get "Hello?"---*/
    bzero(buffer, MAXBUF);
    recv(sockfd, buffer, sizeof(buffer), 0);
    printf("%s", buffer);

    /*---Clean up---*/
    close(sockfd);
    return 0;
}
OUTPUT:
$ gcc Ftpclient.c -o Ftpclient.exe
$ Ftpclient.exe
220 Microsoft FTP Service

Post a Comment

3 Comments

  1. if i do use localhost as a ftp then what should i do?

    ReplyDelete
    Replies
    1. for this i need a server program,how should i do that?

      Delete
    2. Hi

      You might be searching for file transfer C program, Please refer below link.
      http://www.askforprogram.in/2016/07/c-program-for-ftp-socket-file-transfer.html

      revert back if you have need any other help, share your detailed requirement.

      Thanks

      Delete