ftp云盘(socket网络,共享内存)

1、功能

客户端可以往服务端,上传、下载、查看、删除文件
和博文:ftp云盘功能一样
在博文ftp云盘的基础上服务端增加了共享内存
运行截图:
在这里插入图片描述

2、API

共享内存的相关API:进程间通信

memset//初始化
memcpy//拷贝
memcmp//比较,比较对象可以是结构体

3、实现思路

服务端:
1.socket等待连接,连接成功fork进程
2.父进程运行程序ser1
3.子进程连接2个共享内存,接收客户端socket发来的指令并通过共享内存1发送到ser1处理,等待执行结果读取共享内存2内容
4.ser1程序
创建2个共享内存,共享内存1接收指令,共享内存2发送指令执行结果
每隔1秒判断共享内存1的指令,指令不同开始执行内容,否则继续循环判断
每条指令都会返回数据,无论成功失败,因为ser有数据判断,没数据返回进程就会卡住
客户端:
和博文:ftp云盘一样

4、代码展示

服务端

ser代码

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "ser.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>

void serv_cl(int c_fd)
{
        key_t key2;
        key_t key;
        int shid;
        int shid2;
        int a;
        //char *shmatadd2;
        //char *shmatadd;
        struct Mcc buf;
        struct Mcc *shmatadd;
        struct Mcc *shmatadd2;

        a = sizeof(struct Mcc)/1024+1;

        key2 = ftok(".",'c');
        key = ftok(".",'a');
        shid2 = shmget(key2,1024*a,0);
        shid = shmget(key,1024*a,0);
        if(shid == -1){
                perror("shid");
        }
        if(shid2 == -1){
                perror("shid2");
        }


        //shmatadd = shmat(shid,0,0);
        //shmatadd2 = shmat(shid2,0,0);
        shmatadd2 = shmat(shid2,0,0);
        shmatadd = shmat(shid,0,0);

        printf("ser:shmatadd success..\n");
        while(1){
        /*
			初始化buf和共享内存内容,(防止内容错误)
			读socket,拷贝内容到共享内存中
			再次初始化buf内容(buf是因为防止put指令数据data出错,)
			判断buf和共享内存内容是否一样,不一样代表指令执行完毕
			读取执行结果发回客户端
			
			收到quit指令,退出循环,进而退出进程
		*/
                //printf("ser/\n");
                memset(&buf,0,sizeof(buf));
                memset(shmatadd2,0,sizeof(shmatadd2));

                read(c_fd,&buf,sizeof(buf));
                //printf("ser//\n");

                //strcpy(shmatadd,buf.cmd);
                memcpy(shmatadd,&buf,sizeof(buf));

                //printf("ser:cmd->%s\n",buf.cmd);
                //sleep(2);i

                memset(&buf,0,sizeof(buf));
                //memset(shmatadd2,0,sizeof(shmatadd2));
                while(!memcmp(buf.data,shmatadd2->data,2));

                //printf("while ok\n");

                memcpy(&buf,shmatadd2,sizeof(buf));
                //printf("ser buf.type%d\n",buf.type);
                write(c_fd,&buf,sizeof(buf));
                if(!strncmp("quit",buf.cmd,4)){

                        printf("ser quit\n");
                        break;
                }
                /*if(strcmp(buf.cmd,"quit") == 0){
                        printf("quit\n");
                        break;
                }*/
        }
        shmdt(shmatadd2);
        shmdt(shmatadd);
}
int main(int argc,char **argv)
{
        if(argc != 3)
        {
                printf("usage: command listen_port\n");
                exit(-1);
        }
        int jieshou;
        int fid;
        int s_fd;
        int c_fd;
        struct sockaddr_in addr;
        struct sockaddr_in c_addr;
        memset(&c_addr,0,sizeof(struct sockaddr_in));
        memset(&addr,0,sizeof(struct sockaddr_in));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(atoi(argv[2]));
        inet_aton(argv[1],&addr.sin_addr);
        s_fd = socket(AF_INET,SOCK_STREAM,0);
        if(s_fd < 0)
        {
                perror("socket");
                exit(-1);
        }
        if(bind(s_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) < 0)
        {
                perror("bind");
                exit(-1);
        }
        if(listen(s_fd,10) < 0)
        {
                perror("listen");
                exit(-1);
        }
        jieshou = sizeof(struct sockaddr_in);
        while(1)
        {
                c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&jieshou);
                if(c_fd < 0)
                {
                        perror("accept");
                        exit(-1);
                }
                else
                {
                        printf("client IP:%s\n",inet_ntoa(c_addr.sin_addr));
                }
                fid = fork();
                if(fid < 0)
                {
                        perror("fork error!");
                        exit(-1);
                }
                else if(fid == 0)
                {
                        sleep(1);
                        serv_cl(c_fd);
                //      printf("serv_cl\n");
                }else{
                        //execl("./ser1","ser1",NULL);
                        system("./ser1");
                //      printf("ser system\n");
                }
                close(c_fd);
        }
        close(s_fd);
        return 0;
}
                                     

ser1代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include "ser.h"
#include <sys/stat.h>
#include <fcntl.h>

int get_cmd_type(char *buf)
{
        if(!strncmp("ls",buf,2))                return LS;
        if(!strncmp("quit",buf,4))              return QUIT;
        if(!strncmp("pwd",buf,3))               return PWD;
        if(!strncmp("cd",buf,2))                return CD;
        if(!strncmp("rm",buf,2))                return RM;
        if(!strncmp("get",buf,3))               return GET;
        if(!strncmp("put",buf,3))               return PUT;
        return 100;
}
char *S_tok(char *a)
{
        char *p;
        p = strtok(a," ");
        p = strtok(NULL," ");
        return p;
}

void GetPut(struct Mcc *buf,int set)
{
        char *p = NULL;
        int fd;
        char cmd[128];
        strcpy(cmd,buf->cmd);
        switch(set){
                case GET:
                        p = S_tok(cmd);
                        if(access(p,F_OK) == -1)
                        {
                                buf->type = -1;
                                //printf("type:%c\n",buf->type);
                                strcpy(buf->data,"NO This file!!!");
                                //write(c_fd,&buf,sizeof(buf));
                        }
                        else
                        {
                                fd = open(p,O_RDWR);
                                //buf.a = lseek(fd,0,SEEK_END);
                                //lseek(fd,0,SEEK_SET);
                                //printf("lek:%d\n",buf.a);

                                //buf.getput = (char*)malloc(buf.a);
                                read(fd,buf->data,sizeof(buf->data));

                                //write(c_fd,&buf.type,sizeof(buf.type));
                                //write(c_fd,&buf.a,sizeof(buf.a));
                                //write(c_fd,buf.getput,buf.a);

                                close(fd);
                                //free(buf.getput);
                        }
                        break;
                case PUT:
                        p = S_tok(cmd);

                        fd = open(p,O_RDWR|O_CREAT,0600);
                        if(fd == -1){
                                perror("open");
                                strcpy(buf->data,"open fail");
                                break;
                                //printf("open on\n");
                        }

                        //buf.getput = (char*)malloc(buf.a);
                        //read(c_fd,buf.getput,buf.a);
                        //printf("a=%d,getput=%ld\n",buf.a,strlen(buf.getput));
                        write(fd,buf->data,sizeof(buf->data));
                        close(fd);
                        //free(buf.getput);
                        memset(buf->data,0,sizeof(buf->data));
                        strcpy(buf->data,"put cuccess...");
                        break;
        }
}
void Cd(struct Mcc *buf,int set)
{
        char *p = NULL;
        switch(set){
                case RM:
                        system(buf->cmd);
                        //printf("rm ok!\n");
                        strcpy(buf->data,"rm success...\n");
                        break;
                case CD:
                {
                        p = S_tok(buf->cmd);
                        printf("cd=%s\n",p);
                        if(chdir(p) == -1)
                        {
                                perror("cd");
                                strcpy(buf->data,"cd fail...\n");
                                break;
                        }
                        strcpy(buf->data,"cd success...\n");
                        //printf("cd ok!\n");
                        break;
                }
                case PWD:
                case LS:
                {
                        FILE *f = popen(buf->cmd,"r");
                        fread(buf->data,1024,5,f);
                        pclose(f);
                        //printf("ls ok!\n");
                        break;
                }
        }
}

int main()
{
        key_t key;
        key_t key2;
        int shid;
        int shid2;
        int a;
        //char *shmatadd2;
        //char *shmatadd;
        struct Mcc *shmatadd;
        struct Mcc *shmatadd2;
        int set = 100;
        struct Mcc buf;
        a = sizeof(struct Mcc)/1024+1;

        key2 = ftok(".",'c');
        key = ftok(".",'a');
        shid2 = shmget(key2,1024*a,IPC_CREAT|0666);
        shid = shmget(key,1024*a,IPC_CREAT|0666);
        if(shid == -1){
                perror("shid");
        }
        if(shid2 == -1){
                perror("shid2");
        }
        //shmatadd = shmat(shid,0,0);
        //shmatadd2 = shmat(shid2,0,0);
        shmatadd2= shmat(shid2,0,0);
        shmatadd = shmat(shid,0,0);
        memcpy(shmatadd,&buf,sizeof(buf));
        printf("ser1:shmatadd success\n");
        while(1){
        /*
			判断指令是否和上次一样,不一样才会执行指令
			读取共享内存1
			判断什么指令
			根据判断执行相应指令
			执行完往共享内存2放数据
			进入下一次循环判断
			接收到quit退出循环进而退出程序
		*/
                //memset(cmd,0,sizeof(cmd));
                //if(strncmp(cmd,shmatadd,2)){
                        //strcpy(cmd,shmatadd);
                //if(strncmp(cmd,shmatadd->cmd,2)){
                if(memcmp(buf.cmd,shmatadd->cmd,2)){
                        memset(&buf,0,sizeof(buf));

                        memcpy(&buf,shmatadd,sizeof(buf));
                        //strcpy(cmd,buf.cmd);

                        //memset(&buf,0,sizeof(buf));

                        printf("ser1:buf.cmd=%s\n",buf.cmd);
                        set = get_cmd_type(buf.cmd);

                        if(set < 0){
                                GetPut(&buf, set);
                        }else if((set < 20) && (set > 0)){
                                //strcpy(buf,Cd(buf,cmd,set));
                                Cd(&buf,set);
                                //buf = Cd(cmd,set);
                                //printf("buf:%s\n",buf);
                        }else if(set == 0){
                                strcpy(buf.data,"quit...\n");
                        }else{
                                strcpy(buf.data,"zhiling fail...\n");
                        }
                        //printf("ser1 type+%c\n",buf.type);
                        memcpy(shmatadd2,&buf,sizeof(buf));
                }
                if(set == QUIT){
                        printf("ser1 quit\n");
                        break;
                }
                /*if((strcmp(cmd,"quit") == 0)){
                        printf("quit\n");
                        break;
                }*/
                sleep(1);
        }

        shmdt(shmatadd);
        if(shmctl(shid,IPC_RMID,0) == -1)
        {
                printf("shmctl no\n");
        }
        shmdt(shmatadd2);
        if(shmctl(shid2,IPC_RMID,0) == -1)
        {
                printf("shmctl2 no\n");
        }

        return 0;
}


客户端

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "ser.h"
char *S_tok(char *a)
{
        char *p;
        p = strtok(a," ");
        p = strtok(NULL," ");
        return p;
}

int get_cmd_type(char *buf)
{
        if(!strncmp("ls",buf,2))                return LS;
        if(!strncmp("lls",buf,3))               return LLS;
        if(!strncmp("quit",buf,4))              return QUIT;
        if(!strncmp("pwd",buf,3))               return PWD;
        if(!strncmp("lpwd",buf,4))              return LPWD;
        if(!strncmp("cd",buf,2))                return CD;
        if(!strncmp("lcd",buf,3))               return LCD;
        if(!strncmp("rm",buf,2))                return RM;
        if(!strncmp("lrm",buf,3))               return LRM;
        if(!strncmp("get",buf,3))               return GET;
        if(!strncmp("put",buf,3))               return PUT;
        return 100;
}
void serv_client(int c_fd)
{
        int fd;
        struct Mcc buf;
        char *p;
        char pd[128];
        char cmd[128];
        printf(">>ls,cd,rm,pwd,lls,lcd,lrm,lpwd,quit,get,put<<\n");
        while(1)
        {
                printf("------------------------------------------------\n");
                memset(&buf,0,sizeof(buf));
                printf(">");
                //do{
                //      gets(buf.cmd);
                //}while(!strncmp(cmd,buf.cmd,2));
                gets(buf.cmd);
                while(!strncmp(cmd,buf.cmd,2))
                {
                        printf("command repeat\n>");
                        gets(buf.cmd);
                }
                strcpy(cmd,buf.cmd);
                buf.set = get_cmd_type(buf.cmd);
                switch(buf.set)
                {
                        case QUIT:
                                write(c_fd,&buf,sizeof(buf));
                                printf("cl quit\n");
                                sleep(2);
                                return;
                                //exit(0);
                        case PWD:
                        case LS:
                                write(c_fd,&buf,sizeof(buf));
                                read(c_fd,&buf,sizeof(buf));
                                printf("%s",buf.data);
                                break;
                        case CD:
                                write(c_fd,&buf,sizeof(buf));
                                read(c_fd,&buf,sizeof(buf));
                                printf("%s",buf.data);
                                break;
                        case RM:
                                write(c_fd,&buf,sizeof(buf));
                                read(c_fd,&buf,sizeof(buf));
                                printf("%s",buf.data);
                                break;
                        case GET:
                                //p = S_tok(buf.cmd);
                                //strcpy(buf.cmd,p);
                                write(c_fd,&buf,sizeof(buf));
                                read(c_fd,&buf,sizeof(buf));
                                printf("buf.type = %d\n",buf.type);
                                if(buf.type != -1)
                                {
                                        //read(c_fd,&buf.a,sizeof(buf.a));
                                        //buf.getput = (char*)malloc(buf.a);
                                        //read(c_fd,buf.getput,buf.a);

                                        p = S_tok(buf.cmd);
                                        fd = open(p,O_RDWR|O_CREAT,0600);
                                        if(fd == -1){
                                                perror("open");
                                                printf("open on\n");
                                        }
                                        //printf("a=%d,getput=%ld\n",buf.a,strlen(buf.getput));
                                        write(fd,buf.data,sizeof(buf.data));
                                        close(fd);
                                        printf("get success\n");
                                        //free(buf.getput);
                                }
                                else
                                {
                                        printf("%s\n",buf.data);
                                }
                                break;
                        case PUT:
                                strcpy(cmd,buf.cmd);
                                p = S_tok(cmd);
                                //strcpy(buf.cmd,p);
                                if(access(p,F_OK) == -1)
                                {
                                        printf("NO This file!!!\n");
                                }
                                else
                                {
                                        fd = open(p,O_RDWR);
                                        //buf.a = lseek(fd,0,SEEK_END);
                                        //lseek(fd,0,SEEK_SET);
                                        //printf("lek:%d\n",buf.a);

                                        //buf.getput = (char*)malloc(buf.a);
                                        read(fd,buf.data,sizeof(buf.data));

                                        write(c_fd,&buf,sizeof(buf));

                                        //write(c_fd,buf.getput,buf.a);
                                        read(c_fd,&buf,sizeof(buf));
                                        close(fd);

                                        printf("%s\n",buf.data);
                                        //free(buf.getput);
                                }
                                break;
                        case LPWD:
                        case LLS:
                        case LRM:
                                p = buf.cmd;
                                p++;
                                system(p);
                                break;
                        case LCD:
                                p = S_tok(buf.cmd);
                                strcpy(buf.cmd,p);
                                printf("lcd=%s\n",buf.cmd);
                                if(chdir(buf.cmd) == -1)
                                {
                                        perror("why:");
                                }
                                printf("success......\n");
                                break;
                        case 100:
                                write(c_fd,&buf,sizeof(buf));
                                read(c_fd,&buf,sizeof(buf));
                                printf("%s",buf.data);
                                break;
                }
        }
}
int main(int argc,char **argv)
{
        if(argc != 3)
        {
                printf("usage: command listen_port\n");
                exit(-1);
        }
        int fid;
        int s_fd;
        struct sockaddr_in addr;
        memset(&addr,0,sizeof(struct sockaddr_in));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(atoi(argv[2]));
        inet_aton(argv[1],&addr.sin_addr);
        s_fd = socket(AF_INET,SOCK_STREAM,0);
        if(s_fd < 0)
        {
                perror("socket");
                exit(-1);
        }
        if(connect(s_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) == -1)
        {
                perror("connect");
                exit(-1);
        }
        printf("client......\n");
        fid = fork();
        if(fid < 0)
        {
                perror("fork error!");
                exit(-1);
        }
        if(fid == 0)
        {
                serv_client(s_fd);
                //exit(0);
        }
        wait(NULL);
        close(s_fd);
        return 0;
}

头文件


#define PWD 2
#define CD 3
#define RM 5

#define GET -1
#define PUT -2

#define LPWD 8
#define LCD 9
#define LRM 10

#define QUIT 0

struct Mcc
{
        char data[1024*5];
        char cmd[128];
        int type;
        int set;
};

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dz小伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值