阿里狼 发表于 2015-11-6 07:17:00

linux下用C编写ftp客户端

  这是一个大作业,要求能够模拟ftp协议,实现一个ftp客户端,然后要求能够实现相应的功能,主要是能够实现ls,pwd,cwd(cd),put和get功能。然后是在被动模式下来实现这些功能。那么首先我们需要对ftp协议有一个具体的了解,然后才能够自己实现这个功能。
  
FTP 概述
  
文件传输协议(FTP)作为网络共享文件的传输协议,在网络应用软件中具有广泛的应用。FTP的目标是提高文件的共享性和可靠高效地传送数据。
在传输文件时,FTP 客户端程序先与服务器建立连接,然后向服务器发送命令。服务器收到命令后给予响应,并执行命令。FTP 协议与操作系统无关,任何操作系统上的程序只要符合 FTP 协议,就可以相互传输数据。本文主要基于 LINUX 平台,对 FTP 客户端的实现原理进行详尽的解释并阐述如何使用 C 语言编写一个简单的 FTP 客户端。
回页首
FTP 协议
相比其他协议,如 HTTP 协议,FTP 协议要复杂一些。与一般的 C/S 应用不同点在于一般的C/S 应用程序一般只会建立一个 Socket 连接,这个连接同时处理服务器端和客户端的连接命令和数据传输。而FTP协议中将命令与数据分开传送的方法提高了效率。
FTP 使用 2 个端口,一个数据端口和一个命令端口(也叫做控制端口)。这两个端口一般是21 (命令端口)和 20 (数据端口)。控制 Socket 用来传送命令,数据 Socket 是用于传送数据。每一个 FTP 命令发送之后,FTP 服务器都会返回一个字符串,其中包括一个响应代码和一些说明信息。其中的返回码主要是用于判断命令是否被成功执行了。
命令端口
一般来说,客户端有一个 Socket 用来连接 FTP 服务器的相关端口,它负责 FTP 命令的发送和接收返回的响应信息。一些操作如“登录”、“改变目录”、“删除文件”,依靠这个连接发送命令就可完成。
数据端口
对于有数据传输的操作,主要是显示目录列表,上传、下载文件,我们需要依靠另一个 Socket来完成。
如果使用被动模式,通常服务器端会返回一个端口号。客户端需要用另开一个 Socket 来连接这个端口,然后我们可根据操作来发送命令,数据会通过新开的一个端口传输。
如果使用主动模式,通常客户端会发送一个端口号给服务器端,并在这个端口监听。服务器需要连接到客户端开启的这个数据端口,并进行数据的传输。
下面对 FTP 的主动模式和被动模式做一个简单的介绍。
主动模式 (PORT)
主动模式下,客户端随机打开一个大于 1024 的端口向服务器的命令端口 P,即 21 端口,发起连接,同时开放N +1 端口监听,并向服务器发出 “port N+1” 命令,由服务器从它自己的数据端口 (20) 主动连接到客户端指定的数据端口 (N+1)。
FTP 的客户端只是告诉服务器自己的端口号,让服务器来连接客户端指定的端口。对于客户端的防火墙来说,这是从外部到内部的连接,可能会被阻塞。
被动模式 (PASV)
为了解决服务器发起到客户的连接问题,有了另一种 FTP 连接方式,即被动方式。命令连接和数据连接都由客户端发起,这样就解决了从服务器到客户端的数据端口的连接被防火墙过滤的问题。
被动模式下,当开启一个 FTP 连接时,客户端打开两个任意的本地端口 (N > 1024 和 N+1) 。
第一个端口连接服务器的 21 端口,提交 PASV 命令。然后,服务器会开启一个任意的端口 (P > 1024 ),返回如“227 entering passive mode (127,0,0,1,4,18)”。 它返回了 227 开头的信息,在括号中有以逗号隔开的六个数字,前四个指服务器的地址,最后两个,将倒数第二个乘256 再加上最后一个数字,这就是 FTP 服务器开放的用来进行数据传输的端口。如得到 227 entering passive mode (h1,h2,h3,h4,p1,p2),那么端口号是 p1*256+p2,ip 地址为h1.h2.h3.h4。这意味着在服务器上有一个端口被开放。客户端收到命令取得端口号之后, 会通过 N+1 号端口连接服务器的端口 P,然后在两个端口之间进行数据传输。
这是ftp协议的基本了解。  然后就是代码来实现:
  
主要用到的 FTP 命令
  
USER: 指定用户名。通常是控制连接后第一个发出的命令。“USER gaoleyi\r\n”: 用户名为gaoleyi 登录。
PASS: 指定用户密码。该命令紧跟 USER 命令后。“PASS gaoleyi\r\n”:密码为 gaoleyi。
CWD: 改变工作目录。如:“CWD dirname\r\n”。
PASV: 让服务器在数据端口监听,进入被动模式。如:“PASV\r\n”。
PORT: 告诉 FTP 服务器客户端监听的端口号,让 FTP 服务器采用主动模式连接客户端。如:“PORT h1,h2,h3,h4,p1,p2”。
RETR: 下载文件。“RETR file.txt \r\n”:下载文件 file.txt。
STOR: 上传文件。“STOR file.txt\r\n”:上传文件 file.txt。
QUIT: 关闭与服务器的连接。
  
FTP 响应码
  
客户端发送 FTP 命令后,服务器返回响应码。
响应码用三位数字编码表示:
第一个数字给出了命令状态的一般性指示,比如响应成功、失败或不完整。
第二个数字是响应类型的分类,如 2 代表跟连接有关的响应,3 代表用户认证。
第三个数字提供了更加详细的信息。
第一个数字的含义如下:
1 表示服务器正确接收信息,还未处理。
2 表示服务器已经正确处理信息。
3 表示服务器正确接收信息,正在处理。
4 表示信息暂时错误。
5 表示信息永久错误。
第二个数字的含义如下:
0 表示语法。
1 表示系统状态和信息。
2 表示连接状态。
3 表示与用户认证有关的信息。
4 表示未定义。
5 表示与文件系统有关的信息。
Socket 编程的几个重要步骤
Socket 客户端编程主要步骤如下:

[*]socket() 创建一个 Socket
[*]connect() 与服务器连接
[*]write() 和 read() 进行会话
[*]close() 关闭 Socket

  FTP客户端实现登录功能:
  首先是打开一个tcp连接
  int cliopen(char *host,int port)
{
    int control_sock;
    struct hostent *ht = NULL;
    control_sock = socket(AF_INET,SOCK_STREAM,0);
    if(control_sock < 0)
    {
       printf(&quot;socket error\n&quot;);
       return -1;
    }
    ht = gethostbyname(host);
    if(!ht)
    {
      return -1;
    }
    //memcpy(&servaddr.sin_addr.s_addr,ht->h_addr,ht->h_length);
   
    memset(&servaddr,0,sizeof(struct sockaddr_in));
    memcpy(&servaddr.sin_addr.s_addr,ht->h_addr,ht->h_length);
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port);
   
    if(connect(control_sock,(struct sockaddr*)&servaddr,sizeof(struct sockaddr)) == -1)
    {
      return -1;
    }
    return control_sock;
}

  执行这个函数后,会返回一个新创建的TCP连接的序号,然后就是执行主函数了:cmd_tcp(); 注意这个函数中用到了一个select函数,这个函数是用来选择和监听从键盘输入还是服务器端返回,这个函数是socket编程中很重要的一个函数。
  void cmd_tcp(int sockfd)
{
    int maxfdp1,nread,nwrite,fd,replycode,tag=0,data_sock;
    int port;
    char *pathname;
    fd_set rset;
    FD_ZERO(&rset);
    maxfdp1 = sockfd &#43; 1;
    for(;;)
    {
         FD_SET(STDIN_FILENO,&rset);
         FD_SET(sockfd,&rset);
         if(select(maxfdp1,&rset,NULL,NULL,NULL)<0)
         {
             printf(&quot;select error\n&quot;);
         }
         if(FD_ISSET(STDIN_FILENO,&rset))                     //判断是从键盘输入的,还是从服务器端返回的
         {
            //nwrite = 0;
            //nread = 0;
            bzero(wbuf,MAXBUF);          //zero               //当然要注意的是,每次在重新读取缓冲区里的内容时,需要将原来的缓冲区清空,用到的是bzero函数,或者也可以用memset函数
            bzero(rbuf1,MAXBUF);
            if((nread = read(STDIN_FILENO,rbuf1,MAXBUF)) <0)
                   printf(&quot;read error from stdin\n&quot;);
            nwrite = nread &#43; 5;
            //printf(&quot;%d\n&quot;,nread);      
            if(replycode == USERNAME)
            {
                  sprintf(wbuf,&quot;USER %s&quot;,rbuf1);
            
               if(write(sockfd,wbuf,nwrite) != nwrite)
               {
                     printf(&quot;write error\n&quot;);
               }
               //printf(&quot;%s\n&quot;,wbuf);
               //memset(rbuf1,0,sizeof(rbuf1));
               //memset(wbuf,0,sizeof(wbuf));
               //printf(&quot;1:%s\n&quot;,wbuf);
            }


            if(replycode == PASSWORD)
            {
                   //printf(&quot;%s\n&quot;,rbuf1);
                   sprintf(wbuf,&quot;PASS %s&quot;,rbuf1);
                   if(write(sockfd,wbuf,nwrite) != nwrite)
                      printf(&quot;write error\n&quot;);
                   //bzero(rbuf,sizeof(rbuf));
                   //printf(&quot;%s\n&quot;,wbuf);
                   //printf(&quot;2:%s\n&quot;,wbuf);
            }
            if(replycode == 550 || replycode == LOGIN || replycode == CLOSEDATA || replycode == PATHNAME || replycode == ACTIONOK)
            {
          if(strncmp(rbuf1,&quot;pwd&quot;,3) == 0)
            {   
                     //printf(&quot;%s\n&quot;,rbuf1);
                     sprintf(wbuf,&quot;%s&quot;,&quot;PWD\n&quot;);
                     write(sockfd,wbuf,4);
                     continue;
               }
               if(strncmp(rbuf1,&quot;quit&quot;,4) == 0)
               {
                     sprintf(wbuf,&quot;%s&quot;,&quot;QUIT\n&quot;);
                     write(sockfd,wbuf,5);
                     //close(sockfd);
                  if(close(sockfd) <0)
                     printf(&quot;close error\n&quot;);
                  break;
               }
               if(strncmp(rbuf1,&quot;cwd&quot;,3) == 0)
               {
                     //sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);
                     sprintf(wbuf,&quot;%s&quot;,rbuf1);
                     write(sockfd,wbuf,nread);
                     
                     //sprintf(wbuf1,&quot;%s&quot;,&quot;CWD\n&quot;);
                     
                     continue;
               }
               if(strncmp(rbuf1,&quot;ls&quot;,2) == 0)
               {
                     tag = 2;
                     //printf(&quot;%s\n&quot;,rbuf1);
                     sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);
                     //printf(&quot;%s\n&quot;,wbuf);
                     write(sockfd,wbuf,5);
                     //read
                     //sprintf(wbuf1,&quot;%s&quot;,&quot;LIST -al\n&quot;);
                     nwrite = 0;
                     //write(sockfd,wbuf1,nwrite);
                     //ftp_list(sockfd);
                     continue;   
               }
               if(strncmp(rbuf1,&quot;get&quot;,3) == 0)
               {
                     tag = 1;
                     sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);                  
                     //printf(&quot;%s\n&quot;,s(rbuf1));
                     //char filename;
                     s(rbuf1,filename);
                     printf(&quot;%s\n&quot;,filename);
                     write(sockfd,wbuf,5);
                     continue;
               }
               if(strncmp(rbuf1,&quot;put&quot;,3) == 0)
               {
                     tag = 3;
                     sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);
                     st(rbuf1,filename);
                     printf(&quot;%s\n&quot;,filename);
                     write(sockfd,wbuf,5);
                     continue;
               }
            }
                  /*if(close(sockfd) <0)
                     printf(&quot;close error\n&quot;);*/
         }
         if(FD_ISSET(sockfd,&rset))
         {
             bzero(rbuf,strlen(rbuf));
             if((nread = recv(sockfd,rbuf,MAXBUF,0)) <0)
                  printf(&quot;recv error\n&quot;);
             else if(nread == 0)
               break;
         
             if(strncmp(rbuf,&quot;220&quot;,3) ==0 || strncmp(rbuf,&quot;530&quot;,3)==0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
               strcat(rbuf,&quot;your name:&quot;);
               //printf(&quot;%s\n&quot;,rbuf);
               nread &#43;= 12;
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
               replycode = USERNAME;
             }
             if(strncmp(rbuf,&quot;331&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;)*/;
                strcat(rbuf,&quot;your password:&quot;);
                nread &#43;= 16;
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = PASSWORD;
             }
             if(strncmp(rbuf,&quot;230&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = LOGIN;
             }
             if(strncmp(rbuf,&quot;257&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = PATHNAME;
             }
             if(strncmp(rbuf,&quot;226&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = CLOSEDATA;
             }
             if(strncmp(rbuf,&quot;250&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = ACTIONOK;
             }
             if(strncmp(rbuf,&quot;550&quot;,3) == 0)
             {
                replycode = 550;
             }
             /*if(strncmp(rbuf,&quot;150&quot;,3) == 0)
             {
                if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);
             }*/   
             //fprintf(stderr,&quot;%d\n&quot;,1);
             if(strncmp(rbuf,&quot;227&quot;,3) == 0)
             {
                //printf(&quot;%d\n&quot;,1);
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                   printf(&quot;write error to stdout\n&quot;);*/
                int port1 = strtosrv(rbuf);
                printf(&quot;%d\n&quot;,port1);
                printf(&quot;%s\n&quot;,host);
                data_sock = cliopen(host,port1);
      


//bzero(rbuf,sizeof(rbuf));
                //printf(&quot;%d\n&quot;,fd);
                //if(strncmp(rbuf1,&quot;ls&quot;,2) == 0)
                if(tag == 2)
                {
                   write(sockfd,&quot;list\n&quot;,strlen(&quot;list\n&quot;));
                   ftp_list(data_sock);
                   /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                     printf(&quot;write error to stdout\n&quot;);*/
                  
                }
                //else if(strncmp(rbuf1,&quot;get&quot;,3) == 0)
                else if(tag == 1)
                {
                  //sprintf(wbuf,&quot;%s&quot;,&quot;RETR\n&quot;);
                  //printf(&quot;%s\n&quot;,wbuf);
                  //int str = strlen(filename);
                  //printf(&quot;%d\n&quot;,str);
                  sprintf(wbuf,&quot;RETR %s\n&quot;,filename);
                  printf(&quot;%s\n&quot;,wbuf);
                  //int p = 5 &#43; str &#43; 1;


                  printf(&quot;%d\n&quot;,write(sockfd,wbuf,strlen(wbuf)));
                  //printf(&quot;%d\n&quot;,p);
                  ftp_get(data_sock,filename);
                }
                else if(tag == 3)
                {
                  sprintf(wbuf,&quot;STOR %s\n&quot;,filename);
                  printf(&quot;%s\n&quot;,wbuf);
                  write(sockfd,wbuf,strlen(wbuf));
                  ftp_put(data_sock,filename);
                }
                nwrite = 0;   
             }
             /*if(strncmp(rbuf,&quot;150&quot;,3) == 0)
             {
               if(write(STDOUT_FILENO,rbuf,nread) != nread)
                     printf(&quot;write error to stdout\n&quot;);
             }*/
             //printf(&quot;%s\n&quot;,rbuf);
             if(write(STDOUT_FILENO,rbuf,nread) != nread)
               printf(&quot;write error to stdout\n&quot;);
             /*else
               printf(&quot;%d\n&quot;,-1);*/            
         }
    }
}

  过程,首先是从键盘输入,然后服务器端会向我的ftp客户端发送执行请求的返回码,然后我的代码就通过select函数来捕获这些个验证码replycode,然后代码根据这个replycode,来执行相应的不同的代码。
  接下来是具体的针对每一个功能的代码,首先是将我被动模式下,新生成的端口号计算新生成的端口号的代码,注意,这里用到了sscanf函数
  int strtosrv(char *str)
{
   int addr;
   //printf(&quot;%s\n&quot;,str);
   sscanf(str,&quot;%*[^(](%d,%d,%d,%d,%d,%d)&quot;,&addr,&addr,&addr,&addr,&addr,&addr);   //sscanf函数是从一个字符串中读进与制定格式相符的数据,可以和sprintf来对应。这里用到了一点正则表达式,用来匹配相应的字符结果。
   bzero(host,strlen(host));
   sprintf(host,&quot;%d.%d.%d.%d&quot;,addr,addr,addr,addr);
   int port = addr*256 &#43; addr;
   return port;
}

  而在我上传和下载的过程中,我也得先取得put filename和get filename后面的filename,这个就需要调用一个函数来将这个filename分离并取出来。
  int s(char *str,char *s2)
{
    //char s1;
   
    return sscanf(str,&quot; get %s&quot;,s2) == 1;
   
}

  接下来是显示的函数ftp_list
  void ftp_list(int sockfd)                                  //注意,list显示内容不用先进入被动模式,而是直接就可以显示信息了
{
    int nread;
    for(;;)
    {
      if((nread = recv(sockfd,rbuf1,MAXBUF,0)) < 0)
      {
            printf(&quot;recv error\n&quot;);
      }
      else if(nread == 0)
      {
            //printf(&quot;over\n&quot;);
            break;
      }
      if(write(STDOUT_FILENO,rbuf1,nread) != nread)            //这个STDOUT_FILENO表示输出到屏幕
            printf(&quot;send error to stdout\n&quot;);
      /*else
            printf(&quot;read something\n&quot;);*/
    }
    if(close(sockfd) < 0)
      printf(&quot;close error\n&quot;);
}

  接下来是ftp_get函数,是用从ftp服务器端下载文件用的
  int ftp_get(int sck,char *pDownloadFileName)
{
   int handle = open(pDownloadFileName,O_WRONLY | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
   int nread;
   printf(&quot;%d\n&quot;,handle);
   /*if(handle == -1)
       return -1;*/
   for(;;)
   {
       if((nread = recv(sck,rbuf1,MAXBUF,0)) < 0)
       {
          printf(&quot;receive error\n&quot;);
       }
       else if(nread == 0)
       {
          printf(&quot;over\n&quot;);
          break;
       }
    //   printf(&quot;%s\n&quot;,rbuf1);
       if(write(handle,rbuf1,nread) != nread)
         printf(&quot;receive error from server!&quot;);
       if(write(STDOUT_FILENO,rbuf1,nread) != nread)
         printf(&quot;receive error from server!&quot;);
   }
       if(close(sck) < 0)
         printf(&quot;close error\n&quot;);
}

  
这个是先建立连接的过程图,然后就是针对get请求,具体的详细的过程图。  


  这个是进入被动模式的过程,然后就是在进入被动模式后,开始下载文件,
  

  接下来是上传文件,上传文件的过程与下载文件的过程如出一撤,基本过程是类似的,但是有些地方需要当心,应该先open本地的文件,然后是用read函数将本地的文件如出来,然后调用write函数向新建立的端口发送这个文件。


  以下是代码部分:
  int ftp_put(int sck,char *pUploadFileName_s)
{
   //int c_sock;
   int handle = open(pUploadFileName_s,O_RDWR);
   int nread;
   if(handle == -1)
       return -1;
   //ftp_type(c_sock,&quot;I&quot;);
   for(;;)
   {
       if((nread = read(handle,rbuf1,MAXBUF)) < 0)
       {
            printf(&quot;read error!&quot;);
       }
       else if(nread == 0)
          break;
       if(write(STDOUT_FILENO,rbuf1,nread) != nread)
            printf(&quot;send error!&quot;);
       if(write(sck,rbuf1,nread) != nread)
            printf(&quot;send error!&quot;);
   }
   if(close(sck) < 0)
      printf(&quot;close error\n&quot;);
}

  

  完整代码:
  #include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>


#define MAXBUF 1024
#define STDIN_FILENO 1
#define STDOUT_FILENO 0


#define USERNAME 220
#define PASSWORD 331
#define LOGIN 230
#define PATHNAME 257
#define CLOSEDATA 226
#define ACTIONOK 250


char *rbuf,*rbuf1,*wbuf,*wbuf1;


char filename;
char *host;


struct sockaddr_in servaddr;


int cliopen(char *host,int port);
int strtosrv(char *str);
int ftp_get(int sck,char *pDownloadFileName);
int ftp_put(int sck,char *pUploadFileName_s);
void cmd_tcp(int sockfd);


int main(int argc,char *argv[])
{
    int fd;
    if(0 != argc -2)
    {
      printf(&quot;%s\n&quot;,&quot;missing <hostname>&quot;);
      exit(0);


    }
    host = argv;
    int port = 21;
   
    rbuf = (char *)malloc(MAXBUF*sizeof(char));
    rbuf1 = (char *)malloc(MAXBUF*sizeof(char));
    wbuf = (char *)malloc(MAXBUF*sizeof(char));
    wbuf1 = (char *)malloc(MAXBUF*sizeof(char));
   
    fd = cliopen(host,port);
    cmd_tcp(fd);
    exit(0);
}






int cliopen(char *host,int port)
{
    int control_sock;
    struct hostent *ht = NULL;
    control_sock = socket(AF_INET,SOCK_STREAM,0);
    if(control_sock < 0)
    {
       printf(&quot;socket error\n&quot;);
       return -1;
    }
    ht = gethostbyname(host);
    if(!ht)
    {
      return -1;
    }
    //memcpy(&servaddr.sin_addr.s_addr,ht->h_addr,ht->h_length);
   
    memset(&servaddr,0,sizeof(struct sockaddr_in));
    memcpy(&servaddr.sin_addr.s_addr,ht->h_addr,ht->h_length);
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port);
   
    if(connect(control_sock,(struct sockaddr*)&servaddr,sizeof(struct sockaddr)) == -1)
    {
      return -1;
    }
    return control_sock;
}


int s(char *str,char *s2)
{
    //char s1;
   
    return sscanf(str,&quot; get %s&quot;,s2) == 1;
   
}


int st(char *str,char *s1)
{
    return sscanf(str,&quot; put %s&quot;,s1) == 1;
}


int strtosrv(char *str)
{
   int addr;
   //printf(&quot;%s\n&quot;,str);
   sscanf(str,&quot;%*[^(](%d,%d,%d,%d,%d,%d)&quot;,&addr,&addr,&addr,&addr,&addr,&addr);
   bzero(host,strlen(host));
   sprintf(host,&quot;%d.%d.%d.%d&quot;,addr,addr,addr,addr);
   int port = addr*256 &#43; addr;
   return port;
}


void ftp_list(int sockfd)
{
    int nread;
    for(;;)
    {
      if((nread = recv(sockfd,rbuf1,MAXBUF,0)) < 0)
      {
            printf(&quot;recv error\n&quot;);
      }
      else if(nread == 0)
      {
            //printf(&quot;over\n&quot;);
            break;
      }
      if(write(STDOUT_FILENO,rbuf1,nread) != nread)
            printf(&quot;send error to stdout\n&quot;);
      /*else
            printf(&quot;read something\n&quot;);*/
    }
    if(close(sockfd) < 0)
      printf(&quot;close error\n&quot;);
}


int ftp_get(int sck,char *pDownloadFileName)
{
   int handle = open(pDownloadFileName,O_WRONLY | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
   int nread;
   printf(&quot;%d\n&quot;,handle);
   /*if(handle == -1)
       return -1;*/
   for(;;)
   {
       if((nread = recv(sck,rbuf1,MAXBUF,0)) < 0)
       {
          printf(&quot;receive error\n&quot;);
       }
       else if(nread == 0)
       {
          printf(&quot;over\n&quot;);
          break;
       }
    //   printf(&quot;%s\n&quot;,rbuf1);
       if(write(handle,rbuf1,nread) != nread)
         printf(&quot;receive error from server!&quot;);
       if(write(STDOUT_FILENO,rbuf1,nread) != nread)
         printf(&quot;receive error from server!&quot;);
   }
       if(close(sck) < 0)
         printf(&quot;close error\n&quot;);
}


int ftp_put(int sck,char *pUploadFileName_s)
{
   //int c_sock;
   int handle = open(pUploadFileName_s,O_RDWR);
   int nread;
   if(handle == -1)
       return -1;
   //ftp_type(c_sock,&quot;I&quot;);
   for(;;)
   {
       if((nread = read(handle,rbuf1,MAXBUF)) < 0)
       {
            printf(&quot;read error!&quot;);
       }
       else if(nread == 0)
          break;
       if(write(STDOUT_FILENO,rbuf1,nread) != nread)
            printf(&quot;send error!&quot;);
       if(write(sck,rbuf1,nread) != nread)
            printf(&quot;send error!&quot;);
   }
   if(close(sck) < 0)
      printf(&quot;close error\n&quot;);
}




void cmd_tcp(int sockfd)
{
    int maxfdp1,nread,nwrite,fd,replycode,tag=0,data_sock;
    int port;
    char *pathname;
    fd_set rset;
    FD_ZERO(&rset);
    maxfdp1 = sockfd &#43; 1;
    for(;;)
    {
         FD_SET(STDIN_FILENO,&rset);
         FD_SET(sockfd,&rset);
         if(select(maxfdp1,&rset,NULL,NULL,NULL)<0)
         {
             printf(&quot;select error\n&quot;);
         }
         if(FD_ISSET(STDIN_FILENO,&rset))
         {
            //nwrite = 0;
            //nread = 0;
            bzero(wbuf,MAXBUF);          //zero
            bzero(rbuf1,MAXBUF);
            if((nread = read(STDIN_FILENO,rbuf1,MAXBUF)) <0)
                   printf(&quot;read error from stdin\n&quot;);
            nwrite = nread &#43; 5;
            //printf(&quot;%d\n&quot;,nread);      
            if(replycode == USERNAME)
            {
                  sprintf(wbuf,&quot;USER %s&quot;,rbuf1);
            
               if(write(sockfd,wbuf,nwrite) != nwrite)
               {
                     printf(&quot;write error\n&quot;);
               }
               //printf(&quot;%s\n&quot;,wbuf);
               //memset(rbuf1,0,sizeof(rbuf1));
               //memset(wbuf,0,sizeof(wbuf));
               //printf(&quot;1:%s\n&quot;,wbuf);
            }


            if(replycode == PASSWORD)
            {
                   //printf(&quot;%s\n&quot;,rbuf1);
                   sprintf(wbuf,&quot;PASS %s&quot;,rbuf1);
                   if(write(sockfd,wbuf,nwrite) != nwrite)
                      printf(&quot;write error\n&quot;);
                   //bzero(rbuf,sizeof(rbuf));
                   //printf(&quot;%s\n&quot;,wbuf);
                   //printf(&quot;2:%s\n&quot;,wbuf);
            }
            if(replycode == 550 || replycode == LOGIN || replycode == CLOSEDATA || replycode == PATHNAME || replycode == ACTIONOK)
            {
          if(strncmp(rbuf1,&quot;pwd&quot;,3) == 0)
            {   
                     //printf(&quot;%s\n&quot;,rbuf1);
                     sprintf(wbuf,&quot;%s&quot;,&quot;PWD\n&quot;);
                     write(sockfd,wbuf,4);
                     continue;
               }
               if(strncmp(rbuf1,&quot;quit&quot;,4) == 0)
               {
                     sprintf(wbuf,&quot;%s&quot;,&quot;QUIT\n&quot;);
                     write(sockfd,wbuf,5);
                     //close(sockfd);
                  if(close(sockfd) <0)
                     printf(&quot;close error\n&quot;);
                  break;
               }
               if(strncmp(rbuf1,&quot;cwd&quot;,3) == 0)
               {
                     //sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);
                     sprintf(wbuf,&quot;%s&quot;,rbuf1);
                     write(sockfd,wbuf,nread);
                     
                     //sprintf(wbuf1,&quot;%s&quot;,&quot;CWD\n&quot;);
                     
                     continue;
               }
               if(strncmp(rbuf1,&quot;ls&quot;,2) == 0)
               {
                     tag = 2;
                     //printf(&quot;%s\n&quot;,rbuf1);
                     sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);
                     //printf(&quot;%s\n&quot;,wbuf);
                     write(sockfd,wbuf,5);
                     //read
                     //sprintf(wbuf1,&quot;%s&quot;,&quot;LIST -al\n&quot;);
                     nwrite = 0;
                     //write(sockfd,wbuf1,nwrite);
                     //ftp_list(sockfd);
                     continue;   
               }
               if(strncmp(rbuf1,&quot;get&quot;,3) == 0)
               {
                     tag = 1;
                     sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);                  
                     //printf(&quot;%s\n&quot;,s(rbuf1));
                     //char filename;
                     s(rbuf1,filename);
                     printf(&quot;%s\n&quot;,filename);
                     write(sockfd,wbuf,5);
                     continue;
               }
               if(strncmp(rbuf1,&quot;put&quot;,3) == 0)
               {
                     tag = 3;
                     sprintf(wbuf,&quot;%s&quot;,&quot;PASV\n&quot;);
                     st(rbuf1,filename);
                     printf(&quot;%s\n&quot;,filename);
                     write(sockfd,wbuf,5);
                     continue;
               }
            }
                  /*if(close(sockfd) <0)
                     printf(&quot;close error\n&quot;);*/
         }
         if(FD_ISSET(sockfd,&rset))
         {
             bzero(rbuf,strlen(rbuf));
             if((nread = recv(sockfd,rbuf,MAXBUF,0)) <0)
                  printf(&quot;recv error\n&quot;);
             else if(nread == 0)
               break;
         
             if(strncmp(rbuf,&quot;220&quot;,3) ==0 || strncmp(rbuf,&quot;530&quot;,3)==0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
               strcat(rbuf,&quot;your name:&quot;);
               //printf(&quot;%s\n&quot;,rbuf);
               nread &#43;= 12;
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
               replycode = USERNAME;
             }
             if(strncmp(rbuf,&quot;331&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;)*/;
                strcat(rbuf,&quot;your password:&quot;);
                nread &#43;= 16;
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = PASSWORD;
             }
             if(strncmp(rbuf,&quot;230&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = LOGIN;
             }
             if(strncmp(rbuf,&quot;257&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = PATHNAME;
             }
             if(strncmp(rbuf,&quot;226&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = CLOSEDATA;
             }
             if(strncmp(rbuf,&quot;250&quot;,3) == 0)
             {
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);*/
                replycode = ACTIONOK;
             }
             if(strncmp(rbuf,&quot;550&quot;,3) == 0)
             {
                replycode = 550;
             }
             /*if(strncmp(rbuf,&quot;150&quot;,3) == 0)
             {
                if(write(STDOUT_FILENO,rbuf,nread) != nread)
                  printf(&quot;write error to stdout\n&quot;);
             }*/   
             //fprintf(stderr,&quot;%d\n&quot;,1);
             if(strncmp(rbuf,&quot;227&quot;,3) == 0)
             {
                //printf(&quot;%d\n&quot;,1);
                /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                   printf(&quot;write error to stdout\n&quot;);*/
                int port1 = strtosrv(rbuf);
                printf(&quot;%d\n&quot;,port1);
                printf(&quot;%s\n&quot;,host);
                data_sock = cliopen(host,port1);
      


//bzero(rbuf,sizeof(rbuf));
                //printf(&quot;%d\n&quot;,fd);
                //if(strncmp(rbuf1,&quot;ls&quot;,2) == 0)
                if(tag == 2)
                {
                   write(sockfd,&quot;list\n&quot;,strlen(&quot;list\n&quot;));
                   ftp_list(data_sock);
                   /*if(write(STDOUT_FILENO,rbuf,nread) != nread)
                     printf(&quot;write error to stdout\n&quot;);*/
                  
                }
                //else if(strncmp(rbuf1,&quot;get&quot;,3) == 0)
                else if(tag == 1)
                {
                  //sprintf(wbuf,&quot;%s&quot;,&quot;RETR\n&quot;);
                  //printf(&quot;%s\n&quot;,wbuf);
                  //int str = strlen(filename);
                  //printf(&quot;%d\n&quot;,str);
                  sprintf(wbuf,&quot;RETR %s\n&quot;,filename);
                  printf(&quot;%s\n&quot;,wbuf);
                  //int p = 5 &#43; str &#43; 1;


                  printf(&quot;%d\n&quot;,write(sockfd,wbuf,strlen(wbuf)));
                  //printf(&quot;%d\n&quot;,p);
                  ftp_get(data_sock,filename);
                }
                else if(tag == 3)
                {
                  sprintf(wbuf,&quot;STOR %s\n&quot;,filename);
                  printf(&quot;%s\n&quot;,wbuf);
                  write(sockfd,wbuf,strlen(wbuf));
                  ftp_put(data_sock,filename);
                }
                nwrite = 0;   
             }
             /*if(strncmp(rbuf,&quot;150&quot;,3) == 0)
             {
               if(write(STDOUT_FILENO,rbuf,nread) != nread)
                     printf(&quot;write error to stdout\n&quot;);
             }*/
             //printf(&quot;%s\n&quot;,rbuf);
             if(write(STDOUT_FILENO,rbuf,nread) != nread)
               printf(&quot;write error to stdout\n&quot;);
             /*else
               printf(&quot;%d\n&quot;,-1);*/            
         }
    }
}

  

  执行的效果图:
  首先是登陆:
,如果ftp连接成功,是会返回一个220,提示用户输入登陆名

  接下来是输入用户的登录名,
,然后是输入我的账号,接下来就是输入密码,

,然后如果账户和密码都输对,那么就会提示登录成功。

  接下来演示pwd,
  然后是ls,显示当前路径下的目录的指令
,看到了返回的结果。

  接下来是下载文件档当前路径下。
,待会退出ftp后,可以通过ls来查看是否已经将这个文件下载到本地客户端了。

  

  

         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: linux下用C编写ftp客户端