Chinaunix

标题: sco unix ftp 源代码 [打印本页]

作者: zengok    时间: 2003-01-23 15:17
标题: sco unix ftp 源代码
如下代码可以实现 unix to unix 的文件发送,但不通实现unix to windows之间的发送,各位指点指点。

#include <stdio.h>;
  #include <net/libftp.h>;

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

  main (argc, argv)
          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 ( &amp;ftpinfo, host ) < 0) {
                  printf("error: ftp_prconnect failed.\n";      
                  (void) check_n_close ( &amp;ftpinfo, ABNORMAL );
          }

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

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

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

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

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

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

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

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

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

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

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

          /*
           * do a 'put' to the remote host.
           */
          if (ftp_putfile ( &amp;ftpinfo, "/tmp/passwd", "c:/passwd" ) < 0) {
                  printf("error: ftp_putfile failed.\n");
                  (void) check_n_close ( &amp;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 ( &amp;ftpinfo, ASCII ) < 0) {
                  printf("error: ftp_settype failed in ascii mode.\n");
                  (void) check_n_close ( &amp;ftpinfo, ABNORMAL );
          }

          /*
           * do a 'get' from the remote host.
           */
          if (ftp_getfile ( &amp;ftpinfo, "/tmp/passwd","c:/passwd" )< 0){
                  printf("error: ftp_getfile failed.\n");
                  (void) check_n_close ( &amp;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 ( &amp;ftpinfo, "/tmp", "/tmp/test" ) < 0) {
                  printf("error: ftp_dir failed.\n");
                  (void) check_n_close ( &amp;ftpinfo, ABNORMAL );
          }

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

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

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

          /*
           * we're done with our job...so exit the program gracefully.
           */
          (void) check_n_close ( &amp;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);
  }
作者: htldm    时间: 2003-01-23 15:39
标题: sco unix ftp 源代码
ok
作者: rengongpu    时间: 2003-01-23 16:21
标题: sco unix ftp 源代码
怎么样编译。
作者: htldm    时间: 2003-01-23 19:31
标题: sco unix ftp 源代码
cc  prog.c -o prog -lftp
作者: sensir    时间: 2003-01-23 19:33
标题: sco unix ftp 源代码
加为精华吧
作者: zengok    时间: 2003-01-24 13:20
标题: sco unix ftp 源代码
难道没有人作过吗?
作者: 千禧龙    时间: 2003-02-03 02:55
标题: sco unix ftp 源代码
我没用过ftp库函数,用普通socket函数到是写过,
效果还不错,一直在用!
作者: chiaws    时间: 2004-04-29 09:40
标题: sco unix ftp 源代码
请问老大, 我的机器怎么有一个提示/usr/lib/libftp.a 的提示呀!
编译没有其他的错误,希望指点一二,谢谢!
作者: chiaws    时间: 2004-05-20 15:02
标题: sco unix ftp 源代码
各位老大:编译的问题解决了,命令如下:  cc -o ftp ftp.c -llftp -lsocket
可是运行时出现错误如下:
error: fopen failed to open in "r" mode for sending
final reply from server: 150

error: ftp _ftpfile failed
ferror:data connection failed
inal reply from server: 553
那位能帮我解决!这里先行谢过了!
作者: xsfh66    时间: 2004-07-13 16:13
标题: sco unix ftp 源代码
man ftp看到最后就是上面的例程了,只有unixware下能够使用,必须链接libftp.a,其他系统用不了的。
作者: kinks    时间: 2004-08-18 15:34
标题: sco unix ftp 源代码
我也来研究研究
作者: unixzyy    时间: 2005-02-05 21:12
标题: sco unix ftp 源代码
这个程序在UNIXWARE下实现很顺利,可是那个FTP_LOGIN()函数有问题,如果服务器(host)与本机网络不通就完蛋了,这个函数会一直等下去,只有按[delete]强制退出程序.谁有办法解决?
作者: wangzongxu    时间: 2009-05-02 06:08
?这个程序编译了 不能连接WIN机器呢
请指证





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2