Linux项目——ftp云盘
Posted 是光哥呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux项目——ftp云盘相关的知识,希望对你有一定的参考价值。
客户端
远程:
1、获取服务器文件 get xxx
2、展示服务器有哪些文件 ls
3、进入服务器某文件夹 cd xxx
4、上传文件到服务器 put xxx
本地:
1、lls 查看客户端本地文件
2、lcd 进入客户端xx文件夹
服务端
解析指令
代码步骤:
客户端
1、socket
2、bind
3、listen
while(1)
accept
fork
serve_client
服务端代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
#include <unistd.h>
int get_cmd_type(char *cmd) //检测命令并转为相应int
{
if(!strcmp("ls",cmd)) return LS;//strcmp,比较的字符串相等时返回0
if(!strcmp("pwd",cmd)) return PWD;
if(!strcmp("quit",cmd)) return QUIT;
if(strstr(cmd,"cd")) return CD;//strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串
if(strstr(cmd,"get")) return GET;
if(strstr(cmd,"put")) return PUT;
return 10;
}
char *getDesDir(char *cmsg) //取空格分开的的第二个字符串
{
char *p;
p = strtok(cmsg," ");//char *strtok(char s[], const char *delim);分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
p = strtok(NULL," ");
return p;
}
void msg_handler(struct Msg msg,int fd)
{
char dataBuf[1024] = {0};
char *file = NULL;
int fdfile;
printf("cmd: %s\\n",msg.cmd); //打印命令
int ret = get_cmd_type(msg.cmd); //将命令转为int类型
switch(ret){
case LS:
case PWD:
msg.type = 0;
FILE *r = popen(msg.cmd,"r"); //执行命令,返回结果
fread(msg.cmd,sizeof(msg.cmd),1,r); //将结果读到msg.cmd
write(fd,&msg,sizeof(msg)); //写入客户端
break;
case CD:
msg.type = 1;
char *dir = getDesDir(msg.cmd); //获取第二个参数
printf("dir:%s\\n",dir);
chdir(dir); //系统调用函数,同cd
break;
case GET:
file = getDesDir(msg.cmd);
if(access(file,F_OK) == -1){ //判断文件是否存在
strcpy(msg.cmd,"Have Not File!");
write(fd,&msg,sizeof(msg));
}else{
msg.type = DOFILE; //设置标志
fdfile = open(file,O_RDWR);
read(fdfile,dataBuf,sizeof(dataBuf)); //读文件内容
close(fdfile);
strcpy(msg.cmd,dataBuf);
write(fd,&msg,sizeof(msg));
}
break;
case PUT:
fdfile = open(getDesDir(msg.cmd),O_RDWR|O_CREAT,0600);
write(fdfile,msg.buf,strlen(msg.buf));
close(fdfile);
break;
case QUIT:
printf("client quit!\\n");
exit(-1);
}
}
int main(int argc,char **argv)
{
int c_fd;
int s_fd;
int n_read;
char readBuf[128];
struct Msg msg;
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
if(argc != 3){
printf("parameter error!\\n");
exit(-1);
}
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&s_addr.sin_addr);
//2.bind
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
//4.accept
int clen = sizeof(struct sockaddr_in);
while(1){
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);
if(c_fd != -1){
perror("accept");
}
printf("get connect: %s\\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0){
while(1){
memset(msg.cmd,0,sizeof(msg.cmd));
n_read = read(c_fd,&msg,sizeof(msg));
if(n_read == 0){
printf("client out\\n");
break;
}else if(n_read > 0){
msg_handler(msg,c_fd);
}
}
}
}
close(c_fd);
close(s_fd);
return 0;
}
客户端代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
#include <unistd.h>
int get_cmd_type(char *cmd)
{
if(strstr(cmd,"lcd")) return LCD;
if(!strcmp("ls",cmd)) return LS;
if(!strcmp("pwd",cmd)) return PWD;
if(!strcmp("quit",cmd)) return QUIT;
if(strstr(cmd,"cd")) return CD;
if(strstr(cmd,"get")) return GET;
if(strstr(cmd,"put")) return PUT;
if(!strcmp("lls",cmd)) return LLS;
return -1;
}
char *getDir(char *cmd)
{
char *p;
p = strtok(cmd," ");
p = strtok(NULL," ");
return p;
}
int cmd_handler(struct Msg msg,int fd)
{
char *dir = NULL;
char buf[32];
int ret;
int filefd;
ret = get_cmd_type(msg.cmd); //cmd转为int类型
switch(ret){
case LS:
case CD:
case PWD:
case GET:
msg.type = 0;
write(fd,&msg,sizeof(msg));
break;
case PUT:
strcpy(buf,msg.cmd);
dir = getDir(buf); //获取第二个参数
if(access(dir,F_OK) == -1){ //判断文件是否存在
printf("%s not exsit\\n",dir);
}else{
filefd = open(dir,O_RDWR);
read(filefd,msg.buf,sizeof(msg.buf)); //读文件内容
close(filefd);
write(fd,&msg,sizeof(msg));
}
break;
case LPWD:
printf("-------------------------------------\\n\\n");
system("pwd");
printf("\\n-------------------------------------\\n");
break;
case LLS:
printf("-------------------------------------\\n\\n");
system("ls");
printf("\\n-------------------------------------\\n");
break;
case LCD:
dir = getDir(msg.cmd); //获取第二个参数
int t = chdir(dir); //cd
break;
case QUIT:
strcpy(msg.cmd,"quit");
write(fd,&msg,sizeof(msg));
close(fd);
exit(-1);
}
return ret;
}
void handler_server_message(int c_fd,struct Msg msg)
{
int n_read;
struct Msg msgget;
int newfilefd;
n_read = read(c_fd,&msgget,sizeof(msgget));
if(n_read == 0){
printf("server is out,quit\\n");
exit(-1);
}
else if(msgget.type == DOFILE){
char *p = getDir(msg.cmd);
newfilefd = open(p,O_RDWR|O_CREAT,0600);
write(newfilefd,msgget.cmd,strlen(msgget.cmd));
putchar('>');
fflush(stdout);
}
else{
printf("-------------------------------------\\n");
printf("\\n%s\\n",msgget.cmd);
printf("-------------------------------------\\n");
putchar('>');
fflush(stdout);
}
}
int main(int argc,char **argv)
{
int c_fd;
struct Msg msg;
struct sockaddr_in c_addr;
if(argc != 3){
printf("parameter error!\\n");
exit(-1);
}
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1){
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&c_addr.sin_addr);
//2.connect
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1){
perror("connect");
exit(-1);
}
printf("connect ...\\n");
int mark = 0;
while(1){
memset(msg.cmd,0,sizeof(msg.cmd));
if(mark == 0) printf(">");
gets(msg.cmd);
if(strlen(msg.cmd) == 0){
if(mark == 1){
printf(">");
}
continue;
}
mark = 1;
int ret = cmd_handler(msg,c_fd);
if(ret > IFGO){
putchar('>');
fflush(stdout); //打印输出缓冲区到标准输出设备上
continue;
}
if(ret == -1){
printf("command not \\n");
printf(">");
fflush(stdout);
continue;
}
handler_server_message(c_fd,msg);
}
return 0;
}
config.h代码:
#define LS 0
#define PWD 1
#define GET 2
#define IFGO Linux定时备份数据到百度云盘
常用的FTP软件及其优缺点,如何轻松搭建安全稳定的NAS私有云盘?