//简易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,"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);
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);
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);
}