博客
关于我
LINUX学习—FTP云服务器
阅读量:516 次
发布时间:2019-03-07

本文共 4376 字,大约阅读时间需要 14 分钟。

FTP服务器与客户端实现

服务器端代码解析

在本节中,我们将重点分析服务器端的实现细节。服务器端代码主要包含以下几个部分:

1. 包含头文件

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "config.h"

2. 命令处理函数

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

3. 获取目录函数

char *GetDir(char *dircmd){    char *p;    p = strtok(dircmd, " ");    p = strtok(NULL, " ");    return p;}

4. 命令执行函数

void command(struct Message msg, int fd){    char cmdbuf[1024] = {0};    char *file = NULL;    int file_fd;    printf("the client cmd is: %s\n", msg.data);    int cmd = option_cmd(msg.data);    switch(cmd)    {        case LS:            msg.type = 0;            FILE *p = popen(msg.data, "r");            fread(msg.data, sizeof(msg.data), 1, p);            write(fd, &msg, sizeof(msg));            break;        case PWD:            msg.type = 0;            write(fd, &msg, sizeof(msg));            break;        case QUIT:            printf("the client die out\n");            exit(-1);            break;        case CD:            msg.type = 1;            char *dir = GetDir(msg.data);            break;        case PUT:            file_fd = open(GetDir(msg.data), O_CREAT | O_RDWR, 0666);            write(file_fd, msg.msgbuf, strlen(msg.msgbuf));            close(file_fd);            break;        case GET:            file = GetDir(msg.data);            if (access(file, F_OK) == -1)            {                strcpy(msg.data, "the file don't exit");                write(fd, &msg, sizeof(msg));            }            else            {                msg.type = DOFILE;                file_fd = open(file, O_RDWR);                read(file_fd, cmdbuf, sizeof(cmdbuf));                close(file_fd);                strcpy(msg.data, cmdbuf);                write(fd, &msg, sizeof(msg));            }            break;        default:            break;    }}

客户端代码解析

客户端代码的实现主要包含以下几个部分:

1. 包含头文件

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "config.h"

2. 命令处理函数

int option_cmd(char *cmd){    if (strcmp(cmd, "ls") == 0) return LS;    if (strcmp(cmd, "pwd") == 0) return PWD;    if (strcmp(cmd, "lls") == 0) return LLS;    if (strcmp(cmd, "quit") == 0) return QUIT;    if (strstr(cmd, "get") != NULL) return GET;    if (strstr(cmd, "put") != NULL) return PUT;    if (strcmp(cmd, "cd") == 0) return CD;    if (strstr(cmd, "lcd") != NULL) return LCD;    return -1;}

3. 获取目录函数

char *GetDir(char *cmd){    char *p;    p = strtok(cmd, " ");    p = strtok(NULL, " ");    return p;}

4. 命令执行函数

int option_cmd_handle(struct Message msg, int fd){    char buf[128] = {0};    char cmdbuf[1024];    int CMD;    strncpy(buf, msg.data, sizeof(msg.data));    char *dir = GetDir(buf);    CMD = option_cmd(buf);    switch(CMD)    {        case LS:        case PWD:            msg.type = 0;            write(fd, &msg, sizeof(msg));            break;        case CD:            msg.type = 1;            write(fd, &msg, sizeof(msg));            break;        case LCD:            chdir(dir);            break;        case QUIT:            strcpy(msg.data, "quit");            write(fd, &msg, sizeof(msg));            close(fd);            exit(-1);            break;        case LLS:            system("ls");            break;        case GET:            write(fd, &msg, sizeof(msg));            break;        case PUT:            strcpy(cmdbuf, msg.data);            if (access(dir, F_OK) == -1)            {                printf("the %s file is no have\n", dir);            }            else            {                file_fd = open(dir, O_RDWR);                read(file_fd, msg.msgbuf, sizeof(msg.msgbuf));                close(file_fd);                write(fd, &msg, sizeof(msg));            }            break;        default:            break;    }    return CMD;}

总结

通过上述分析可以看出,FTP服务器与客户端的实现主要包含以下几个关键部分:

  • 套接字编程:实现了客户端与服务器之间的连接
  • 命令处理:服务器端根据客户端传输的命令进行相应的操作
  • 文件传输:支持文件的上传和下载
  • 目录操作:支持切换目录和查看目录内容
  • 整个实现过程体现了TCP/IP协议栈的应用,尤其是socket编程的核心知识点。

    转载地址:http://eotnz.baihongyu.com/

    你可能感兴趣的文章
    NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_处理器介绍_处理过程说明---大数据之Nifi工作笔记0019
    查看>>
    NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_实际操作---大数据之Nifi工作笔记0020
    查看>>
    NIFI大数据进阶_Json内容转换为Hive支持的文本格式_实际操作_02---大数据之Nifi工作笔记0032
    查看>>
    NIFI大数据进阶_Json内容转换为Hive支持的文本格式_操作方法说明_01_EvaluteJsonPath处理器---大数据之Nifi工作笔记0031
    查看>>
    NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka消费者处理器_来消费kafka数据---大数据之Nifi工作笔记0037
    查看>>
    NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
    查看>>
    NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
    查看>>
    NIFI大数据进阶_NIFI监控功能实际操作_Summary查看系统和处理器运行情况_viewDataProvenance查看_---大数据之Nifi工作笔记0026
    查看>>
    NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
    查看>>
    NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
    查看>>
    NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
    查看>>
    NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
    查看>>
    NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
    查看>>
    NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_01---大数据之Nifi工作笔记0033
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_说明操作步骤---大数据之Nifi工作笔记0028
    查看>>
    NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
    查看>>
    NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
    查看>>
    NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
    查看>>