基于 linux + c 实现的简易版 FTP
具体功能有
- get + 文件名 实现从服务器获取某个文件到客户端
- put + 文件名 实现从客户端上传某个文件到服务器
- cd + 路径 实现切换服务器文件目录
- lls 实现列出客户端(本地)文件目录列表
- ls 实现列出服务端文件目录列表
- 客户端输入quit 退出,服务端支持重连
client
#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 <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