如下代码可以实现 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 ( &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, "bank" ) < 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, "winnt" ) < 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, "bank", "", 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);
}
htldm 回复于:2003-01-23 15:39:54 |
ok |
rengongpu 回复于:2003-01-23 16:21:08 |
怎么样编译。 |
htldm 回复于:2003-01-23 19:31:18 |
cc prog.c -o prog -lftp |
sensir 回复于:2003-01-23 19:33:55 |
加为精华吧 |
zengok 回复于:2003-01-24 13:20:24 |
难道没有人作过吗? |
千禧龙 回复于:2003-02-03 02:55:06 |
我没用过ftp库函数,用普通socket函数到是写过,
效果还不错,一直在用! |
chiaws 回复于:2004-04-29 09:40:55 |
请问老大, 我的机器怎么有一个提示/usr/lib/libftp.a 的提示呀!
编译没有其他的错误,希望指点一二,谢谢! |
chiaws 回复于:2004-05-20 15:02:21 |
各位老大:编译的问题解决了,命令如下: 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:28 |
man ftp看到最后就是上面的例程了,只有unixware下能够使用,必须链接libftp.a,其他系统用不了的。 |
kinks 回复于:2004-08-18 15:34:32 |
我也来研究研究 |
unixzyy 回复于:2005-02-05 21:12:56 |
这个程序在UNIXWARE下实现很顺利,可是那个FTP_LOGIN()函数有问题,如果服务器(host)与本机网络不通就完蛋了,这个函数会一直等下去,只有按[delete]强制退出程序.谁有办法解决? |
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/