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

📄 ftp_get.c

📁 涵盖ftp封装的大部分库函数 以get命令为例演示,一个不错的ftp编程资料
💻 C
字号:
/************************************************************//** PROGRAM : ftp_get.c                                    **//** FUNCTION: test the ftp_api                             **//** SYSTEM  : Sco Open Server, UnixWare                    **//** COMPILED: cc                                           **//** LIBARY  : -lftp -lsocket                               **//** AUTHOR  : yiwu                                         **//** DATE    :                                 **//** MODIFY  :                                              **//************************************************************/#include "ftp_dir.h"#include <signal.h>#ifndef LEN#define LEN			128#endifvoid check_n_close( FTPINFO*, int );void Proc_Timeout( int sig ){	fprintf( stderr, "connect time out.\n" );	exit( 9 );}int main( int argc, char** argv ){	LOGININFO	logininfo;	char		lcz_file_buf[LEN][LEN];	int			i, li_ret=0;	memset( &logininfo, 0, sizeof(LOGININFO) );	strcpy( logininfo.trantime, "7200" );	logininfo.trantype = BINARY;	logininfo.tranmode = RECEIVE;	logininfo.debug = FALSE;		if( argc < 5 )	{		printf( "Usage: %s host user passwd remotefile1 [file2 [...]]\n", argv[0] );		printf( "\thost      : remote host_ip.\n" );		printf( "\tuser      : user name.\n" );		printf( "\tpasswd    : user passwd.\n" );		/*printf( "\tremotepath: remote file path.\n" ); */ /*rem by guoqiuliang in 2005.11.3*/		printf( "\tremotefile: remote file name[1,2...], at least 1.\n" );		exit( -1 );	}	strcpy( logininfo.remotehost, argv[1] );	strcpy( logininfo.username, argv[2] );	strcpy( logininfo.passwd, argv[3] );	/*strcpy( logininfo.remotepath, argv[4] ); */  /* rem by guoqiuliang in 2005.11.3*/	memset( lcz_file_buf, 0, sizeof(lcz_file_buf) );	for( i = 4; i < argc; i ++ )	{		strcpy( lcz_file_buf[i-4], argv[i] );	}	li_ret = ftpfile( &logininfo, lcz_file_buf, argc-4 );	if( li_ret < 0 )		exit( li_ret );	exit( 0 );}int ftpfile( LOGININFO *logininfo, char lca_file_buf[][LEN], int li_count ){	FTPINFO		ftpinfo;	char		lcz_remote_dir[129];	int			i;	struct 		sigaction sa;	memset( &ftpinfo, 0, sizeof(FTPINFO) );	sa.sa_handler = Proc_Timeout;	sa.sa_flags = 0;	sigemptyset( &sa.sa_mask );	sigaddset( &sa.sa_mask, SIGALRM );	sigaction( SIGALRM, &sa, NULL );	alarm( 60 );		/* connectto peer at remote host */	if( logininfo->debug )	{		printf( "connect remote=[%s]\n", logininfo->remotehost );	}	if( ftp_prconnect( &ftpinfo, logininfo->remotehost ) < 0 )	{		perror( "ftp_prconnect ERROR!\n" );		check_n_close( &ftpinfo, ABNORMAL );		return (1);	}	if( logininfo->debug )	{		printf( "connect ok.\n" );	}		if( logininfo->debug )	{		printf( "login: username=[%s], passwd=[%s]\n", \				logininfo->username, logininfo->passwd );	}	/* send user name to the remote server */	if( ftp_user( &ftpinfo, logininfo->username ) < 0 )	{		perror( "ftp_user ERROR!\n" );		check_n_close( &ftpinfo, ABNORMAL );		return (2);	}		/* send user passwd to the remote server */	if( ftp_passwd( &ftpinfo, logininfo->passwd ) < 0 )	{		perror( "ftp_passwd ERROR!\n" );		check_n_close( &ftpinfo, ABNORMAL );		return( 3 );	}	if( logininfo->debug )	{		printf( "login ok.\n" );	}		/* set the idle time to this connect *//*	if( ftp_idle( &ftpinfo, logininfo->trantime ) < 0 )	{		perror( "ftp_idle ERROR!\n" );		check_n_close( &ftpinfo, ABNORMAL );		return( 4 );	}*/		/* set transfer mode */	if( logininfo->debug )	{		printf( "set tran type...\n" );	}	if( ftp_settype( &ftpinfo, logininfo->trantype ) < 0 )	{		perror( "ftp_settype ERROR!\n" );		check_n_close( &ftpinfo, ABNORMAL );		return( 5 );	}	if( logininfo->debug )	{		printf( "set tran type ok.\n" );	}	/* change the remote dir */	/*if( logininfo->debug )	{		printf( "change dir=[%s]\n", logininfo->remotepath );	}	if( ftp_chdir( &ftpinfo, logininfo->remotepath ) < 0 )	{		perror( "ftp_chdir ERROR!\n" );		check_n_close( &ftpinfo, ABNORMAL );		return( 6 );	}	if( logininfo->debug )	{		printf( "change dir ok.\n" );	}*/    /* rem by guoqiuliang in 2005.11.3 */	/* get the remote file */	for( i = 0; i < li_count; i ++ )	{		if( ftp_getfile( &ftpinfo, lca_file_buf[i], lca_file_buf[i] ) < 0 )		{			printf( "get remote file [%s] failed.\n", lca_file_buf[i] );			return( 11 );		}	}	/* close the connect */	if( ftp_bye( &ftpinfo ) < 0 )	{		perror( "ftp_bye ERROR!\n" );		return ( 30 );	}		return ( 0 );}void check_n_close( 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 );}

⌨️ 快捷键说明

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