免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3298 | 回复: 15
打印 上一主题 下一主题

如何在c裡面使用FTP? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-13 09:53 |只看该作者 |倒序浏览
Dear ALL :
        假如有一個FTP Address : 192.169.10.1
         user : test
         password : 1234
         裡面有一個目錄 : temp
         目錄底下有一個檔案 : a.txt
         如果用 Gun C 要如何編寫呢 ?
         Thanks!!!

论坛徽章:
0
2 [报告]
发表于 2003-02-13 10:14 |只看该作者

如何在c裡面使用FTP?

原帖由 "ksc" 发表:
Dear ALL :
        假如有一個FTP Address : 192.169.10.1
         user : test
         password : 1234
         裡面有一個目錄 : temp
         目錄底下有一個檔案 : a.txt
         如果用 Gun C 要?.........


用system执行shell不就可以了吗?

论坛徽章:
0
3 [报告]
发表于 2003-02-13 10:24 |只看该作者

如何在c裡面使用FTP?

对呀,写一个shell不就可以了
或者使用wget。
如果真要写C程序,那就看看ftp的RFC,也是很简单的

论坛徽章:
0
4 [报告]
发表于 2003-02-13 10:38 |只看该作者

如何在c裡面使用FTP?

那以上一個例子,Shell要如何寫呢?Thanks

论坛徽章:
0
5 [报告]
发表于 2003-02-13 10:49 |只看该作者

如何在c裡面使用FTP?

写一个get.sh,内容如下

#get.sh
ftp -n 192.169.10.1  21
user test 1234
cd temp
asc
get a.txt
bye

论坛徽章:
0
6 [报告]
发表于 2003-02-13 15:42 |只看该作者

如何在c裡面使用FTP?

Thanks liupch!

论坛徽章:
0
7 [报告]
发表于 2003-02-13 15:58 |只看该作者

如何在c裡面使用FTP?

liupch提供的shell不对,我修改了一下,以下的没问题.

  1.        #!/bin/bash
  2.        ftp -n<<!
  3.        open 192.168.0.104 21
  4.        user wenhao wenhao
  5.        binary
  6.        get xxx.tar.gz
  7.        quit
  8.        !
复制代码

论坛徽章:
0
8 [报告]
发表于 2003-02-13 17:48 |只看该作者

如何在c裡面使用FTP?

Dear 問號 :
        You are right,Thanks!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2003-02-13 19:58 |只看该作者

如何在c裡面使用FTP?

#编译方法:
#cc ... -lftp

#include <stdio.h>;
#include <sys/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, "root" ) < 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, "hitme" ) < 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, "/tmp" ) < 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, "root", "hitme", 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", "/etc/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","/d1/tmp/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);
}

论坛徽章:
0
10 [报告]
发表于 2003-02-14 09:06 |只看该作者

如何在c裡面使用FTP?

请问楼上,您的这个程序在什么系统中编译?
我在FreeBSD和Solaris下都没有这些函数


原帖由 "htldm" 发表:
      ftpinfo.debug = ON;
         ftpinfo.transf_calc = ON;
         ftpinfo.linger = OFF;

         /*
          * connect to peer at remote host.
          */
         if (ftp_prconnect ( &amp;a..........
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP