linux ftp服务器客户端项目
实现功能:
服务器客户端连接上之后 客户端输入ls pwd cd会显示服务器下的内容 按下lls lpwd lcd显示客户端的内容、按下get 客户端获取到服务器上的内容、按下put 把客户端的内容上传到服务器
设计思路:
1 网络模型得建立 服务器 客户端 通过主函数传入 ip地址和端口号、while1 不断连接 创建子进程不断等待读取 或者发送、
2 定义一个msg的结构体、
3 客户端获取输入 解析指令 发送给服务器。 服务器 收到内容解析指令 并发回客户端。
get时则需要读文件 定义一个收到的 msg.type = DOFILE 创建 写进去。
ls pwd cd get 则需要服务器 发送回来 客户端 read。
lls lcd lpwd quit则不需要发送数据 直接执行 所以不用接收 大于INFO
4 一个一个写 双方的收发 都加打印信息 方便调试!!!!!!!!!!!!!!!!!!!
函数用法
#define LS 0
if(!strcmp("ls",cmd)) return LS; // strcmp一样则返回0 取反返回1
if(strstr(cmd,"cd") != NULL) return CD; //strstr 查找cmd中有没有cd 有 返回1
FILE *r = popen(msg.cmd,"r"); //popen流 fread读取存放在msg.cmd中 再通过write发送回去
memset(&msg.cmd,0,sizeof(msg.cmd));//先清空 防止存在干扰
char *getDesDir(char *cmsg) // get aa 用空格把字符串分割 返回aa的地址
{
char *p;
p = strtok(cmsg," ");
p = strtok(NULL," ");
return p;
}
chdir(dir); //定义到该地址
if(access(file,F_OK) == -1){ //文件不存在
memset(&s_addr,0,sizeof(struct sockaddr_in));
代码段
//服务器
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define LS 0
#define PWD 1
#define CD 2
#define GET 4
#define INFO 5
#define LCD 6
#define PUT 7
#define LLS 8
#define QUIT 9
#define LPWD 11
#define DOFILE 10
struct Msg{
int type;
char cmd[1024];
char data[1024];
};
int get_cmd_type(char *cmd) //解析指令并返回给getDesDir函数
{
if(!strcmp("ls",cmd)) return LS; // strcmp一样则返回0 取反返回1
if(!strcmp("pwd",cmd)) return PWD;
if(!strcmp("lls",cmd)) return LLS;
if(!strcmp("lcd",cmd)) return LCD;
if(!strcmp("quit",cmd)) return QUIT;
if(!strcmp("lpwd",cmd)) return LPWD;
if(strstr(cmd,"cd") != NULL) return CD; //strstr 查找cmd中有没有cd 有 返回1
if(strstr(cmd,"get") != NULL) return GET;
if(strstr(cmd,"put") != NULL) return PUT;
return 100;
}
char *getDesDir(char *cmsg) // get aa 用空格把字符串分割 返回aa的地址
{
char *p;
p = strtok(cmsg," ");
p = strtok(NULL," ");
return p;
}
void msg_handler(struct Msg msg,int fd)//服务器收到信息处理函数
{
int ret;
char dataBuf[256];
char *file;
char *dir;
int filefd;
printf("server:%s\n",msg.cmd);
ret = get_cmd_type(msg.cmd); //解析是什么指令
switch(ret){
case LS:
case PWD:
msg.type = 0;
FILE *r = popen(msg.cmd,"r"); //通过popen流 然后fread读取存放在msg.cmd中 再通过write发送回去
memset(&msg.cmd,0,sizeof(msg.cmd));//先清空 防止存在干扰
fread(msg.cmd,sizeof(msg.cmd),1,r);
write(fd,&msg,sizeof(msg));
break;
case CD:
char *dir = getDesDir(msg.cmd); //cd aa 找到aa地址
printf("dir:%s\n",dir);
chdir(dir); //定义到该地址
write(fd,&msg,sizeof(msg)); //write返回去 如果不写 则要大于IFGO 防止客户端阻塞读取
break;
case GET:
file = getDesDir(msg.cmd); //定义到get aa地址 判断文件是否存在
if(access(file,F_OK) == -1){ //文件不存在
strcpy(msg.cmd,"No This File\n");
write(fd,&msg,sizeof(msg)); //发送文件不存在
}
else{
msg.type == DOFILE; //文件存在 则定义type读文件
int fd_file = open(file,O_RDWR); //到开该文件
memset(dataBuf,0,sizeof(dataBuf));//清空
read(fd_file,dataBuf,sizeof(dataBuf));//读取到dataBuf
close(fd_file);//关闭该文件
strcpy(msg.cmd,dataBuf);//拷贝到msg.cmd中 发送到客户端 ----- 客户收到则创建文件 写进去
write(fd,&msg,sizeof(msg));
}
break;
case PUT:
dir = getDesDir(msg.cmd); //客户端判断该文件是否存在 存在则打开 读取 存放再msg.data中 发送给服务器
filefd = open(dir,O_RDWR|O_CREAT,0600);//服务器收到 创建文件 写进去
write(filefd,msg.data,sizeof(msg.data));
close(filefd);
write(fd,&msg,sizeof(msg));
break;
case QUIT:
printf("cliten quit!\n");
exit(-1);
}
}
int main(int argc,char **argv)//网络模型的建立 while不断连接 fork子进程不断读取判断
{
int s_fd;
int c_fd;
int mark = 0;
int bind_fd;
int cLen = sizeof(struct sockaddr_in);
int n_read;
char readBuf[128] = {0};
char writeBuf[128] = {0};
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
struct Msg msg;
if(argc != 3){
perror("argc error");
}
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_fd = bind(s_fd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr_in));
if(bind_fd == -1){
perror("bind");
exit(-1);
}
listen(s_fd,10);
while(1){
c_fd = accept(s_fd,(struct sockaddr*)&c_addr,&cLen);
if(c_fd == -1){
perror("accept");
}
else{
printf("IP:%s\n",inet_ntoa(c_addr.sin_addr));
}
mark++;
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;
}
if(n_read > 0){
msg_handler(msg,c_fd);
}
}
}
}
return 0;
}
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#define LS 0
#define PWD 1
#define CD 2
#define GET 4
#define IFGO 5
#define LCD 6
#define LLS 8//lls put lcd quit
#define PUT 7
#define QUIT 9
#define LPWD 11
#define DOFILE 10
struct Msg{
int type;
char cmd[1024];
char data[1024];
};
int get_cmd_type(char *cmd)
{
if(!strcmp("ls",cmd)) return LS;
if(!strcmp("pwd",cmd)) return PWD;
if(!strcmp("lls",cmd)) return LLS;
if(!strcmp("lcd",cmd)) return LCD;
if(!strcmp("quit",cmd)) return QUIT;
if(!strcmp("lpwd",cmd)) return LPWD;
if(strstr(cmd,"cd") != NULL) return CD;
if(strstr(cmd,"get") != NULL) return GET;
if(strstr(cmd,"put") != NULL) return PUT;
return -1;
}
char *getDesDir(char *cmsg)
{
char *p;
p = strtok(cmsg," ");
p = strtok(NULL," ");
return p;
}
int cmd_handler(struct Msg msg,int fd)//获取到输入 发送函数
{
int ret;
char *dir;
char buf[512];
int filefd;
ret = get_cmd_type(msg.cmd);//解析指令
switch(ret){
case LS:
case PWD:
write(fd,&msg,sizeof(msg));
break;
case CD:
write(fd,&msg,sizeof(msg));
break;
case GET:
write(fd,&msg,sizeof(msg));
break;
case PUT:
strcpy(buf,msg.cmd);
dir = getDesDir(buf);
if(access(dir,F_OK) == -1){
printf("Not This Filei\n");
}
else{
filefd = open(dir,O_RDWR);
read(filefd,msg.data,sizeof(msg.data));
write(fd,&msg,sizeof(msg));
}
break;
case LPWD:
system("pwd");
break;
case LLS:
system("ls");
break;
case LCD:
dir = getDesDir(msg.cmd);
chdir(dir);
break;
case QUIT:
strcpy(msg.cmd,"quit");
write(fd,&msg,sizeof(msg));
exit(-1);
break;
}
}
void handler_server_message(struct Msg msg,int fd)
{
int n_read;
struct Msg msgget;
memset(&msgget,0,sizeof(msgget));
memset(&msg,0,sizeof(msg));
n_read = read(fd,&msg,sizeof(msg));
if(n_read == 0){
printf("server is out,quit\n");
exit(-1);
}
else if(msgget.type == DOFILE){
char *p = getDesDir(msg.cmd);
int new_filefd = open(p,O_RDWR|O_CREAT,0600);
write(new_filefd,msg.cmd,strlen(msg.cmd));
close(new_filefd);
putchar('>');
fflush(stdout);
}
else{
printf("--------------------------------\n");
printf("\n%s\n",msg.cmd);
printf("--------------------------------\n");
putchar('>');
fflush(stdout);
}
}
int main(int argc,char **argv)
{
int c_fd;
int connect_fd;
char writeBuf[128] = {0};
char readBuf[128] = {0};
int n_read;
struct sockaddr_in c_addr;
struct Msg msg;
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
perror("argc error");
}
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);
connect_fd = connect(c_fd,(struct sockaddr*)&c_addr,sizeof(struct sockaddr_in));
if(connect_fd == -1){
perror("connect");
}
while(1){
printf(">");
gets(msg.cmd); //获取输入
if(strlen(msg.cmd) == 0){//没有输入不执行后面代码 continue
printf(">");
continue;
}
if(strlen(msg.cmd) > 0){ //有输入
int ret = cmd_handler(msg,c_fd);//调用该函数 解析指令并 发送
if(ret < IFGO){ //!!!!返还回来的值如果 《 则不需要接收读取
putchar('>');
fflush(stdout);
continue;
}
if(ret == -1){//没有该指令
printf(">");
fflush(stdout);
continue;
}
handler_server_message(msg,c_fd); //否则调用接收函数
}
}
return 0;
}