Linux编程 ------ ftp 服务器

功能

远程
1.获取服务器的文件 get xx
2.展示服务器有哪些文件 ls
3.进入服务器 xx 文件夹 cd xx
4.上传文件到服务器 put xx
本地
1.查看客户端本地文件 lls
2.进入客户端 xx 文件夹 lcd xx

服务端代码

#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 <config.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define LS      0
#define GET     1
#define PWD     2

#define IFGO    3

#define LCD     4
#define LLS     5
#define CD      6
#define PUT     7

#define QUIT    8
#define DOFILE  9

struct Msg
{
        int type;
        char data[1024];
        char secondBuf[128];
};

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 100;
}

char *getDesDir(char *cmsg)
{
        char *p;
        p = strtok(cmsg," ");//以空格来分隔,获取第一个字符串
        p = strtok(NULL," ");//获取第二个串开始,第一个参数都要写 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.data);//打印出客户端发过来的指令
        int ret = get_cmd_type(msg.data);//把字符串传到get_cmd_type转换成我们想要的整型数
        
		switch(ret){
                case LS://列出服务端当前文件夹所有文件
                case PWD://列出服务端当前路径
                        msg.type = 0;
                        FILE *r = popen(msg.data, "r");//打开
                        fread(msg.data, sizeof(msg.data), 1, r);//把msg结果读出来
                        write(fd, &msg, sizeof(msg));//写到fd(fd是socket通道),发送给客户端

                        break;
                case CD://进入文件夹,需要分割字符串
                        msg.type = 1;
                        char *dir = getDesDir(msg.data);//调用getDesDir来获取cd后面的字符串
                        printf("dir:%s\n",dir);
                        chdir(dir);//chdir 是C语言中的一个系统调用函数(同cd),用于改变当前工作目录,其参数为 dir 目标目录
                        break;

                case GET://获取文件
                        file = getDesDir(msg.data);//调用getDesDir来获取cd后面的文件(字符串)

                        if(access(file, F_OK) == -1){//判断文件是否存在
                                strcpy(msg.data, "No This File!");//如果没有此文件,把No This File!拷贝到msg的信息
                                write(fd, &msg, sizeof(dataBuf));//返回给客户端
                        }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);//打开客户端发送过来的文件,如果客户端没有此文件则创建此文件
                        write(fdfile, msg.secondBuf, strlen(msg.secondBuf));//此时msg结构体的data存放的是指令,secondBuf存放的是文件的内容
                        close(fdfile);
                        break;
                case QUIT://退出
                        printf("client Quit!\n");
                        exit(-1);
        }
}
int main(int argc,char **argv)
{
        int s_fd;
        int c_fd;
        int n_read;
        char readBuf[128];

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

        if(argc != 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));

        //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.data, 0, sizeof(msg.data));//清空数据
                                n_read = read(c_fd, &msg, sizeof(msg));//首先是读取客户端传过来的指令,读不到就等待
                                if(n_read == 0){//当读到的字节数为0时,代表客户端已经退出
                                        printf("client out\n");
                                        break;
                                }else if(n_read > 0){//当读到的字节数>0代表读到真正有效的数据,
                                        msg_handler(msg, c_fd);//msg和c_fd传到msg_handler函数
                                }

                        }
                }
        }
        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 <config.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define LS      0
#define GET     1
#define PWD     2

#define IFGO    3

#define LCD     4
#define LLS     5
#define CD      6
#define PUT     7

#define QUIT    8
#define DOFILE  9

struct Msg
{
        int type;
        char data[1024];
        char secondBuf[128];
};

char * getdir(char *cmd)
{
        char *p;

        p = strtok(cmd, " ");
        p = strtok(NULL, " ");//获取第二个串开始,第一个参数都要写 NULL

        return p;
}

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

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

        return -1;//没有符合的字符串返回-1
}

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

        ret = get_cmd_type(msg.data);//把字符串变成整型数
		
        switch(ret){
                case LS:
                case CD:
                case PWD:
                        msg.type = 0;
                        write(fd, &msg, sizeof(msg));
                        break;
                case GET://获取文件
                        msg.type = 2;
                        write(fd, &msg, sizeof(msg));
                        break;
                case PUT://放入文件
                        strcpy(buf, msg.data);
                        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(fd, &msg, sizeof(msg));//发送给服务端
                        }
                        break;
                case LLS://列出客户端当前文件夹所有文件
                        system("ls");
                        break;
                case LCD://进入客户端XX文件夹
                        dir = getdir(msg.data);
                        chdir(dir);
                        break;
                case QUIT:
                        strcpy(msg.data, "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){//服务器接收到get指令回返回DOFILE,让客户端创建文件
                char *p = getdir(msg.data);//获取到文件名的字符串
                newfilefd = open(p, O_RDWR|O_CREAT, 0600);
                write(newfilefd, msgget.data, strlen(msgget.data));
                putchar('>');
                fflush(stdout);
        }
        else{
                printf("-----------------------------------\n");
                printf("\n%s\n",msgget.data);
                printf("-----------------------------------\n");

                putchar('>');
                fflush(stdout);//进行下一次输入
        }
}

int main(int argc,char **argv)
{
        int c_fd;
        struct sockaddr_in c_addr;

        struct Msg msg;

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

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


        //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.data, 0, sizeof(msg.data));
                if(mark == 0)   printf(">");//美化输入界面的格式

                gets(msg.data);//接入以后通过while不断获取用户的输入,存放在结构体msg的data

                if(strlen(msg.data) == 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;
}		
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

从入门到捕蛇者说

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

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

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

打赏作者

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

抵扣说明:

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

余额充值