当前位置: 技术问答>linux和unix
求高手简答,ftp客户端问题,程序贴在这里。
来源: 互联网 发布时间:2016-09-16
本文导语: /* * ===================================================================================== * * Filename: ftpclient.c * * Description: ftpclient * * Version: 1.0 * Created: 2010年08月04日 08时40分15秒...
/*
* =====================================================================================
*
* Filename: ftpclient.c
*
* Description: ftpclient
*
* Version: 1.0
* Created: 2010年08月04日 08时40分15秒
* Revision: none
* Compiler: gcc
*
* Author: Yang Shao Kun (), cdutyangshaokun@163.com
* Company: College of Information Engineering of CDUT
*
* =====================================================================================
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_PORT 21
#define MAXSIZE 1024
#define SA struct sockaddr
void login();
void zeromery(char *arry,int len);
typedef struct hoststruct
{
unsigned int hostip;
int npsupport;
struct hoststruct *next;
}host1;
void main(int argc,char **argv)
{
printf("ftp>");
char command[MAXSIZE];
char*cmd;
scanf("%s",command);
cmd=command;
while(*(cmd)==' ')
cmd++;
if(strncmp(cmd,"login",5)==0)
{
login();
}
/*else if (strncmp(cmd,"quit",4)==0)
{
printf("bye^_^");
exit(1);
}*/
}
void login()
{
//初始化端口信息
int sockfd;
struct sockaddr_in serv_addr;
char senddate,recvdate;
char sendline[MAXSIZE],recvline[MAXSIZE];
struct hostent *host;
char name[1024],password[1024];
//获取hostent中相关参数
printf("please enter the hostnamen");
printf("ftp-> ");
scanf("%s",name);
host=gethostbyname(name);
if(host==NULL)
{
printf("get host by name is error");
exit(1);
}
//创建socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfdh_addr,addr,1024));
recvdate=recv(sockfd,recvline,sizeof(recvline),0);
if(recvdate==-1)
{
printf("recvdate is connect errorn");
exit(1);
}
else if(strncmp(recvline,"220",3)==0)
{
printf("connect success,pelase enter usernamen");
}
else
{
printf("220 connect is error!");
exit(1);
}
//ftp用户登录主体部分
int sendbytes,recvbytes;
zeromery(name,1024);
zeromery(password,1024);
zeromery(recvline,1024);
zeromery(sendline,1024);
printf("ftp-> ");
scanf("%s",name);//可以支持匿名登录vsftpd
strcat(sendline,"USER ");
strcat(sendline,name);
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(sendline),0);
if(sendbytes==-1)
{
printf("send is wrongn");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strncmp(recvline,"331",3)==0)
{
printf("331 please specify the password.n");
}
else
{
printf("recv date is error.n");
exit(1);
}
zeromery(sendline,1024);
zeromery(recvline,1024);
printf("ftp-> ");
scanf("%s",password);
strcat(sendline,"PASS ");
strcat(sendline,password);
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(sendline),0);
if(sendbytes==-1)
{
printf("pass send is errorn");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strncmp(recvline,"230",3)==0)
{
printf("login success!wait for command...n");
}
else
{
printf("pass recv is errorn");
printf("%d",recvbytes);
exit(1);
}
//支持断点续传
int npsupport;
zeromery(sendline,1024);
zeromery(recvline,1024);
strcat(sendline,"REST ");
strcat(sendline,"100");
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(sendline),0);
if(sendbytes==-1)
{
printf("rest send is error!n");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strstr(recvline,"350 restarting at 100")!=0)
{
npsupport=1;
printf("support 断点续传n");
}
if(recvbytes==-1)
{
printf("rest recv date is error.n");
exit(1);
}
else
{
npsupport=0;
printf("not support 断点续传n");
}
//获取服务器版本信息
zeromery(recvline,1024);
zeromery(sendline,1024);
strcat(sendline,"SYST");
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(1024),0);
if(sendbytes==-1)
{
printf("syst send is errorn");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strncmp(recvline,"215",3)==0)
{
printf("%s",recvline);
}
if(recvbytes==-1)
{
printf("syst recv is errorn");
exit(1);
}
else
{
printf("syst recv connectin is errorn");
exit(1);
}
}
void zeromery(char *arry,int len)
{
int i;
len=sizeof(arry);
for(i=0;i
* =====================================================================================
*
* Filename: ftpclient.c
*
* Description: ftpclient
*
* Version: 1.0
* Created: 2010年08月04日 08时40分15秒
* Revision: none
* Compiler: gcc
*
* Author: Yang Shao Kun (), cdutyangshaokun@163.com
* Company: College of Information Engineering of CDUT
*
* =====================================================================================
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_PORT 21
#define MAXSIZE 1024
#define SA struct sockaddr
void login();
void zeromery(char *arry,int len);
typedef struct hoststruct
{
unsigned int hostip;
int npsupport;
struct hoststruct *next;
}host1;
void main(int argc,char **argv)
{
printf("ftp>");
char command[MAXSIZE];
char*cmd;
scanf("%s",command);
cmd=command;
while(*(cmd)==' ')
cmd++;
if(strncmp(cmd,"login",5)==0)
{
login();
}
/*else if (strncmp(cmd,"quit",4)==0)
{
printf("bye^_^");
exit(1);
}*/
}
void login()
{
//初始化端口信息
int sockfd;
struct sockaddr_in serv_addr;
char senddate,recvdate;
char sendline[MAXSIZE],recvline[MAXSIZE];
struct hostent *host;
char name[1024],password[1024];
//获取hostent中相关参数
printf("please enter the hostnamen");
printf("ftp-> ");
scanf("%s",name);
host=gethostbyname(name);
if(host==NULL)
{
printf("get host by name is error");
exit(1);
}
//创建socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfdh_addr,addr,1024));
recvdate=recv(sockfd,recvline,sizeof(recvline),0);
if(recvdate==-1)
{
printf("recvdate is connect errorn");
exit(1);
}
else if(strncmp(recvline,"220",3)==0)
{
printf("connect success,pelase enter usernamen");
}
else
{
printf("220 connect is error!");
exit(1);
}
//ftp用户登录主体部分
int sendbytes,recvbytes;
zeromery(name,1024);
zeromery(password,1024);
zeromery(recvline,1024);
zeromery(sendline,1024);
printf("ftp-> ");
scanf("%s",name);//可以支持匿名登录vsftpd
strcat(sendline,"USER ");
strcat(sendline,name);
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(sendline),0);
if(sendbytes==-1)
{
printf("send is wrongn");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strncmp(recvline,"331",3)==0)
{
printf("331 please specify the password.n");
}
else
{
printf("recv date is error.n");
exit(1);
}
zeromery(sendline,1024);
zeromery(recvline,1024);
printf("ftp-> ");
scanf("%s",password);
strcat(sendline,"PASS ");
strcat(sendline,password);
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(sendline),0);
if(sendbytes==-1)
{
printf("pass send is errorn");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strncmp(recvline,"230",3)==0)
{
printf("login success!wait for command...n");
}
else
{
printf("pass recv is errorn");
printf("%d",recvbytes);
exit(1);
}
//支持断点续传
int npsupport;
zeromery(sendline,1024);
zeromery(recvline,1024);
strcat(sendline,"REST ");
strcat(sendline,"100");
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(sendline),0);
if(sendbytes==-1)
{
printf("rest send is error!n");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strstr(recvline,"350 restarting at 100")!=0)
{
npsupport=1;
printf("support 断点续传n");
}
if(recvbytes==-1)
{
printf("rest recv date is error.n");
exit(1);
}
else
{
npsupport=0;
printf("not support 断点续传n");
}
//获取服务器版本信息
zeromery(recvline,1024);
zeromery(sendline,1024);
strcat(sendline,"SYST");
strcat(sendline,"rn");
printf("--->%sn",sendline);
sendbytes=send(sockfd,sendline,sizeof(1024),0);
if(sendbytes==-1)
{
printf("syst send is errorn");
exit(1);
}
recvbytes=recv(sockfd,recvline,sizeof(recvline),0);
if(strncmp(recvline,"215",3)==0)
{
printf("%s",recvline);
}
if(recvbytes==-1)
{
printf("syst recv is errorn");
exit(1);
}
else
{
printf("syst recv connectin is errorn");
exit(1);
}
}
void zeromery(char *arry,int len)
{
int i;
len=sizeof(arry);
for(i=0;i