FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP
协议提供服务。 FTP是File Transfer Protocol(文件传输协议)。 程序运行,服务端不断接收客户端指令,服务
端可同时处理多个客户端接入并对指令作出解析,并把执行结果返回给客户端,客户端根据服务端对指令的
解析并把由服务端传递过来的处理信息通过客户端呈现给客户,实现文件的各种操作。
Linux网络编程实现的FTP服务器,服务器由服务端和客户端组成,具有浏览远程服务端的文件和浏览客户端本地文
件,同时支持对远程服务端文件的删除,存储,归档操作处理,以及客户端对远程服务端文件的上传和下载。
ls———查看服务端文件
lls———查看客户端自己的文件
cd———切换服务端目录
lcd———切换客户端自己的目录
put———上传文件
get———下载文件
g———客户端退出
access(cmd, F_OK);//判断当前目录里是否含有名字为cmd字符串的文件,有则返回0,无则返回-1;
strtok(cmd," “);//将空格键作为分隔符,把cmd字符串分离出来。
strtok(NULL,” “);//字符串默认为上一次分离的状态,继续以空格键作为分隔符分离。
例如:
char *p=strtok(“hello world”,” “);//此时p为hello
p=strtok(NULL,” ");//此时p为world
服务器代码:
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
#include
void * server_handler(void * arc);
char *getbehind(char *cmd);
int choose(char * cmd);
void getmessage(char *title,int newfd);
void putmessage(char *title,int newfd);
void choosecmd(char *cmd,int newfd);
//使用线程来处理指令
void * server_handler(void * arc)
{
int newfd=*(int *)arc;
char* cmd=(char *)malloc(128);
while(1)
{
read(newfd,cmd,20);
printf("%s\n",cmd);
choosecmd(cmd,newfd);
memset(cmd,0,128);
}
}
char * getbehind(char cmd[128])
{
char *p;
p=strtok(cmd," ");//以空格键为分隔符,获得分离后的数据。
p=strtok(NULL," ");//NULL表示继续往下分隔
return p;
}
int choose(char * cmd)
{
if(!strcmp("lls",cmd)) { return 1;}
else if(strstr(cmd,"lcd")!=NULL) { return 2;}
else if(!strcmp("g",cmd) ) { return 3;}
else if(!strcmp("ls",cmd) ) { return 4;}
else if(strstr(cmd,"cd")!=NULL) { return 5;}
else if(strstr(cmd,"get")!=NULL) { return 6;}
else if(strstr(cmd,"put")!=NULL) { return 7;}
}
void choosecmd(char cmd[128],int newfd)
{ FILE * fp;
int ret=choose(cmd);
char *title=(char *)malloc(128);
char *readbuf=(char *)malloc(8000);
char *p=(char *)malloc(8000);
int sfd;
switch(ret)
{
case 1:
printf("客户端查看客户端的当前目录包含的文件\n");
break;
case 2:
printf("客服端切换进对应目录\n");
break;
case 3:
printf("客户端退出连接\n");
break;
case 4:
fp=popen("ls","r");
fread(readbuf,8000-1,1,fp);
printf("%s\n",readbuf);
write(newfd,readbuf,8000);
printf("ls success\n");
memset(readbuf,0,8000);
break;
case 5:
title=getbehind(cmd);
chdir(title);
printf("切换目录成功\n");
memset(title,0,8000);
break;
case 6:
title =getbehind(cmd);
printf("%s\n",title);
if(access(title,F_OK)==-1)
{
write(newfd,"NO file",strlen("NO file"));
printf("no file\n");
}
else
{
sfd=open(title,O_RDWR,0666);
read(sfd,p,8000);
write(newfd,p,8000);
close(sfd);
memset(p,0,8000);
printf("服务器已发送文件\n");
}
break;
case 7:
title=getbehind(cmd);
read(newfd,readbuf,8000);
if(strlen(readbuf)!=strlen("NO file"))
{
sfd=open(title,O_RDWR|O_CREAT,0666);
write(sfd,readbuf,strlen(readbuf));
printf("服务器已接收文件\n");
close(sfd);
memset(readbuf,0,8000);
}
else
{
printf("NO file\n");
}
break;
}
}
int main(int argc,char **argv)
{
if(argc != 3)
{
perror("argc");
exit(1);
}
int s_fd;
//1.创建套接字
s_fd=socket(AF_INET,SOCK_STREAM,0);
//2.bind绑定地址
struct sockaddr_in maddr;
maddr.sin_family=AF_INET;
maddr.sin_port=htons(atoi(argv[2]));
maddr.sin_addr.s_addr=inet_addr(argv[1]);
bind(s_fd,(struct sockaddr *)&maddr,sizeof(maddr));
//3.listen允许被连接
listen(s_fd,5);
struct sockaddr_in caddr;
while(1)
{
bzero(&caddr,sizeof(caddr));//清除数据
socklen_t p=sizeof(caddr);
printf("waiting connected\n");
//4.等待被连接
int newfd=accept(s_fd,(struct sockaddr *)&caddr,&p);
if(newfd < 0)
{
perror("accept");
exit(1);
}
char ipv4_addr[16];//用来存放接受到的客服端的地址
if(inet_ntop(AF_INET,(void *)&caddr.sin_addr,ipv4_addr,sizeof(caddr))==0)
{
perror("inet_ntop");
exit(1);
}
printf("get connect:%s,%d\n",ipv4_addr,ntohs(caddr.sin_port));
pthread_t pid;
pthread_create(&pid,NULL,&server_handler,(void *)&newfd);
}
return 0;
}
客户端代码:
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
char *getbehind(char *cmd);
int choose(char * cmd);
void getmessage(char *title,int newfd);
void putmessage(char *title,int newfd);
void choosecmd(char *cmd,int newfd);
//获取指令中的第二个字符串,如cd ftp,就会获取ftp
char * getbehind(char cmd[128])
{
char *p;
p=strtok(cmd," ");//以空格键为分隔符,获得分离后的数据。
p=strtok(NULL," ");//NULL表示继续往下分隔
return p;
}
//获取数据
void getmessage(char cmd[128],int c_fd)
{
char *readbuf=(char *)malloc(8000);
read(c_fd,readbuf,8000);
char *p=getbehind(cmd);
printf("%s\n",p);
if(strlen(readbuf) != strlen("NO file"))
{
int fd = open(p,O_RDWR|O_CREAT,0666);
write(fd,readbuf,strlen(readbuf));
printf("recevie successful\n");
memset(readbuf,0,sizeof(p));
close(fd);
}
else
{
printf("NO file\n");
}
}
//输出数据
void putmessage(char cmd[128],int c_fd)
{
char writebuf[8000]={'\0'};
char *p=(char *)malloc(128);
int fd;
p=getbehind(cmd);
printf("%s\n",p);
if(access(p,F_OK)==-1)//判断当前目录是否存在p数组里对应的文件
{
printf("No file\n");
}
else
{
fd=open(p,O_RDWR,0666);
read(fd,writebuf,8000);
write(c_fd,writebuf,strlen(writebuf));
close(fd);
memset(writebuf,0,8000);
}
}
//用来选择指令操作
int choose(char * cmd)
{
if(!strcmp("lls",cmd)){return 1;}
else if(strstr(cmd,"lcd")!=NULL){return 2;}
else if(!strcmp("g",cmd)){return 3;}
else if(!strcmp("ls",cmd)){return 4;}
else if(strstr(cmd,"cd")!=NULL){return 5;}
else if(strstr(cmd,"get")!=NULL){return 6;}
else if(strstr(cmd,"put")!=NULL){return 7;}
}
void choosecmd(char *cmd,int c_fd)
{
int ret=choose(cmd);
char *readbuf=(char *)malloc(128);
//char *p=(char *)malloc(8000);
char p[8000]={'\0'};
switch(ret)
{
case 1:system("ls");
// printf("ok\n");
break;
case 2: readbuf=getbehind(cmd);
chdir(readbuf);
memset(readbuf,0,128);
break;
case 3: printf("exit connect\n");
write(c_fd,"away host",8000);
close(c_fd);
exit(1);
break;
case 4:read(c_fd,p,sizeof(p));
printf("%s\n",p);
memset(p,0,8000);
break;
case 5:printf("host folder open or exit sucessful\n");
break;
case 6:getmessage(cmd,c_fd);
break;
case 7:putmessage(cmd,c_fd);
break;
}
}
int main(int argc,char **argv)
{
if(argc != 3)
{
perror("argc");
exit(1);
}
int c_fd;
//1.创建套接字
c_fd=socket(AF_INET,SOCK_STREAM,0);
if(c_fd < 0)
{
perror("socket");
exit(1);
}
struct sockaddr_in saddr;
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[2]));
saddr.sin_addr.s_addr=inet_addr(argv[1]);
if(connect(c_fd,(struct sockaddr *)&saddr,sizeof(saddr))<0)
{
perror("connect");
exit(1);
}
printf("connect...... \n");
char *writebuf=(char *)malloc(128);
while(1)
{
gets(writebuf);
//read( _STDIO_H,writebuf,128);
printf("cmd:%s\n",writebuf);
write(c_fd,writebuf,strlen(writebuf));
choosecmd(writebuf,c_fd);
printf("*********cmd***********\n");
memset(writebuf,0,128);
}
}