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

📄 ftp_tools.c

📁 sco unix下使用ftp函数传文件、接收文件
💻 C
字号:
/************************************************************************* * *     作者: 朱世峰   2000.10.9 * *     功能: 用 ftp 方式从远程机器发送、接收文件子函数 * *     用法: putfile(对方服务器  用户名 密码 要发送的本地文件名  *                   对方文件名  错误信息) * *           getfile(对方服务器  用户名 密码 要接收的对方文件名 *                   本地文件名  错误信息) * * **************************************************************************/#ifdef CCextern "C"{#endif #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <net/libftp.h>#include <sys/socket.h>#include <sys/types.h>#include <signal.h>#ifdef CC}#endif#define TIMEOUT 	20#define TIMEOUTTR 	90#define NORMAL  0#define ABNORMAL        1#define ON              1#define OFF             0void alarm_f(int i) { return; }putfile(char *host,char *user,char *password,char *lf,char *rf,char *em){	FTPINFO ftpinfo;	void check_n_close ( FTPINFO *, int,char *);	ftpinfo.debug = ON;	ftpinfo.transf_calc = ON;	ftpinfo.linger = OFF;	/*	* connect to peer at remote host.	if (ftp_prconnect ( &ftpinfo, host ) < 0)	{		sprintf(em,"连接(ftp_prconnect)失败.");		(void) check_n_close ( &ftpinfo, ABNORMAL,em );		return 1;	}	*/	// login 	/*	 * use ftp_login to login into the remote ftp server and quit.	 */        signal(SIGALRM,alarm_f);        alarm(TIMEOUT); 	if (ftp_login ( &ftpinfo, host, user, password, NULL ) < 0)	{		sprintf(em,"登录(login)到对方服务器失败");		(void) check_n_close ( &ftpinfo, ABNORMAL,em );		return 1;	}        signal(SIGALRM,SIG_IGN);	/*	 * set the idle time for this connection.	 */	// chdir	// bin	/*	 * set transfer mode to binary.	 */	if (ftp_binary ( &ftpinfo ) < 0)	{		sprintf(em,"binary.");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	// dir	// getfile	/* 	* do a 'get' from the remote host. 	*/        signal(SIGALRM,alarm_f);        alarm(TIMEOUTTR);	if (ftp_putfile ( &ftpinfo, rf,lf)< 0){        	signal(SIGALRM,SIG_IGN);		sprintf(em,"ftp_getfile");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	else {        	signal(SIGALRM,SIG_IGN);		sprintf(em,"文件大小:%ld字节,用时%.2g秒,速率%2.g千字节每秒",			ftpinfo.speed.size_bytes,			ftpinfo.speed.seconds,			ftpinfo.speed.kbs);	  /******************************************		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);	  ********************************************/	}	// check recieved file	// bye	/* 	* quit the FTP sessions decently. 	*/	if (ftp_bye( &ftpinfo ) < 0) {		sprintf(em,"ftp_bye.");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	/* 	* we're done with our job...so exit the program gracefully. 	*/	(void) check_n_close ( &ftpinfo, NORMAL ,em);	return 0;}getfile(char *host,char *user,char *password,char *rf,char *lf,char *em){	FTPINFO ftpinfo;	void check_n_close ( FTPINFO *, int,char *);	ftpinfo.debug = ON;	ftpinfo.transf_calc = ON;	ftpinfo.linger = OFF;	/*	* connect to peer at remote host.	if (ftp_prconnect ( &ftpinfo, host ) < 0)	{		sprintf(em,"连接(ftp_prconnect)失败.");		(void) check_n_close ( &ftpinfo, ABNORMAL,em );		return 1;	}	*/	// login 	/*	 * use ftp_login to login into the remote ftp server and quit.	 */        signal(SIGALRM,alarm_f);        alarm(TIMEOUT); 	if (ftp_login ( &ftpinfo, host, user, password, NULL ) < 0)	{		sprintf(em,"登录(login)到对方服务器失败");		(void) check_n_close ( &ftpinfo, ABNORMAL,em );		return 1;	}        signal(SIGALRM,SIG_IGN);	/*	 * set the idle time for this connection.	 */	// chdir	// bin	/*	 * set transfer mode to binary.	 */	if (ftp_binary ( &ftpinfo ) < 0)	{		sprintf(em,"binary.");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	// dir	// getfile	/* 	* do a 'get' from the remote host. 	*/	if (ftp_getfile ( &ftpinfo, rf,lf)< 0){		sprintf(em,"ftp_getfile");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	else {		sprintf(em,"文件大小:%ld字节,用时%.2g秒,速率%2.g千字节每秒",			ftpinfo.speed.size_bytes,			ftpinfo.speed.seconds,			ftpinfo.speed.kbs);	  /******************************************		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);	  ********************************************/	}	// check recieved file	// bye	/* 	* quit the FTP sessions decently. 	*/	if (ftp_bye( &ftpinfo ) < 0) {		sprintf(em,"ftp_bye.");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	/* 	* we're done with our job...so exit the program gracefully. 	*/	(void) check_n_close ( &ftpinfo, NORMAL ,em);	return 0;}remotedir(char *host,char *user,char *password,char *rf,char *lf,char *em){	FTPINFO ftpinfo;	void check_n_close ( FTPINFO *, int,char *);	ftpinfo.debug = ON;	ftpinfo.transf_calc = ON;	ftpinfo.linger = OFF;	/*	* connect to peer at remote host.	if (ftp_prconnect ( &ftpinfo, host ) < 0)	{		sprintf(em,"连接(ftp_prconnect)失败.");		(void) check_n_close ( &ftpinfo, ABNORMAL,em );		return 1;	}	*/	// login 	/*	 * use ftp_login to login into the remote ftp server and quit.	 */        signal(SIGALRM,alarm_f);        alarm(TIMEOUT); 	if (ftp_login ( &ftpinfo, host, user, password, NULL ) < 0)	{		sprintf(em,"登录(login)到对方服务器失败");		(void) check_n_close ( &ftpinfo, ABNORMAL,em );		return 1;	}        signal(SIGALRM,SIG_IGN);	/*	 * set the idle time for this connection.	 */	// chdir	// bin	/*	 * set transfer mode to binary.	if (ftp_binary ( &ftpinfo ) < 0)	{		sprintf(em,"binary.");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	 */	// dir	// getfile	/* 	* do a 'get' from the remote host. 	*/	if (ftp_dir ( &ftpinfo, rf,lf)< 0){		sprintf(em,"ftp_dir");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	// bye	/* 	* quit the FTP sessions decently. 	*/	if (ftp_bye( &ftpinfo ) < 0) {		sprintf(em,"ftp_bye.");		(void) check_n_close ( &ftpinfo, ABNORMAL ,em);		return 1;	}	/* 	* we're done with our job...so exit the program gracefully. 	*/	(void) check_n_close ( &ftpinfo, NORMAL ,em);	return 0;}void check_n_close ( FTPINFO *ftpinfo, int     status,char *m){	  char str[64];          if (ftpinfo -> sockfd >= 0)                  close (ftpinfo -> sockfd);          if (status == ABNORMAL)	  {                  sprintf(str,"error: %s\n", ftpinfo -> ftp_msg);		  strcat(m,str);	  }	  /**************          else                  sprintf(m,"success: %s\n", ftpinfo -> ftp_msg);          sprintf(m,"final reply from server: %d\n", ftpinfo -> reply);	  **************/}

⌨️ 快捷键说明

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