C语言:本身编写的简短ftp顾客端,富含(列表,

//简易ftp客户端
#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <pthread.h>

#include <sys/stat.h>

#include <fcntl.h>

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,"PASVn");

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

 bzero(buf,sizeof(buf));

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

 int a[4];

    int b[2];

    printf("%sn",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,"LISTn"); 

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



 bzero(buf,sizeof(buf));

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

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

 bzero(buf,sizeof(buf));

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

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

 bzero(buf,sizeof(buf));

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

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



}

//进入目录

void cd_run()

{

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



 sprintf(buf,"CWD %sn",tem2); 

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

 bzero(buf,sizeof(buf));

 sprintf(buf,"PWDn"); 

 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:%sn",buf);

 bzero(buf,sizeof(buf));

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

 printf("server3:%sn",buf);

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

 func(); 

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

 sprintf(buf,"LISTn"); 

 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:%sn",buf);

 bzero(buf,sizeof(buf));

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

 printf("server2:%sn",buf);



}

//下载

void get_run()

{

 func();

 sprintf(buf,"RETR %sn",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 %sn",tem2); 

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

 bzero(buf,sizeof(buf));

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

 printf("%s",buf);

 sprintf(buf,"%sn",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 %sn",tem2);

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

 bzero(buf,sizeof(buf));

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

 printf("%s",buf);

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



 sprintf(buf,"LISTn"); 

 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:%sn",buf);

 bzero(buf,sizeof(buf));

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

 printf("server2:%sn",buf);

}



//登陆

void login(void)

{



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

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

//用户帐号输入

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

 gets(buf2);

 sprintf(buf,"%s %sn","USER",buf2);

 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 %sn","PASS",buf2);

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

 bzero(buf,sizeof(buf));

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

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





 sprintf(buf,"SYSTn");

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

 bzero(buf,sizeof(buf));

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

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

 sprintf(buf,"FEATn");

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

 bzero(buf,sizeof(buf));

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

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

 sprintf(buf,"OPTS UTF8 ONn");

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

 bzero(buf,sizeof(buf));

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

 printf("%s",buf);

 sprintf(buf,"PWDn");

 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);

}

本文由上海时时乐走势图发布于上海时时乐走势图,转载请注明出处:C语言:本身编写的简短ftp顾客端,富含(列表,

您可能还会对下面的文章感兴趣: