文件服务器

来源:互联网 发布:上海淘宝代理运营 编辑:程序博客网 时间:2024/05/20 17:40
服务器代码:#include #include #include #include #include #include #include #include #include #include #include #define err_log(errlog) do{perror(errlog); exit(1);}while(0)#define N 128int process_list(int sockfd){DIR * dir;struct dirent *dp;char buf[N] = {};if((dir = opendir(".")) == NULL){err_log("fail to opendir");}while((dp = readdir(dir)) != NULL){if(dp->d_name[0] == '.')continue;strcpy(buf, dp->d_name);send(sockfd, buf, N, 0);}printf("Transmit list done.\n");close(sockfd);return 0;}// G filenameint process_download(int sockfd, char *filename){int fd;char buf[N] = {};int nbyte;if((fd = open(filename, O_RDONLY)) < 0){strcpy(buf, "filename is not exist.\n");}else{strcpy(buf, "Y");}send(sockfd, buf, N, 0);if(buf[0] != 'Y'){printf("filename is not exist.\n");return -1;}while((nbyte = read(fd, buf, N)) > 0){send(sockfd, buf, nbyte, 0);usleep(50);}printf("Transmit file done.\n");close(fd);close(sockfd);return 0;}// P filenameint process_upload(int sockfd, char *filename){int fd;char buf[N] = {};int nbyte;if((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664)) < 0){err_log("fail to open");}while((nbyte = recv(sockfd, buf, N, 0)) > 0){write(fd, buf, nbyte);}printf("Receive file done.\n");close(fd);close(sockfd);return 0;}int main(int argc, const char *argv[]){struct sockaddr_in serveraddr, clientaddr;int sockfd;int acceptfd;char buf[N] = {};if(argc != 3){fprintf(stderr, "Usage:%s serverip port.\n", argv[0]);return -1;}if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("fail to socket");}serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("fail to bind");}if(listen(sockfd, 10) < 0){err_log("fail to listen");}socklen_t addrlen = sizeof(struct sockaddr);while(1){if((acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen))< 0){err_log("fail to accept");}// G filename // P filename// Lif(recv(acceptfd, buf, N, 0) < 0){err_log("fail to recv");}switch(buf[0]){case 'G':process_download(acceptfd, buf+2);break;case 'P':process_upload(acceptfd, buf+2);break;case 'L':process_list(acceptfd);break;default :printf("Invalid command.\n");}}return 0;}客户端代码:#include #include #include #include #include #include #include #include #include #define err_log(errlog) do{perror(errlog); exit(1);}while(0)#define N 128int process_help(void){printf("************************************************************\n");printf("** help : 帮助手册. *****\n");printf("** get filename: 下载文件 *****\n");printf("** put filename: 上传文件 *****\n");printf("** list : 打印服务器文件列表 *****\n");printf("** quit : 退出 *****\n");printf("************************************************************\n");return 0;}int process_list(struct sockaddr_in serveraddr){int sockfd;char buf[N] = {};if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("fail to socket");}if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("fail to connect");}// "L"strcpy(buf, "L");if(send(sockfd, buf, N, 0) < 0){err_log("fail to send");}while(recv(sockfd, buf, N, 0) > 0){printf("%s\n", buf);}printf("List done.\n");close(sockfd);return 0;}int process_get(char *filename,struct sockaddr_in serveraddr ){int sockfd;char buf[N] = {};int fd;int nbyte;if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("fail to socket");}if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("fail to connect");}// "G filename"sprintf(buf,"G %s", filename); if(send(sockfd, buf, N, 0) < 0){err_log("fail to send");}if(recv(sockfd, buf,N, 0) < 0){err_log("fail to recv");}if(strncmp(buf, "Y", 1) != 0){printf("filename is not exist.\n");close(sockfd);return -1;}else{printf("开始下载...\n");}if((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664)) < 0){err_log("fail to open");}// 接受网络信息, 写文件while((nbyte = recv(sockfd, buf, N, 0)) > 0){write(fd, buf, nbyte);}printf("Download done.\n");close(sockfd);close(fd);return 0;}int process_put(char *filename,struct sockaddr_in serveraddr ){int sockfd;char buf[N] = {};int fd;int nbyte;if((fd = open(filename, O_RDONLY, 0664)) < 0){err_log("fail to open");}if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("fail to socket");}if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("fail to connect");}// "P filename"sprintf(buf,"P %s", filename); if(send(sockfd, buf, N, 0) < 0){err_log("fail to send");}while((nbyte = read(fd, buf, N)) > 0){send(sockfd, buf, nbyte, 0);usleep(50);}printf("Upload done.\n");close(sockfd);close(fd);return 0;}int main(int argc, const char *argv[]){struct sockaddr_in serveraddr, clientaddr;int acceptfd;char buf[N] = {};if(argc != 3){fprintf(stderr, "Usage:%s serverip port.\n", argv[0]);return -1;}serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));socklen_t addrlen = sizeof(struct sockaddr);while(1){printf("");// get filename// put filename// list // helpfgets(buf, N, stdin);buf[strlen(buf)-1] = '\0';if(strncmp(buf,"get", 3) == 0){process_get(buf + 4, serveraddr);}else if(strncmp(buf, "put", 3) == 0){process_put(buf+4, serveraddr);}else if(strncmp(buf, "list", 4) == 0){process_list(serveraddr);}else if(strncmp(buf, "help", 4) == 0){process_help();}else if(strncmp(buf, "quit", 4) == 0){printf("Client exit..\n");break;}else{printf("Invalid input , Please help.\n");}}return 0;}
0 0
原创粉丝点击