⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp.c

📁 利用ftp协议进行ftp编程
💻 C
字号:
#include <stdio.h> 
#include <net/libftp.h> 

#define NORMAL  0 
#define ABNORMAL        1 
#define ON              1 
#define OFF             0 

main( int argc, char *argv[] ) 
{ 
          FTPINFO ftpinfo;        
          void    usage(), check_n_close(); 
          char    *progname; 
          char    *host; 
          
          progname = (char *) argv[0]; 
          if ( argc < 2 ) 
                  (void) usage(progname); 

          host = argv[1]; 
          ftpinfo.debug = ON; 
          ftpinfo.transf_calc = ON; 
          ftpinfo.linger = OFF; 

          /* 
           * connect to peer at remote host. 
           */ 
          if (ftp_prconnect ( &ftpinfo, host ) < 0) { 
                  printf("error: ftp_prconnect failed.\n");       
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * send user name to the remote server. 
           */ 
          if (ftp_user( &ftpinfo, "hui" ) < 0) { 
                  printf("error: ftp_user failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * send user password to the remote server. 
           */ 
          if (ftp_passwd ( &ftpinfo, "" ) < 0) { 
                  printf("error: ftp_passwd failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          }       
                  
          /* 
           * set the idle time for this connection. 
           */ 
          if (ftp_idle ( &ftpinfo, "7200" ) < 0) { 
                  printf("error: ftp_idle failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 
          
          /* 
           * do a 'cd' on the remote ftp server. 
           */ 
          if (ftp_chdir( &ftpinfo, "d:\c++" ) < 0) { 
                  printf("error: ftp_chdir failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * do a 'pwd' on the remote ftp server. 
           */ 
          if (ftp_pwd ( &ftpinfo ) < 0) { 
                  printf("error: ftp_pwd failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * set transfer mode to ascii. 
           */ 
          if (ftp_ascii ( &ftpinfo ) < 0) { 
                  printf("error: ftp_ascii failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * set transfer mode to binary. 
           */ 
          if (ftp_binary ( &ftpinfo ) < 0) { 
                  printf("error: ftp_binary failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * set transfer mode back to ascii - using ftp_settype. 
           */ 
          if (ftp_settype ( &ftpinfo, ASCII ) < 0) { 
                  printf("error: ftp_settype failed in ascii mode.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * set transfer mode back to binary - using ftp_settype. 
           */ 
          if (ftp_settype ( &ftpinfo, BINARY ) < 0) { 
                  printf("error: ftp_settype failed in binary mode.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * make a directory under /tmp on the server. 
           */ 
          if (ftp_mkdir ( &ftpinfo, "prem" ) < 0) { 
                  printf("error: ftp_mkdir failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 
                  
          /* 
           * change the mode of the directory created above. 
           */ 
          if (ftp_site ( &ftpinfo, "chmod 775 prem" ) < 0) { 
                  printf("error: ftp_site failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * remove the directory created above. 
           */ 
          if (ftp_rmdir ( &ftpinfo, "prem" ) < 0) { 
                  printf("error: ftp_rmdir failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * quit the FTP session decently. 
           */ 
          if (ftp_bye( &ftpinfo ) < 0) { 
                  printf("error: ftp_bye failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * use ftp_login to login into the remote ftp server and quit. 
           */ 
          if (ftp_login ( &ftpinfo, host, "hui", "", NULL ) < 0) { 
                  printf("error: ftp_login failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * do a 'put' to the remote host. 
           */ 
          if (ftp_putfile ( &ftpinfo, "/tmp/passwd", "c:/passwd" ) < 0) { 
                  printf("error: ftp_putfile failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 
          else { 
                  printf("transfer speed: \n"); 
                  printf("\t\tbytes transferred = %ld\n", 
                                  ftpinfo.speed.size_bytes); 
                  printf("\t\ttime taken        = %.2g seconds\n", 
                                  ftpinfo.speed.seconds);                         
                  printf("\t\trate              = %.2g Kbytes/s\n", 
                                  ftpinfo.speed.kbs); 
          } 

          /* 
           * set transfer mode back to ascii - using ftp_settype. 
           */ 
          if (ftp_settype ( &ftpinfo, ASCII ) < 0) { 
                  printf("error: ftp_settype failed in ascii mode.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * do a 'get' from the remote host. 
           */ 
          if (ftp_getfile ( &ftpinfo, "/tmp/passwd","c:/passwd" )< 0){ 
                  printf("error: ftp_getfile failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 
          else { 
                  printf("transfer speed: \n"); 
                  printf("\t\tbytes transferred = %ld\n", 
                                  ftpinfo.speed.size_bytes); 
                  printf("\t\ttime taken       = %.2g seconds\n", 
                                  ftpinfo.speed.seconds);                         
                  printf("\t\trate             = %.2g Kbytes/s\n", 
                                  ftpinfo.speed.kbs); 
          } 

          /* 
           * list /tmp on remote host to file /tmp/test. 
           */ 
          if (ftp_dir ( &ftpinfo, "/tmp", "/tmp/test" ) < 0) { 
                  printf("error: ftp_dir failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * list /tmp on remote host to stdout. 
           */ 
          if (ftp_dir ( &ftpinfo, "/tmp", NULL ) < 0) { 
                  printf("error: ftp_dir failed to write to stdout.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * delete the file on the remote host. 
           */ 
          if (ftp_del ( &ftpinfo, "/tmp/asciifile" ) < 0) { 
                  printf("error: ftp_del failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * quit the FTP sessions decently. 
           */ 
          if (ftp_bye( &ftpinfo ) < 0) { 
                  printf("error: ftp_bye failed.\n"); 
                  (void) check_n_close ( &ftpinfo, ABNORMAL ); 
          } 

          /* 
           * we're done with our job...so exit the program gracefully. 
           */ 
          (void) check_n_close ( &ftpinfo, NORMAL );      

  } 


  void 
  usage(progname) 
          char *progname; 
  { 
          printf("usage: %s <remhost>\n", progname); 
          exit (1); 
  } 

  void 
  check_n_close ( ftpinfo, status ) 
          FTPINFO *ftpinfo; 
          int     status; 
  { 
          if (ftpinfo -> sockfd >= 0) 
                  close (ftpinfo -> sockfd); 
          if (status == ABNORMAL) 
                  printf("error: %s\n", ftpinfo -> ftp_msg); 
          else 
                  printf("success: %s\n", ftpinfo -> ftp_msg); 

          printf("final reply from server: %d\n", ftpinfo -> reply); 
          fflush ( stdout ); 
          exit (status); 
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -