TCP檔案伺服器搭建
阿新 • • 發佈:2018-12-17
TCP 檔案伺服器實現功能 主要實現檔案的上傳,下載,檢視功能。
TCP檔案伺服器server.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define N 128 // ./a.out 192.168.1.57 8000 // argv[0] argv[1] aegv[2] int process_list(int newsockfd,char *buf) { DIR * dirp; int ret; struct dirent * direntp; dirp = opendir("./"); if(dirp == NULL) { perror("opendir"); exit(-1); } while( (direntp = readdir(dirp)) != NULL) { if(strncmp(direntp->d_name,".",1) == 0 ) continue; strcpy(buf,direntp->d_name); ret = write(newsockfd,buf,N); if(ret < 0) { perror("write"); exit(-1); } } close(newsockfd); return 0; } int process_get(int newsockfd,char *buf) { int ret,fd,nbytes; fd = open(buf+4,O_RDWR); if(fd < 0) { perror("open"); exit(-1); } while( (nbytes= read(fd,buf,N)) > 0) { ret = write(newsockfd,buf,nbytes); if(ret < 0) { perror("write"); exit(-1); } } close(newsockfd); return 0; } int process_put(int newsockfd,char *buf) { int ret,fd,nbytes; fd = open(buf+4,O_RDWR|O_CREAT|O_TRUNC,0664); if(fd < 0) { perror("open"); exit(-1); } while( (nbytes= read(newsockfd,buf,N)) > 0) { ret = write(fd,buf,nbytes); if(ret < 0) { perror("write"); exit(-1); } } close(newsockfd); close(fd); return 0; } int main(int argc, const char *argv[]) { char buf[N] ={0}; int sockfd,ret,newsockfd; struct sockaddr_in myaddr,client; if(argc != 3) { printf("執行程式時帶入ip 和 port\n"); exit(-1); } socklen_t addrlen = sizeof(client); sockfd = socket(AF_INET,SOCK_STREAM,0); if(sockfd < 0) { perror("socket"); exit(-1); } printf("sockfd=%d\n",sockfd); memset(&myaddr,0,sizeof(myaddr)); myaddr.sin_family = AF_INET ; myaddr.sin_port = htons(atoi(argv[2])); myaddr.sin_addr.s_addr = inet_addr(argv[1]); ret = bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)) ; // 給socket 一個固定的ip 和埠 if(ret < 0) { perror("bind"); exit(-1); } ret = listen(sockfd,10); if(ret < 0) { perror("listen"); exit(-1); } while(1) { newsockfd = accept(sockfd,(struct sockaddr *)&client,&addrlen) ; if(newsockfd < 0) { printf("accept"); exit(-1); } printf("newsockfd = %d\n",newsockfd); ret = read(newsockfd,buf,N); if(ret < 0) { printf("read"); exit(-1); } printf("buf=%s\n",buf); if( strncmp(buf,"list",4) == 0 ) { process_list(newsockfd,buf); } else if( strncmp(buf,"get",3) == 0 ) { process_get(newsockfd,buf); } else if( strncmp(buf,"put",3) == 0 ) { process_put(newsockfd,buf); } } return 0; }
TCP檔案伺服器client.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define N 128 // ./client 192.168.1.57 8000 // list // get file // put file // help int process_list(struct sockaddr_in *addr,char * buf) { int ret,sockfd,nbytes; sockfd = socket(AF_INET,SOCK_STREAM,0); if(sockfd < 0) { perror("socket"); exit(-1); } ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ; if(ret < 0) { perror("connect"); exit(-1); } ret = write(sockfd,buf,N); if(ret < 0) { perror("write"); exit(-1); } while( (nbytes= read(sockfd,buf,N)) > 0) { printf("%s\n",buf); } close(sockfd); return 0; } int process_get(struct sockaddr_in *addr,char * buf) { int ret,sockfd,nbytes,fd; sockfd = socket(AF_INET,SOCK_STREAM,0); if(sockfd < 0) { perror("socket"); exit(-1); } ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ; if(ret < 0) { perror("connect"); exit(-1); } ret = write(sockfd,buf,N); if(ret < 0) { perror("write"); exit(-1); } fd = open(buf+4,O_RDWR|O_CREAT|O_TRUNC,0664); if(fd < 0) { perror("open"); exit(-1); } while( (nbytes= read(sockfd,buf,N)) > 0) { ret = write(fd,buf,nbytes); if(ret < 0) { perror("write"); exit(-1); } } close(sockfd); return 0; } int process_put(struct sockaddr_in *addr,char * buf) { int ret,sockfd,nbytes,fd; sockfd = socket(AF_INET,SOCK_STREAM,0); if(sockfd < 0) { perror("socket"); exit(-1); } ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ; if(ret < 0) { perror("connect"); exit(-1); } ret = write(sockfd,buf,N); if(ret < 0) { perror("write"); exit(-1); } fd = open(buf+4,O_RDWR); if(fd < 0) { perror("open"); exit(-1); } while( (nbytes= read(fd,buf,N)) > 0) { ret = write(sockfd,buf,nbytes); if(ret < 0) { perror("write"); exit(-1); } } close(sockfd); close(fd); return 0; } int main(int argc, const char *argv[]) { char buf[N] = {0}; int sockfd,ret; struct sockaddr_in server,client; if(argc != 3) { printf("執行程式時帶入ip 和 port\n"); exit(-1); } memset(&server,0,sizeof(server)); server.sin_family = AF_INET ; server.sin_port = htons( atoi(argv[2]) ); server.sin_addr.s_addr = inet_addr(argv[1]); while(1) { printf(">:"); fgets(buf,N,stdin); buf[strlen(buf)-1] = '\0'; if( strncmp(buf,"list",4) == 0 ) { process_list(&server,buf); } else if( strncmp(buf,"get",3) == 0 ) { process_get(&server,buf); } else if( strncmp(buf,"put",3) == 0 ) { process_put(&server,buf); } else if( strncmp(buf,"quit",4) == 0 ) { break; } else if( strncmp(buf,"exit",4) == 0 ) { break; } else { printf("命令錯誤,請重新輸入\n"); } } #if 0 printf(">:"); fgets(buf,N,stdin); ret = write(sockfd,buf,N) ; if(ret < 0) { perror("write"); exit(-1); } ret = read(sockfd,buf,N); if(ret < 0) { printf("read"); exit(-1); } printf("%s\n",buf); close(sockfd); #endif return 0; }
執行client輸入list實現檢視功能 輸入get+檔名實現下載 輸入put+檔名實現上傳