AI智能
改变未来

ftp服务器


一 FTP服务器

服务器代码

#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 \"config.h\"#include <unistd.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 Msg msg;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));if(argc != 3){printf(\"param error\\n\");exit(-1);}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 len = sizeof(struct sockaddr_in);while(1){c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&len);if(c_fd == -1 ){perror(\"accept\");exit(-1);}else{printf(\"connect sucess : %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(s_fd);close(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 <string.h>#include \"config.h\"#include <unistd.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(!strcmp(\"lls\",cmd))          return LLS;if(strstr(cmd,\"cd\")!=NULL)      return CD;if(strstr(cmd,\"lcd\")!=NULL)     return LCD;if(strstr(cmd,\"get\")!=NULL)     return GET;if(strstr(cmd,\"put\")!=NULL)     return PUT;return -1;}char *getDir(char *cmsg){char*p;p = strtok(cmsg,\" \");p = strtok(NULL,\" \");return p;}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 is 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(\"sever 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(\"---------------------------\");printf(\"\\n%s\\n\",msgget.data);printf(\"---------------------------\");putchar(\'>\');fflush(stdout);}}int main(int argc , char **argv){int c_fd;int n_read;struct Msg msg;struct sockaddr_in c_addr;memset(&c_addr,0,sizeof(struct sockaddr_in));if(argc != 3){printf(\"param error\\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_in)) == -1){                          perror(\"connect : \");exit(-1);}elseprintf(\"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;}

config.h代码

#define  LS     0#define  GET    1#define  PWD    2#define  IFGO   3#define  LCD    4#define  LLS    5#define  CD 6#define  PUT    7#define  QUIT   8#define  DOFILE 9struct Msg{int type;char data[1024];char secondBuf[128];};
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ftp服务器