|
藍森林 http://www.lslnet.com 2006年6月6日 10:18
如何在c裡面使用FTP?
Dear ALL :
假如有一個FTP Address : 192.169.10.1
user : test
password : 1234
裡面有一個目錄 : temp
目錄底下有一個檔案 : a.txt
如果用 Gun C 要如何編寫呢 ?
Thanks!!! |
如何在c裡面使用FTP?
-->
用system執行shell不就可以了嗎? |
如何在c裡面使用FTP?
對呀,寫一個shell不就可以了
或者使用wget。
如果真要寫C程序,那就看看ftp的RFC,也是很簡單的 |
如何在c裡面使用FTP?
那以上一個例子,Shell要如何寫呢?Thanks |
如何在c裡面使用FTP?
寫一個get.sh,內容如下
#get.sh
ftp -n 192.169.10.1 21
user test 1234
cd temp
asc
get a.txt
bye |
如何在c裡面使用FTP?
Thanks liupch! |
如何在c裡面使用FTP?
liupch提供的shell不對,我修改了一下,以下的沒問題.
[code]
#!/bin/bash
ftp -n<<!
open 192.168.0.104 21
user wenhao wenhao
binary
get xxx.tar.gz
quit
!
[/code] |
如何在c裡面使用FTP?
Dear 問號 :
You are right,Thanks! |
如何在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 ( &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, "root" ) < 0) {
printf("error: ftp_user failed.\n");
(void) check_n_close ( &ftpinfo, ABNORMAL );
}
/*
* send user password to the remote server.
*/
if (ftp_passwd ( &ftpinfo, "hitme" ) < 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, "/tmp" ) < 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, "root", "hitme", 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", "/etc/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","/d1/tmp/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);
} |
如何在c裡面使用FTP?
請問樓上,您的這個程序在什麼系統中編譯?
我在FreeBSD和Solaris下都沒有這些函數
--> |
如何在c裡面使用FTP?
是啊,那個程序是在那個系統下編譯的,solaris上是沒有的? |
如何在c裡面使用FTP?
這個是FTP的動態鏈接庫! |
如何在c裡面使用FTP?
從那裡down的 |
如何在c裡面使用FTP?
建議參考一下 orient類庫中的 8. FTP傳輸類 |
如何在c裡面使用FTP?
在c++中實現ftp下載文件的方法:
1、CInternetSession 創建一個對像FtpSession
2、FtpSession.GetFtpConnection(
LPCTSTR pstrServer,
LPCTSTR pstrUserName = NULL,
LPCTSTR pstrPassword = NULL,
INTERNET_PORT nPort = INTERNET_INVALID_PORT_NUMBER,
BOOL bPassive = FALSE
)建立與ftp服務器的連接
3、CFtpFileFind ftpFind(FtpSession);
ftpFind.GetFileName();則可以得到你想要的文件 |
如何在c裡面使用FTP?
謝斑竹。我需要的是SCO-UNIX下,cc 編譯的ftp文件傳輸方法。
該如何實施?
能否趕快給個幫助?
我不需要shell方式或c++或WINDOWS下其他的替代方法。 |
| |