c语言 自动下载 ftp,C语言:自己编写的简易ftp客户端,包含(列表,进入目录,上传文件,下载文件,删除文件)功能...

//简易ftp客户端

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef struct sockaddr* sockaddrp;

//命令传输

int sockfd;

//数据传输

int sockfd1;

char buf[4096] = {};

//地址

char buf1[4096] = {};

//帐号、密码

char buf2[4096] = {};

//存放命令名

char tem1[255] = {};

//存放文件名

char tem2[255] = {};

struct sockaddr_in new_addr = {AF_INET};

void func()

{

sprintf(buf,"PASV\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

int a[4];

int b[2];

printf("%s\n",buf);

sscanf(buf,"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)",&a[0],&a[1],&a[2],&a[3],&b[0],&b[1]);

int msg = b[0]*256+b[1];

sockfd1 = socket(AF_INET,SOCK_STREAM,0);

new_addr.sin_port = htons(msg);

new_addr.sin_addr.s_addr = inet_addr("192.168.6.66");

connect(sockfd1,(sockaddrp)&new_addr,sizeof(new_addr));

}

//目录

void ls_run()

{

sprintf(buf,"LIST\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd1,buf,sizeof(buf),0);

printf("server:%s\n",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server:%s\n",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server:%s\n",buf);

}

//进入目录

void cd_run()

{

//-----------------------------------------------------

sprintf(buf,"CWD %s\n",tem2);

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

sprintf(buf,"PWD\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd1,buf,sizeof(buf),0);

printf("server1:%s",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server2:%s\n",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server3:%s\n",buf);

sprintf(buf1,"%s",buf);

func();

//-------------------------------------------------------

sprintf(buf,"LIST\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd1,buf,sizeof(buf),0);

printf("%s",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server1:%s\n",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server2:%s\n",buf);

}

//下载

void get_run()

{

func();

sprintf(buf,"RETR %s\n",tem2);

send(sockfd,buf,strlen(buf),0);

printf("%s",buf);

bzero(buf,sizeof(buf));

recv(sockfd1,buf,sizeof(buf),0);

int tmp=sizeof(new_addr);

int fd = open(tem2,O_WRONLY|O_CREAT|O_TRUNC,0644);

write(fd,buf,strlen(buf));

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("%s",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

close(fd);

}

//上传

void put_run()

{

char inf[1024] = {};

func();

int fd = open(tem2,O_RDONLY);

read(fd,inf,strlen(buf));

close(fd);

sprintf(buf,"STOR %s\n",tem2);

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("%s",buf);

sprintf(buf,"%s\n",inf);

send(sockfd1,buf,sizeof(buf),0);

close(sockfd1);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("%s",buf);

}

//删除

void delete_run()

{

func();

sprintf(buf,"DELE %s\n",tem2);

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("%s",buf);

//-------------------------------------------------

sprintf(buf,"LIST\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd1,buf,sizeof(buf),0);

printf("%s",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server1:%s\n",buf);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server2:%s\n",buf);

}

//登陆

void login(void)

{

recv(sockfd,buf,sizeof(buf),0);

printf("server:%s",buf);

//用户帐号输入

printf("输入用户名:");

gets(buf2);

sprintf(buf,"%s %s\n","USER",buf2);

//sprintf(buf,"USER EOS1806\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

//printf("server:%s",buf);

//用户密码输入

printf("输入密码:");

gets(buf2);

sprintf(buf,"%s %s\n","PASS",buf2);

//sprintf(buf,"PASS iqot1806\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server:%s",buf);

sprintf(buf,"SYST\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server:%s",buf);

sprintf(buf,"FEAT\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("server:%s",buf);

sprintf(buf,"OPTS UTF8 ON\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("%s",buf);

sprintf(buf,"PWD\n");

send(sockfd,buf,strlen(buf),0);

bzero(buf,sizeof(buf));

recv(sockfd,buf,sizeof(buf),0);

printf("%s",buf);

//功能

while(1)

{

printf("ftp> ");

//获取命令

gets(buf);

//命令分支

if(!strcmp(buf,"ls"))

{

func();

ls_run();

close(sockfd1);

continue;

}

if(!strcmp(buf,"bye"))

{

break;

}

sscanf(buf,"%s %s",tem1,tem2);

if(!strcmp(tem1,"cd") )

{

cd_run();

close(sockfd1);

continue;

}

if(!strcmp(tem1,"get"))

{

get_run();

close(sockfd1);

continue;

}

if(!strcmp(tem1,"put"))

{

put_run();

continue;

}

if(!strcmp(tem1,"delete"))

{

delete_run();

close(sockfd1);

continue;

}

}

}

//主函数

int main(int argc,char* argv[])

{

//socket

sockfd = socket(AF_INET,SOCK_STREAM,0);

if(0 > sockfd)

{

perror("socket");

return -1;

}

struct sockaddr_in addr = {AF_INET};

addr.sin_port = htons(21);

addr.sin_addr.s_addr = inet_addr(argv[1]);

int ret = connect(sockfd,(sockaddrp)&addr,sizeof(addr));

if(0 > ret)

{

perror("connect");

return -1;

}

login();

close(sockfd);

}

ftp客户端ftpclient纯C语言winsock实现socket编程 /* *本程序是2009年计算机网络课程设计作品。 *本程序参考rfc959标准。能准确与遵守此标准的服务器进行信息交互。 *本人不保留任何版权。 *本程序仅供学习研究测试使用。因使用本程序所有或部分代码所产生的任何后果,本人均不负任何法律责任。 *2009年7月13日 */ #include #include #include /*system()*/ #include #pragma comment(lib,"ws2_32.lib") #define ONUM 512 #define MNUM 512 #define FNUM 512 #define pt struct host { char ip[20]; unsigned short port; }; SOCKET ts; fd_set readfds; struct timeval timeval; struct host host; char renum[4]; char ordertemp[ONUM]; char order[ONUM]; char ordercp[ONUM]; char mess[MNUM]; char file[FNUM]; char setpath[FNUM]; char setpathf[FNUM]; int i,door,r,sys,seti;/*i for;door switch;r receive num;sys system state;set set state*/ char *p;/*strtok*/ int printmess(); void input(char ordertemp[]); int ftp(); int user(); int pass(); int command(); int list(); SOCKET createDataSocket(); int set(); int retr(); int stor(); int stor() { char filename[256]; SOCKET ds; int wi; int r2,r3,r4,bsnum,brnum; FILE *fp=NULL; set(); memset(filename,'\0',256); memset(order,'\0',ONUM); for(i=5;ibsnum); }/*while*/ printf("\n"); switch(printmess()) { case 425: case 426: case 451: case 551: case 552:closesocket(ds);fclose(fp);return -1; case 250: case 226:fclose(fp);return 0; default:return 0; } }/*stor*/ int retr() { char filename[256]; unsigned long fsize,wfsize; int r2,wi; SOCKET ds; FILE *fp=NULL; memset(filename,'\0',256); memset(order,'\0',ONUM); for(i=5;i<=200&&ordercp[i]!='\0';i++)order[i-5]=ordercp[i]; strcpy(filename,setpathf); strcat(filename,"\\\\"); strcat(filename,order); ds=createDataSocket(); if(ds==-1)return -1; memset(order,'\0',ONUM); strcpy(order,"type i\r\n");/*type i 二进制 type a ASCII*/ send(ts,order,strlen(order),0); switch(printmess()) { case 421:closesocket(ds);closesocket(ts);sys=0;return 421; case 530:closesocket(ds);sys=1;return -1; case 500: case 501: case 504: case 226:closesocket(ds);return -1; case 200:break; default:closesocket(ds);return -1; } strcat(ordercp,"\r\n"); send(ts,ordercp,strlen(ordercp),0); switch(printmess()) { case 421:closesocket(ds);closesocket(ts);sys=0;return 421; case 530:closesocket(ds);sys=1;return -1; case 450: case 500: case 501: case 550:closesocket(ds);return -1; case 125: case 150:break; default:closesocket(ds);return -1; } set(); system(setpath); fp=fopen(filename,"wb"); if(!fp){printf("create file fail!\n");closesocket(ds);printmess();return -1;} wi=1;fsize=0;wfsize=0; while(wi) { memset(file,'\0',FNUM); r=recv(ds,file,FNUM,0); if(r==SOCKET_ERROR) { printf("file recv error!\n"); closesocket(ds); fclose(fp); return -1; } fsize=fsize+r; if(r==0){wi=0;break;} printf("receive %8d bytes! have received %16d bytes!\r",r,fsize); r2=fwrite(file,sizeof(char),r,fp); fflush(fp);/*这里是关键*/ wfsize=wfsize+r2; printf("write %8d bytes!have written %20d bytes!\r",r2,wfsize); }/*while*/ printf("\n"); switch(printmess()) { case 425: case 426: case 451:closesocket(ds);fclose(fp);return -1; case 250: case 226: closesocket(ds); wi=1; while(wi)if(fclose(fp)==0)wi=0;return 0; default:return 0; } }/*retr()*/ 以下代码请下载本程序。VC++6.0编译通过
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值