socket网络编程搭建FTP云盘

1.socket客户端

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


int  get_cmd_type(char *cmd )
{
        if(!strcmp("ls",cmd))       return LS;
        if(!strcmp("quit",cmd))     return QUIT;
        if(!strcmp("pwd",cmd))      return PWD;
        if(strstr(cmd,"cd") != NULL)    return CD;
        if(strstr(cmd,"get") != NULL)   return GET;
        if(strstr(cmd,"put") != NULL)   return PUT;

        return -1;
}

char *getDesDir(char *cmsg)//字符串分割(以空格为分割)
{
        char *p;
        p = strtok(cmsg," ");
        p = strtok(NULL," ");
        return p;

}


void msg_handler(struct Msg msg,int fd)
{
        char dataBuf[1024] = {0};
        char *dir = NULL;
        char *file = NULL;
        int fdfile;

        printf("cmd:%s\n",msg.data);//打印客户端的命令
        int ret = get_cmd_type(msg.data);//识别指令

        switch(ret){
                case LS:
                case PWD:
                        FILE *r = popen(msg.data,"r");//通过创建一个管道的方式打开一个进程,并调用shell
                        fread(msg.data,sizeof(msg.data),1,r);//把内容读到msg.data中
                        write(fd,&msg,sizeof(msg));//把内容发送到客户端
                        break;
                case CD:
                        dir = getDesDir(msg.data);//获取空格后的字符串
                        printf("dir:%s\n",dir);
                        chdir(dir);//自己进入另外一个文件
                        break;
                case GET:
                        file = getDesDir(msg.data);//获取空格后的字符串
                        if(access(file,F_OK) == -1){ //判断文件是否存在
                                strcpy(msg.data,"No This File!");
                                write(fd,&msg,sizeof(msg));
                        }else{
                        		msg.type = DOFILE;
                                fdfile = open(file,O_RDWR);//打开文件
                                read(fdfile,dataBuf,sizeof(dataBuf));//把内容读到dataBuf中
                                close(fdfile);//关闭文件

                                strcpy(msg.data,dataBuf);//把dataBuf中的内容读到msg.data中
                                write(fd,&msg,sizeof(msg));//把内容发送到客户端
                        }
                        break;
                case PUT:
                        fdfile = open(getDesDir(msg.data),O_RDWR|O_CREAT,0666);//打开put要上传的文件,若无则创建
                        write(fdfile,msg.secondBuf,strlen(msg.secondBuf));//把msg.secondBuf的内容写到put要上传的文件
                        close(fdfile);
                        break;
                case QUIT:
                        printf("client quit!\n");//退出
                        exit(-1);

        }
}
int main(int argv,char **argc )
{
        int s_fd;
        int c_fd;
        int n_read;
        int n_addrlen;
        char readbuf[128] = {0};

        struct sockaddr_in s_addr;
        struct sockaddr_in c_addr;
        struct Msg msg;

        if(argv != 3){
                printf("param is not good\n");
                exit(-1);
        }

        memset(&s_addr,0,sizeof(struct sockaddr_in));//初始化,保证输入空间
        memset(&c_addr,0,sizeof(struct sockaddr_in));

        //int socket(int domain, int type, int protocol);
        s_fd = socket(AF_INET, SOCK_STREAM, 0);//创建套接字,IPV4,流式套接字,使用tcp协议
        if(s_fd == -1){
                perror("socket:");
                exit(-1);
        }


        s_addr.sin_family = AF_INET;//ipv4 协议簇
        s_addr.sin_port = htons(atoi(argc[2]));//端口号
        inet_aton( argc[1],&s_addr.sin_addr);//IP地址
        //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
        bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));//添加套接字信息

        //int listen(int sockfd, int backlog);
        listen(s_fd, 10);//监听,最多10个
        n_addrlen = sizeof(struct sockaddr_in);
        while(1){
                //int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
                c_fd = accept(s_fd, (struct sockaddr *)&c_addr, &n_addrlen);//接受连接
                if(c_fd == -1){
                        perror("why:");
                }else{
                        printf("connect success!id:%s\n",inet_ntoa(c_addr.sin_addr));//返回客户端IP地址
                }
                if(fork() == 0){
                        while(1){
                                memset(readbuf,0,sizeof(readbuf));
                                n_read = read(c_fd, &msg,sizeof(msg));//接收客户端的指令
                                if(n_read == 0){
                                        perror("read:");
                                        printf("client out\n");
                                        break;
                                }else if(n_read >0){
                                        msg_handler(msg,c_fd);
                                }
                        }
                }

        }
        close(s_fd);
        close(c_fd);

        return 0;
}

2.client客户端

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


int  get_cmd_type(char *cmd )
{
        if(!strcmp("ls",cmd))       return LS;
        if(!strcmp("quit",cmd))     return QUIT;
        if(!strcmp("pwd",cmd))      return PWD;
        if(!strcmp("lls",cmd))      return LLS;

        if(strstr(cmd,"cd") != NULL)    return CD;
        if(strstr(cmd,"get") != NULL)   return GET;
        if(strstr(cmd,"put") != NULL)   return PUT;
        if(strstr(cmd,"lcd") != NULL)   return LCD;

        return -1;
}

char *getdir(char *cmsg)
{
        char *p;
        p = strtok(cmsg," ");
        p = strtok(NULL," ");
        return p;

}


int  cmd_handler(struct Msg msg,int c_fd)
{
        char *dir = NULL;
        char buf[32];
        int filefd;



        int ret = get_cmd_type(msg.data);//识别指令

        switch(ret){
                case LS:
                case PWD:
                        write(c_fd,&msg,sizeof(msg));
                        break;
                case CD:
                        write(c_fd,&msg,sizeof(msg));
                        break;
                case GET:
                        write(c_fd,&msg,sizeof(msg));
                        break;
                case PUT:
                        //dir = getdir(msg.data);//获取空格后的字符串
						strcpy(buf,msg.data);//防止buf断裂,否则无法使用完整命令
                        dir = getdir(buf);

                        if(access(dir,F_OK) == -1){//判断文件是否存在
                                printf("%s not exsit\n",dir);
                        }else{
                                filefd = open(dir,O_RDWR);//打开文件
                                read(filefd,msg.secondBuf,sizeof(msg.secondBuf));//把文件读到msg.secondBuf
                                close(filefd);

                                write(c_fd,&msg,sizeof(msg));
                        }
                        break;
                case LLS:
                        system("ls");
                        break;
                case LCD:
                        dir = getdir(msg.data);
                       chdir(dir);
                        break;
                case QUIT:
                        strcpy(msg.data,"quit");
                        write(c_fd,&msg,sizeof(msg));
                        close(c_fd);
                        exit(-1);

        }
        return ret;
}

void handler_server_message(int c_fd,struct Msg msg)
{
        int n_read;
        int newfilefd;
        struct Msg msgget;

        n_read = read(c_fd,&msgget,sizeof(msgget));//把内容读到msgget

        if(n_read == 0){
                printf("server is out,quit\n");
                exit(-1);
        }else if(msgget.type == DOFILE){//获取文件操作
                char *p = getdir(msg.data);
                newfilefd = open(p,O_RDWR|O_CREAT,0600);//打开文件,若无则创建
                write(newfilefd,msgget.data,strlen(msgget.data));//把从服务端接收的数据,写入文件中

        }else{
                printf("--------------------------------------------\n");
                printf("\n%s\n",msgget.data);
                printf("--------------------------------------------\n");

        }
}





int main(int argv,char **argc)
{
        int c_fd;
        int ret;

        struct sockaddr_in c_addr;
        struct Msg msg;

        memset(&c_addr,0,sizeof(struct sockaddr_in));

        //int socket(int domain, int type, int protocol);
        c_fd = socket(AF_INET, SOCK_STREAM, 0);
        if(c_fd == -1){
                perror("why:");
                exit(-1);
        }


        c_addr.sin_family = AF_INET;
        c_addr.sin_port = htons(atoi(argc[2]));
        inet_aton( argc[1],&c_addr.sin_addr);
        if(connect(c_fd, (struct sockaddr *)&c_addr,sizeof(struct sockaddr_in))==-1){//连接服务端
                perror("connect:");
                exit(-1);
        }
        printf("connect....\n");
        while(1){

                memset(msg.data,0,strlen(msg.data));
                printf(">");
                gets(msg.data);//输入指令

                ret = cmd_handler(msg,c_fd);
                if(ret > IFGO){
                        fflush(stdout);
                        continue;
                }
                if(ret == -1){
                        printf("command not \n");
                        fflush(stdout);
                        continue;
                }
                handler_server_message(c_fd,msg);//打印消息
        }
        return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值