定义命令宏代码
#define LS 0 #define PWD 1 #define GET 2 #define IFGO 3 #define LLS 4 #define LCD 5 #define CD 6 #define PUT 7 #define QUIT 8 #define DOFILE 9 struct Msg { int type; char data[1024]; //存放命令和文件数据 char secondbuf[1024]; //存放文件内容 };
服务器代码
#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> //#include<config.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> #include<string.h> #include "config.h" #include<sys/stat.h> #include<fcntl.h> int get_cmd_type(char *cmd) { if(!strcmp("ls",cmd)) return LS; if(!strcmp("quit",cmd))return QUIT; if(!strcmp("pwd",cmd)) return PWD; if(strstr(cmd,"cd")!=NULL) return CD; if(strstr(cmd,"get")!=NULL)return GET; if(strstr(cmd,"put")!=NULL)return PUT; return 100; } char *getdesdir(char *cmsg) { char *p; p = strtok(cmsg," "); p = strtok(NULL," "); return p; } void msg_handler(struct Msg msg,int fd) { char databuf[1024]={0}; char *file = NULL; int fdfile; printf("cmd:%s\n",msg.data); int ret = get_cmd_type(msg.data); switch(ret){ case LS: case PWD: msg.type = 0; FILE *r = popen(msg.data,"r"); fread(msg.data,sizeof(msg.data),1,r); write(fd,&msg,sizeof(msg)); break; case CD: msg.type = 1; char *dir = getdesdir(msg.data); printf("dir:%s\n",dir); chdir(dir); break; case GET: file = getdesdir(msg.data); if(access(file,F_OK)==-1){ strcpy(msg.data,"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.data,databuf); write(fd,&msg,sizeof(msg)); } break; case PUT: fdfile = open(getdesdir(msg.data),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; char readbuf[128]; 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)); memset(&c_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.data,0,sizeof(msg.data)); 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(c_fd); close(s_fd); return 0; }
客户端代码
#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"config.h" //#include<sys/stat.h> #include<fcntl.h> char * getdir(char *cmd) { char *p; p = strtok(cmd," "); p = strtok(NULL," "); return p; } int get_cmd_type(char *cmd) { if(strstr(cmd,"lcd")) return LCD; if(!strcmp("quit",cmd)) return QUIT; if(!strcmp("ls",cmd)) return LS; if(!strcmp("lls",cmd)) return LLS; if(!strcmp("pwd",cmd)) return LS; if(strstr(cmd,"cd")!=NULL) return CD; if(strstr(cmd,"get")!=NULL)return GET; if(strstr(cmd,"put")!=NULL)return PUT; return -1; } int cmd_handler(struct Msg msg,int fd) { char *dir = NULL; char buf[32]; int ret; int filefd; ret = get_cmd_type(msg.data); switch(ret){ case LS: case CD: case PWD: msg.type = 0; write(fd,&msg,sizeof(msg)); break; case GET: msg.type = 2; write(fd,&msg,sizeof(msg)); break; case PUT: strcpy(buf,msg.data); dir = getdir(buf); if(access(dir,F_OK)==-1){ printf("%s not exsit\n",dir); }else{ filefd = open(dir,O_RDWR); read(filefd,msg.secondbuf,sizeof(msg.secondbuf)); close(filefd); write(fd,&msg,sizeof(msg)); } break; case LLS: system("ls"); break; case LCD: dir = getdir(msg.data); chdir(dir); break; case QUIT: strcpy(msg.data,"quit"); write(fd,&msg,sizeof(msg)); close(fd); exit(-1); } return ret; } void handler_server_message(int c_fd,struct Msg msg) { int n_read; struct Msg msgget; int newfilefd; 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){ char *p = getdir(msg.data); newfilefd = open(p,O_RDWR|O_CREAT,0600); write(newfilefd,msgget.data,strlen(msgget.data)); putchar('>'); fflush(stdout); } else { printf("------------------------------------\n"); printf("\n%s\n",msgget.data); printf("------------------------------------\n"); putchar('>'); fflush(stdout); } } int main(int argc,char **argv) { int c_fd; struct sockaddr_in c_addr; struct Msg msg; memset(&c_addr,0,sizeof(struct sockaddr_in)); 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); if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr))== -1){ perror("connect"); exit(-1); } printf("connect ...\n"); int mark = 0; while(1){ memset(msg.data,0,sizeof(msg.data)); if(mark == 0) printf(">"); gets(msg.data); if(strlen(msg.data)==0){ if(mark == 1){ printf(">"); } continue; } mark =1; int ret = cmd_handler(msg,c_fd); if(ret > IFGO){ putchar('>'); fflush(stdout); continue; } if(ret ==-1){ printf("command not \n"); printf(">"); fflush(stdout); continue; } handler_server_message(c_fd,msg); } return 0; }