zoukankan      html  css  js  c++  java
  • linux开发板C实现ftp客户端

    由于需要和windows服务器连接,使用的是ftp通信协议,windows上使用filezilla作为服务器

    关于filazilla参考前面记录的  https://www.cnblogs.com/ChenChangXiong/p/11001004.html

    会有linux开发板的ftp工具,但是实际上也是socket,所有使用socket就可以做到ftp文件传输,这样也可以根据实际情况比较好的控制,所以我 使用C实现了ftp的客户端程序。

    以下是登陆代码代码

    int login()
      {
      //初始化端口信息
      struct sockaddr_in serv_addr;
      char senddate,recvdate;
      char sendline[MAXSIZE],recvline[MAXSIZE];
      struct hostent *host;
       
      //获取hostent中相关参数
      char name[MAXSIZE],password[MAXSIZE];
      printf("please enter the hostname ");
      printf("ftp->");
      fflush(stdout);
      //创建socket
      if( (control_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
      printf("create socket error: %s(errno: %d) ", strerror(errno),errno);
      return -1 ;
      //exit(0);
      }
      memset(&serv_addr, 0, sizeof(serv_addr));
      serv_addr.sin_family=AF_INET;
      serv_addr.sin_port=htons(SERV_PORT);
      serv_addr.sin_addr.s_addr=INADDR_ANY;
      //点分十进制转化为二进制整数地址
      if(inet_pton(AF_INET, FTP_CONNECT_IP, &serv_addr.sin_addr) <= 0){
      printf("inet_pton error for %s ", FTP_CONNECT_IP);
      close(control_sockfd);
      return -1 ;
      //exit(0);
      }
      //调用connect函数发起连接
      if((connect(control_sockfd,(SA*)&serv_addr,sizeof(serv_addr)))<0)
      {
      printf("连接失败 ");
      login_yes=0;
      }
      ///printf("连接到 %d--->>>/n",&serv_addr.sin_addr);
      recvdate=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvdate==-1)
      {
      printf("recvdate is connect error/n");
      login_yes=0;
      }
      else if(strncmp(recvline,"220",3)==0)
      {
      printf("220 连接成功,请输入用户名 ");
      login_yes=1;
      }
      else
      {
      printf("220 connect is error!");
      login_yes=0;
      }
       
      //ftp用户登录主体部分
      int sendbytes,recvbytes;
      zeromery(name,1024);
      zeromery(password,1024);
      zeromery(recvline,1024);
      zeromery(sendline,1024);
       
      //printf("自动登录 用户名:windows-ftp ") ;
      strcat(sendline,"USER ");
      strcat(sendline,"windows-ftp");
      strcat(sendline," ");
      printf("--->%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
       
      if(sendbytes==-1)
      {
      printf("send is wrong ");
      login_yes=0;
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(strncmp(recvline,"331",3)==0)
      {
      //printf("331 please specify the password./n");
      printf("331 请输入密码 ");
      }
      else
      {
      printf("recv date is error./n");
      login_yes=0;
      }
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      sleep(1) ;
      printf("密码:111111 ") ;
      strcat(sendline,"PASS ");
      strcat(sendline,"111111");
      strcat(sendline," ");
      printf("--->%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
       
      if(sendbytes==-1)
      {
      printf("pass send is error ");
      login_yes=0;
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
       
      if(strncmp(recvline,"230",3)==0)
      {
      printf("登录成功! ");
      login_yes=1;
      }
      else
      {
      printf("pass recv is error ");
      login_yes=0;
      }
       
      if(login_yes==0)
      {
      close(control_sockfd); //登陆失败 关闭接口
      return -1 ;
      }
      //进入到对应子路径
      //////ftp_changdir("001",control_sockfd); //cd
       
      return control_sockfd;
      }
       

    实现了登陆,就意味着可以和service进行通信了,接下来的事情九四read  和 write  或者send和rev的事情。

    #include "client_ftp.h"
       
      static char First_login = 0 ;
      //int main(int argc,char **argv)
      //{
      // printf("ftp>");
      // char command[MAXSIZE];
      // char*cmd;
      // login();
      // if(login_yes==1)
      // {
      // while(1)
      // {
      //comm:
      // sleep(1);
      // printf("ftp>");
      // zeromery(command,1024);
      // scanf("%s",command);
      // cmd=command;
      // while(*(cmd)==' ')
      // cmd++;
      // if(strncmp(cmd,"pwd",3)==0)
      // {
      // ftp_pwd(control_sockfd);
      // }
      // if(strncmp(cmd,"mkdir",5)==0)
      // {
      // char path[60];
      // zeromery(path,60);
      // printf("创建的路径名: ");
      // scanf("%s",path);
      // printf("s ",path);
      // ftp_creat_mkd(path,control_sockfd);
      // }
      // if(strncmp(cmd,"back",4)==0)
      // {
      // ftp_back(control_sockfd);
      // ftp_pwd(control_sockfd);
      // }
      // if(strncmp(cmd,"cd",2)==0)
      // {
      // int i;
      // char path[60];
      // zeromery(path,60);
      // printf("要到的路径:");
      // scanf("%s",path);
      // printf("%s ",path);
      // ftp_changdir(path,control_sockfd);
      // }
      // if(strncmp(cmd,"get",3)==0)
      // {
      // ftp_pwd(control_sockfd);
      // ftp_download(control_sockfd);
      // }
      // if(strncmp(cmd,"up",3)==0)
      // {
      // ftp_pwd(control_sockfd);
      // ftp_up(control_sockfd);
      // }
      // if(strncmp(cmd,"quit",4)==0)
      // {
      // printf("bye^_^ ");
      // close(control_sockfd);
      // break;
      // }
      // ///printf("支持 list,pwd,mkdir,back,cd,up,get ");
      // }
      //
      // }
      // else if(login_yes==0)
      // {
      // int i;//不成功登录下最多还有两次机会,如果不能在两次登录,则,关闭链接。
      // printf("Can not login vsftpd");
      // for(i=2;i>0;i--)
      // {
      // printf("你还有 %d 登录机会 ",i);
      // login();
      // if(login_yes==1)
      // {
      // goto comm;
      // }
      // }
      // if(i==0)
      // {
      // printf("你不能在登录! ");
      // close(control_sockfd);
      // }
      //
      // }
      // else if (strncmp(cmd,"quit",4)==0)
      // {
      // ftp_quit(control_sockfd);
      // close(control_sockfd);
      //
      // }
      //
      // return 0;
      //}
      int login()
      {
      //初始化端口信息
      struct sockaddr_in serv_addr;
      char senddate,recvdate;
      char sendline[MAXSIZE],recvline[MAXSIZE];
      struct hostent *host;
       
      //获取hostent中相关参数
      char name[MAXSIZE],password[MAXSIZE];
      printf("please enter the hostname ");
      printf("ftp->");
      fflush(stdout);
      //创建socket
      if( (control_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
      printf("create socket error: %s(errno: %d) ", strerror(errno),errno);
      return -1 ;
      //exit(0);
      }
      memset(&serv_addr, 0, sizeof(serv_addr));
      serv_addr.sin_family=AF_INET;
      serv_addr.sin_port=htons(SERV_PORT);
      serv_addr.sin_addr.s_addr=INADDR_ANY;
      //点分十进制转化为二进制整数地址
      if(inet_pton(AF_INET, FTP_CONNECT_IP, &serv_addr.sin_addr) <= 0){
      printf("inet_pton error for %s ", FTP_CONNECT_IP);
      close(control_sockfd);
      return -1 ;
      //exit(0);
      }
      //调用connect函数发起连接
      if((connect(control_sockfd,(SA*)&serv_addr,sizeof(serv_addr)))<0)
      {
      printf("连接失败 ");
      login_yes=0;
      }
      ///printf("连接到 %d--->>>/n",&serv_addr.sin_addr);
      recvdate=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvdate==-1)
      {
      printf("recvdate is connect error/n");
      login_yes=0;
      }
      else if(strncmp(recvline,"220",3)==0)
      {
      printf("220 连接成功,请输入用户名 ");
      login_yes=1;
      }
      else
      {
      printf("220 connect is error!");
      login_yes=0;
      }
       
      //ftp用户登录主体部分
      int sendbytes,recvbytes;
      zeromery(name,1024);
      zeromery(password,1024);
      zeromery(recvline,1024);
      zeromery(sendline,1024);
       
      //printf("自动登录 用户名:windows-ftp ") ;
      strcat(sendline,"USER ");
      strcat(sendline,"windows-ftp");
      strcat(sendline," ");
      printf("--->%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
       
      if(sendbytes==-1)
      {
      printf("send is wrong ");
      login_yes=0;
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(strncmp(recvline,"331",3)==0)
      {
      //printf("331 please specify the password./n");
      printf("331 请输入密码 ");
      }
      else
      {
      printf("recv date is error./n");
      login_yes=0;
      }
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      sleep(1) ;
      printf("密码:111111 ") ;
      strcat(sendline,"PASS ");
      strcat(sendline,"111111");
      strcat(sendline," ");
      printf("--->%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
       
      if(sendbytes==-1)
      {
      printf("pass send is error ");
      login_yes=0;
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
       
      if(strncmp(recvline,"230",3)==0)
      {
      printf("登录成功! ");
      login_yes=1;
      }
      else
      {
      printf("pass recv is error ");
      login_yes=0;
      }
       
      if(login_yes==0)
      {
      close(control_sockfd); //登陆失败 关闭接口
      return -1 ;
      }
      //进入到对应子路径
      //////ftp_changdir("001",control_sockfd); //cd
       
      return control_sockfd;
      }
       
      //数组初始化
      void zeromery(char *a,int len)
      {
      int i;
      len=sizeof(a);
      for(i=0;i<len;i++)
      {
      a[i]=0;
      }
      }
       
      //quit函数,control_sockfd,通过实参传递
      void ftp_quit(int control_sockfd )
      {
      char sendline[1024];
      char recvline[1024];
      int recvbytes;
      int sendbytes;
       
      First_login = 0 ; //重新恢复到开始目录
       
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      strcat(sendline,"QUIT");
      strcat(sendline," ");
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("quit send is error! ");
      goto END;
      //exit(1);
      }
      recvbytes=recv(control_sockfd,recvline,strlen(recvline),0);
      if(strncmp(recvline,"221",3)==0)
      {
      printf("221 bye!^_^");
      goto END;
      //exit(1);
      }
      else
      {
      printf("quit recv is error! ");
      goto END;
      //exit(1);
      }
      END:
      close(control_sockfd);
      return ;
      }
       
       
      //mkd,在所在路径中创建目录 函数
      void ftp_creat_mkd(char *path,int control_sockfd)
      {
      char sendline[1024];
      char recvline[1024];
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      int recvbytes,sendbytes;
      int issuccess;
      strcat(sendline,"MKD ");
      strcat(sendline,path);
      strcat(sendline," ");
      printf("%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("mkd send is error!");
      exit(1);
      }
      recvbytes=recv(control_sockfd,recvline,strlen(recvline),0);
      if(strncmp(recvline,"257",3)==0)
      {
      issuccess=1;
      }
      else
      {
      issuccess=0;
      }
      }
       
       
      //改变目录函数chdir
      void ftp_changdir(char *dir,int control_sockfd)
      {
       
      char sendline[1024];
      char recvline[1024];
      int recvbytes,sendbytes;
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      strcat(sendline,"CWD ");
      strcat(sendline,dir);
      strcat(sendline," ");
      printf("%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("cwd send is error! ");
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvbytes<0)
      {
      printf("cwd recv is error! ");
      }
      if(strncmp(recvline,"250",3)==0)
      {
      char buf[55];
      snprintf(buf,39,">>> %s ",recvline);
      printf("%s ",buf);
      }
      else
      {
      printf("cwd chdir is error! ");
      return ;
      //exit(1);
      }
      //登录成功
      First_login = 1 ;
      }
       
       
      //pwd 命令函数
      //在应答中返回当前工作目录,“pwd”+
      void ftp_pwd(int control_sockfd)
      {
      int recvbytes,sendbytes;
      char sendline[1024],recvline[1024];
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      strcat(sendline,"PWD");
      strcat(sendline," ");
       
      if(First_login==0)
      {
      //进入到对应子路径
      //////ftp_changdir("001",control_sockfd); //cd
      }
       
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("pwd,send is error ");
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(strncmp(recvline,"257",3)==0)
      {
      int i=0;
      char *ptr;
      char currendir[1024];
      zeromery(currendir,1024);
      ptr=recvline+5;
      while(*(ptr)!='"')
      {
      currendir[i++]=*(ptr);
      ptr++;
      }
      currendir[i]='';
      printf("current directory is:%s ",currendir);
       
      }
      else
      {
      printf("pwd,recv is error! ");
      }
      }
      //back 返回上一级函数,相当于cd /;
      void ftp_back(int control_sockfd)
      {
      char sendline[1024],recvline[1024];
      int recvbytes,sendbytes;
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      strcat(sendline,"CDUP");
      strcat(sendline," ");
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("cdup send is error ! ");
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvbytes<0)
      {
      printf("cdup recv is error ! ");
      }
      if(strncmp(recvline,"250",3)==0)
      {
      printf("请求的文件操作已经成功 ");
      }
      }
       
      //下载的实现函数
      int ftp_download(int control_sockfd)
      {
       
      int pasv_or_port;// 定义the ftp协议的两种不同工作mode
      int recvbytes,sendbytes;
      char sendline[1024],recvline[1024];
      struct sockaddr_in serv_addr;
      FILE *fd;
      int i,j;
      int data_sockfd;
      //type
      zeromery(recvline,1024);
      zeromery(sendline,1024);
      strcat(sendline,"TYPE ");
      strcat(sendline,"I");
      strcat(sendline," ");
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf(" type send is error! ");
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(strncmp(recvline,"200",3)==0)
      {
      printf("使用二进制传输数据 ");
      }
      else
      {
      printf("type recv is error! ");
      }
       
      if(npsupport==0)
      {
      //open the file
      int size;
      char localpathname[60];//预打开的文件路径字符串
      int flags;
      char pathname[60];
      unsigned int mode;
      //用户来选择pasv 或者是 port mode
      char selectdata_mode_tran[1024];
      zeromery(selectdata_mode_tran,1024);
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      pasv_or_port=0;//(默认是pasv模式)
      //pasv mode
      if(pasv_or_port==0)
      {
      strcat(sendline,"PASV");
      strcat(sendline," ");
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("pasv send is error! ");
      }
      zeromery(recvline,1024);
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvbytes<0)
      {
      printf("pasv recv is error! ");
      }
      if(strncmp(recvline,"227",3)==0)
      {
      char buf[55];
      snprintf(buf,51,">>> %s ",recvline);
      printf("%s ",buf);
      }
      else
      {
      printf("pasv recv is error! ");
      }
      //处理ftp server 端口
      char *ptr1,*ptr2;
      char num[1024];
      zeromery(num,1024);
      //取低位字节
      ptr1=recvline+strlen(recvline);
      while(*(ptr1)!=')')
      {
      ptr1--;
      }
      ptr2=ptr1;
      while(*(ptr2)!=',')
      ptr2--;
      strncpy(num,ptr2+1,ptr1-ptr2-1);
      i=atoi(num);//将字符串转换成整数
      //取高位字节
      zeromery(num,1024);
      ptr1=ptr2;
      ptr2--;
      while(*(ptr2)!=',')
      ptr2--;
      strncpy(num,ptr2+1,ptr1-ptr2-1);
      j=atoi(num);
      //初始化服务器数据连接时的端口信息
      int data_serviceport;
      data_serviceport=j*256+i;
      data_sockfd=socket(AF_INET,SOCK_STREAM,0);
       
      serv_addr.sin_family=AF_INET;
      serv_addr.sin_addr.s_addr=INADDR_ANY;
      serv_addr.sin_port=htons(data_serviceport);
      if(connect(data_sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr))==-1)
      {
      printf("pasv data connect is error! ");
      }
      printf("remote-file-pathname=");
      scanf("%s",pathname);
      printf("local-file-pathname=");
      scanf("%s",localpathname);
      printf("local:%s remore:%s ",localpathname,pathname);
      fd=fopen(localpathname,"w+");
      if(fd==NULL)
      {
      printf("cannot open file ");
      exit(1);
      }
       
       
      //send the command retr;
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      strcat(sendline,"RETR ");
      strcat(sendline,pathname);
      strcat(sendline," ");
      printf("%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("retr send is error! ");
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvbytes<0)
      {
      printf("retr recv is error! ");
      }
      if(strncmp(recvline,"400",3)>0)
      {
      printf("return is error! ");
      }
      }
       
      //begin to transpotr data
      sleep(1);
      int flag=0;
      char buffer[65536];
      recvdata:
      zeromery(buffer,1024);
      recvbytes=recv(data_sockfd,buffer,sizeof(buffer),0);
      if(recvbytes<0)
      {
      close(data_sockfd);
      goto end;
      }
      fwrite(buffer,1,recvbytes,fd);
      zeromery(recvline,1024);
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(flag==0)
      {
      if(strncmp(recvline,"226",3)!=0)
      {
      flag=1;
      goto recvdata;
      }
      }
      end:
      if(flag!=1)
      {
      zeromery(recvline,1024);
      shutdown(data_sockfd,SHUT_WR);
      close(data_sockfd);
      }
      close(data_sockfd);
      return 0;
      }
      }
       
       
      //up 函数
      int ftp_up(int control_sockfd, char* localpathname, char* pathname)
      {
       
      int pasv_or_port;// 定义the ftp协议的两种不同工作mode
      int recvbytes,sendbytes;
      char sendline[1024],recvline[1024];
      struct sockaddr_in serv_addr;
      FILE *fd;
      int i,j;
      int data_sockfd;
       
      //type
      zeromery(recvline,1024);
      zeromery(sendline,1024);
      strcat(sendline,"TYPE ");
      strcat(sendline,"I");
      strcat(sendline," ");
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf(" type send is error! ");
      //发送失败 返回-2 启用重连
      return -2 ;
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(strncmp(recvline,"200",3)==0)
      {
      printf("使用二进制传输数据 ");
      }
      else
      {
      printf("type recv is error! ");
      }
       
      if(npsupport==0)
      {
      //open the file
      int size;
      //char localpathname[60];//预打开的文件路径字符串
      int flags;
      //char pathname[60];
      unsigned int mode;
      //用户来选择pasv 或者是 port mode
      char selectdata_mode_tran[1024];
      zeromery(selectdata_mode_tran,1024);
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      pasv_or_port=0;//(默认是pasv模式)
      //pasv mode
      if(pasv_or_port==0)
      {
      strcat(sendline,"PASV");
      strcat(sendline," ");
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("pasv send is error! ");
      return -2 ; //////////////////////////临时增加
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvbytes<0)
      {
      printf("pasv recv is error! ");
      return -2 ; //////////////////////////临时增加
      }
      if(strncmp(recvline,"227",3)==0)
      {
      char buf[55];
      snprintf(buf,51,">>> %s ",recvline);
      printf("%s ",buf);
      }
      else
      {
      printf("pasv recv is error! ");
      return -2 ; //////////////////////////临时增加
      }
      //处理ftp server 端口
      char *ptr1,*ptr2;
      char num[1024];
      zeromery(num,1024);
      //取低位字节
      ptr1=recvline+strlen(recvline);
      while(*(ptr1)!=')')
      {
      ptr1--;
      }
      ptr2=ptr1;
      while(*(ptr2)!=',')
      ptr2--;
      strncpy(num,ptr2+1,ptr1-ptr2-1);
      i=atoi(num);//将字符串转换成整数
      //取高位字节
      zeromery(num,1024);
      ptr1=ptr2;
      ptr2--;
      while(*(ptr2)!=',')
      ptr2--;
      strncpy(num,ptr2+1,ptr1-ptr2-1);
      j=atoi(num);
      //初始化服务器数据连接时的端口信息
      int data_serviceport;
      data_serviceport=j*256+i;
      data_sockfd=socket(AF_INET,SOCK_STREAM,0);
       
      serv_addr.sin_family=AF_INET;
      serv_addr.sin_addr.s_addr=INADDR_ANY;
      serv_addr.sin_port=htons(data_serviceport);
      if(inet_pton(AF_INET, FTP_CONNECT_IP, &serv_addr.sin_addr) <= 0){
      printf("inet_pton error for %s ", FTP_CONNECT_IP);
      exit(0);
      }
       
       
      if(connect(data_sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr))==-1)
      {
      printf("pasv data connect is error! ");
      }
      //printf("local-file-pathname=");
      //scanf("%s",pathname);
      //printf("remote-file-pathname=");
      //scanf("%s",localpathname);
      //printf("local:%s remore:%s ",localpathname,pathname);
       
      fd=fopen(pathname,"r");
      if(fd==NULL)
      {
      printf("cannot open file,请重新输入! ");
      return -2 ;
      }
       
      //send the command retr;
      zeromery(sendline,1024);
      zeromery(recvline,1024);
      strcat(sendline,"STOR ");
      strcat(sendline,localpathname);
      strcat(sendline," ");
      printf("%s ",sendline);
      sendbytes=send(control_sockfd,sendline,strlen(sendline),0);
      if(sendbytes<0)
      {
      printf("stor send is error! ");
      }
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(recvbytes<0)
      {
      printf("retr recv is error! ");
      }
      if(strncmp(recvline,"150",3)==0)
      {
      char buf[55];
      snprintf(buf,25,">>> %s ",recvline);
      printf("%s ",buf);
      }
      }
       
      while(!feof(fd))
      {
      char buffer[65536];
      zeromery(buffer,sizeof(buffer));
      int size;
      size=fread(buffer,1,sizeof(buffer),fd);
      if(ferror(fd))
      {
      printf("read file data is error! ");
      fclose(fd) ;
      return -2 ;
      }
      else
      {
      zeromery(sendline,1024);
      sendbytes=send(data_sockfd,buffer,size,0);
      printf("传输了 %d 个字节 ",sendbytes);
      //发送失败 返回-2 启用重连
      if(sendbytes<0)
      {
      fclose(fd) ;
      return -2 ;
      }
      }
      close(data_sockfd);
      recvbytes=recv(control_sockfd,recvline,sizeof(recvline),0);
      if(strncmp(recvline,"226",3)==0)
      {
      printf("226 transfer complete");
      break;
      }
      }
       
      fclose(fd) ;
      return 0;
      }
      }
  • 相关阅读:
    构建之法阅读笔记05
    构建之法阅读笔记04
    构建之法阅读笔记03
    学习进度条
    软件工程练习——买书
    软件工程练习——找水王2
    Java作业07
    Java课堂作业06
    读《大道至简》第六章有感
    Java课堂作业05
  • 原文地址:https://www.cnblogs.com/ChenChangXiong/p/10979434.html
Copyright © 2011-2022 走看看