Need help with ftp client programming

This is a discussion on Need help with ftp client programming within the Networking/Device Communication forums, part of the General Programming Boards category; Anyone familiar with socket programming? please help~!!! I am trying to implement very simple ftp client from this source file ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Need help with ftp client programming

    Anyone familiar with socket programming?
    please help~!!!
    I am trying to implement very simple ftp client from this source file but when I compile it and run it I get the followings.

    ----------------------------------------------
    linux2[19]% ./ftpclient ftp.gl.umbc.edu

    Username>>mkim

    Password>>1234
    Broken pipe
    ----------------------------------------------
    What does this Broken pipe mean?
    How can I fix it?

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    #include <strings.h>
    
    #define MYFTP_PORT 9999
    
    
       char username[20]; 
       char pwd[6];
       char temp[10];
       int client; 
    /***************************************************************/
       void get_user_pwd(void) {
          char buff[27],temp[10];
          while(1) {
             printf("\nUsername>>");
             scanf("%s",username);
             printf("\nPassword>>");          
             scanf("%s",pwd); 
             strcpy(buff,username);
             strcat(buff," ");
             strcat(buff,pwd);
          
             write(client,buff,strlen(buff)+1);
             read(client,temp,10); 
          
             if (strcmp(temp,"OK")) 
                printf("Unknown username/password");
             else {
                printf("Connected\n");
                return; 
             }
          }     
       }
    /**************************************************************/
       void get_file(char *fname) {
          FILE *in;
          char buffer[100];
          int total=0;
       
          in=fopen(fname,"w+");
          strcpy(buffer,"OK");
          write(client,buffer,strlen(buffer)+1);
          read(client,buffer,100);
          while(buffer[0]!='\0'){
             fprintf(in,"%s",buffer);
             total+=strlen(buffer);       
             strcpy(buffer,"OK");
             write(client,buffer,strlen(buffer)+1);
             read(client,buffer,100);
          }
          printf("\n%d bytes succesfully copied to %s ....\n",
                total,fname);
          fclose(in);    
       }  
    /**************************************************************/
       void send_file(char *fname){
          char line[100]; 
          FILE *fd;
          int total=0;
       
          if((fd=fopen(fname,"r"))){
             fgets(line,100,fd);
             while(!feof(fd)){
                write(client,line,strlen(line)+1);
                total+=strlen(line);
                read(client,line,100);
                fgets(line,100,fd);
             }
             line[0]='\0';
             write(client,line,1);
             printf("\n%d bytes sent to server succesfully",total);
             fclose(fd); 
          }
          else{
             printf("\nCan not open source file");
             strcpy(line,"NOOK");
             write(client,line,strlen(line)+1);
          }
       }
    /**************************************************************/
       void lchdir(char *path){
          if(chdir(path)<0){
             perror("\nInvalid directory path");
          }
          else
             printf("\nNew local directory= %s",path); 
       }
    /**************************************************************/
       void lsfiles(void){
          char buff[100],temp[5];
       
          read(client,buff,strlen(buff)+1);
          strcpy(temp,"OK");
          if(!strcmp(buff,"NOOK")){
             printf("\nCan not list files");
             write(client,temp,strlen(temp)+1);
             return;
          }
          while(buff[0]!='\0'){
             printf("\n %s",buff);
             write(client,temp,strlen(temp)+1);
             read(client,buff,100);
          }
       
       }
    /**************************************************************/
       void get_command(void) {
       
          char srcf_name[10],desf_name[10]; 
          char buff[100]; 
          char command[10]; 
          /*  FILE *fd; */
       
          while(1) {
             printf("\n>>");        
             scanf("%s",command);
          
             if (!strcmp(command,"get")){ 
                scanf(" %s %s",srcf_name,desf_name); 
                strcpy(buff,command);
                strcat(buff," ");
                strcat(buff,srcf_name);
                write(client,buff,strlen(buff)+1);
                read(client,temp,10);
                if (strcmp(temp,"OK"))                     
                   printf("File not found");
                else{           
                   get_file(desf_name);
                   return;
                }
             
             }
             else if(!strcmp(command,"put")){
                scanf(" %s %s",srcf_name,desf_name);
                strcpy(buff,command);
                strcat(buff," ");
                strcat(buff,desf_name);
                write(client,buff,strlen(buff)+1);
                read(client,buff,100);
                send_file(srcf_name);
                return;
             }
             if(!strcmp(command,"lcd")){
                scanf("%s",buff); 
                lchdir(buff);
             }
             else if(!strcmp(command,"ls")){   
                strcpy(buff,command);
                strcat(buff," x");
                write(client,buff,strlen(buff)+1);
                lsfiles();
             }
             else
                printf("\nUnkonwn Command......");
          }
       }
    /**************************************************************/
       int main (int argc,char *argv[]){
       
       
          struct sockaddr_in to;
          struct hostent *host_info;
       
    /*      char buff[100]="", temp[100], eof_str[2]; */
       
          if (argc!=2)
          {
             printf("Wrong Usage myftp ata.cs.hun.edu.tr");
             exit(1);        
          } 
       
          if((client=socket(AF_INET,SOCK_STREAM,0))<0){
             perror("creating stream socket");
             exit(2);
          }
       
          if((host_info=gethostbyname(argv[1]))==NULL){
             perror("unknown host ");
             exit(3);
          }
       
          to.sin_family=host_info->h_addrtype;
          memcpy((char*)&to.sin_addr,host_info->h_addr,
                host_info->h_length);
          to.sin_port=htons(MYFTP_PORT);  
       
          connect(client,(struct sockaddr *)&to,sizeof(to));
          get_user_pwd();
          get_command();         
       
          close(client);
          exit(0);
    
          return 0;
       }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    1. Read this Useful Links And Good Books
    2. Get ethereal and study the protocol in action by observing a real FTP client
    3. Get the RFC which describes the FTP protocol - www.rfc-editor.org

    4.
    Code:
          strcpy(buffer,"OK");
          write(client,buffer,strlen(buffer)+1);
          read(client,buffer,100);
          while(buffer[0]!='\0'){
             fprintf(in,"%s",buffer);
             total+=strlen(buffer);
    All sorts of mistakes here
    a) FTP commands end with \r rather than \0. strlen(buff)+1 includes that \0
    b) none of your read or write calls check for error returns. The network may fragment messages on both sending and receiving, so it's no use assuming that it will always work the way you have it
    c) while(buffer[0]!='\0') - read() won't magically store a \0 at the end of the stream, it just returns a status
    d) total+=strlen(buffer); - read() doesn't append a \0 just to make your data a proper string - again, use the return status to determine what happened.

    All your other use of read() and write() is similarly suspect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 11:41 AM
  2. [HELP] FTP Client Stuck At 220 Welcome Message
    By iqueen in forum C Programming
    Replies: 0
    Last Post: 04-21-2009, 12:29 AM
  3. FTP Client
    By waldoayo in forum Networking/Device Communication
    Replies: 5
    Last Post: 04-29-2006, 11:49 AM
  4. [C++] FTP client problem (winsock)
    By Niekie in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-19-2003, 09:23 AM
  5. FTP client in C???
    By Lau in forum C Programming
    Replies: 8
    Last Post: 05-13-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21