基于 linux + c 实现的简易版 FTP

该博客介绍了基于Linux和C语言实现的简易版FTP。具备从服务器获取文件、客户端上传文件、切换服务器文件目录、列出客户端和服务端文件目录列表等功能,客户端输入quit可退出,服务端支持重连,并展示了运行结果。

基于 linux + c 实现的简易版 FTP

具体功能有

  • get + 文件名 实现从服务器获取某个文件到客户端
  • put + 文件名 实现从客户端上传某个文件到服务器
  • cd + 路径 实现切换服务器文件目录
  • lls 实现列出客户端(本地)文件目录列表
  • ls 实现列出服务端文件目录列表
  • 客户端输入quit 退出,服务端支持重连

client

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

#define LS   0
#define PWD  1
#define GET  2
#define IFGO 3
#define CD   4
#define PUT  5
#define LLS  6
#define LCD  7
#define LPWD 8
#define QUIT 9
#define DOFILE 10
struct Msg
{
   
   
        int type;
        char cmd[1024];
        char seconBuf[1280];
};
char* getdir(char *cmd)
{
   
   
        char *p = NULL;
        p = strtok(cmd, " ");
        p = strtok(NULL, " ");
        return p;
}
int getCmdType(char *cmd)
{
   
   
        if(!strcmp("ls", cmd))   return LS;
        if(!strcmp("lls", cmd))  return LLS;
        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;

        return -1;
}
int cmd_handler(struct Msg msg, int c_fd)
{
   
   
        int ret;
        char buf[32] = {
   
   0};
        int filefd;
        char *dir = NULL;

        ret = getCmdType(msg.cmd);
        switch(ret){
   
   
                case LS:
                case CD:
                case PWD:
                        msg.type = 0;
                        printf("msg: %s\n", msg.cmd);
                        write(c_fd, &msg, sizeof(msg));
                        break;
                case GET:
                        msg.type = 2;
                        write(c_fd, &msg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值