基于 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, sizeof(msg));
break;
case PUT:
strcpy(buf, msg.cmd);
dir = getdir(buf);
if(access(dir, F_OK) == -1){
printf("%s not exsit\n", dir);
}else{
filefd = open(dir, O_RDWR);
read(filefd, msg.seconBuf, sizeof(msg.seconBuf));
close(filefd);
write(c_fd, &msg, sizeof(msg));
}
break;
case LLS:
system("ls");
break;
case LCD:
dir = getdir(msg.cmd);
chdir(dir);
break;
case QUIT:
strcpy(msg.cmd, "quit");
write(c_fd, &msg, sizeof(msg));
close(c_fd);
exit(-1);
}
return ret;
}
void serverMsgHandler(struct Msg msg, int c_fd)
{
int n_read;
struct Msg msgGet;
char *dir = NULL;
int newFile;
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){
dir = getdir(msg.cmd);
newFile = open(dir, O_RDWR|O_CREAT, 0600);
write(newFile, msgGet.cmd, strlen(msgGet.cmd));
putchar('>');
fflush(stdout);
}else{
printf("==========================\n");
printf("\n%s\n", msgGet.cmd);
printf("===========================\n");
putchar('>');
fflush(stdout);
}
}
int main(int argc, char **argv)
{
int c_fd;
int ret;
struct sockaddr_in c_addr;
struct Msg msg;
if(argc != 3){
printf("param is not good\n");
exit(-1);
}
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);
int con = connect(c_fd, (struct sockaddr *)&c_addr, sizeof(struct sockaddr));
if(con == -1){
perror("connect");
exit(-1);
}
printf("connect---------------------\n");
int mark = 0;
while(1){
memset(msg.cmd, 0, sizeof(msg.cmd));
if(mark == 0){
printf(">");
}
gets(msg.cmd);
if(strlen(msg.cmd) == 0){
if(mark == 1){
continue;
}
}
mark = 1;
ret = cmd_handler(msg, c_fd);
if(ret > IFGO){
putchar('>');
fflush(stdout);
continue;
}
if(ret == -1){
printf("cmd not \n");
printf(">");
fflush(stdout);
continue;
}
serverMsgHandler(msg, c_fd);
}
close(c_fd);
return 0;
}
server
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.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 secondBuf[128];
};
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;
}
void msg_handler(struct Msg msg, int fd)
{
char *file = NULL;
char dataBuf[1024] = {0};
int fdFile;
int ret = getCmdType(msg.cmd);
switch(ret){
case LS:
case PWD:
msg.type = 0;
FILE *r = popen(msg.cmd, "r");
fread(msg.cmd, sizeof(msg.cmd), 1, r);
write(fd, &msg, sizeof(msg));
break;
case CD:
msg.type = 1;
char *dir = getdir(msg.cmd);
printf("dir: %s\n", dir);
chdir(dir);
break;
case GET:
file = getdir(msg.cmd);
if(access(file, F_OK) == -1){
strcpy(msg.cmd, "No This file!");
write(fd, &msg, sizeof(msg));
}else{
msg.type = DOFILE;
fdFile = open(file, O_RDWR);
read(fdFile, dataBuf, sizeof(dataBuf));
close(fdFile);
strcpy(msg.cmd, dataBuf);
write(fd, &msg, sizeof(msg));
}
break;
case PUT:
fdFile = open(getdir(msg.cmd), O_RDWR|O_CREAT, 0666);
write(fdFile, msg.secondBuf, strlen(msg.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;
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));
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);
bind(s_fd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in));
listen(s_fd, 10);
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.cmd, 0, sizeof(msg.cmd));
n_read = read(c_fd, &msg, sizeof(msg));
if(n_read == 0){
printf("client out\n");
break;
}else if(n_read > 0){
msg_handler(msg, c_fd);
}
}
}
}
close(s_fd);
close(c_fd);
return 0;
}
运行结果
