分别采用TCP或UDP协议编写一个远程数据备份服务器,运行客户端将本地文件备份到远程的服务器中。
服务器的功能:接受客户端请求,把客户端的文件进行备份(可以备份到指定的文件夹)。
客户端的功能:与远程服务器进行连接,在连接后把本地的文件发送给远程备份服务器。
开发环境:fedora13,vim,gcc
copyserver.c文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<time.h> #include<netdb.h> #include<stdlib.h> #include<string.h> #define PORT 1234 #define BACKLOG 5 #define MAXSIZE 32 struct user { char name[20]; char passwd[20]; }u,u1; int main() { int listenfd, connectfd,num; struct sockaddr_in server; struct sockaddr_in client; socklen_t addrlen; char buf[MAXSIZE]; char dir[50]; // u.name={"d","a","i","z","h","e"}; // u.passwd={"1","1","1","1","1","1"}; FILE * fd; if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket() error."); return 0; } int opt = SO_REUSEADDR; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1) { perror("bind() error."); return 0; } if(listen(listenfd, BACKLOG) == -1) { perror("listen() error."); return 0; } addrlen = sizeof(client); if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen )) == -1) { perror("accept() error."); return 0; } else printf("It's Connected\n"); // recv(connectfd,u1.name,20,0); // recv(connectfd,u1.passwd,20,0); // printf("%s",u1.name); printf("Please input directory of copy\n"); scanf("%s",dir); fd=fopen(dir,"wb"); while(num=recv(connectfd,buf,1,0)>0) { fwrite(buf,1,num,fd); } fclose(fd); close(connectfd); return 1; } |
copyclient.c文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #define PORT 1234 /*listen port*/ #define MAXSIZE 32 struct user { char name[20]; char passwd[20]; }u; int main ( int argc, char *argv[]) { int sockfd,num; struct hostent *he; struct sockaddr_in server; char buf[MAXSIZE]; char dir[50]; FILE *fd; if(argc!=2) { printf("usage %s<ip address>\n",argv[0]); return 0; } /*get ip address*/ if((he = gethostbyname(argv[1])) == NULL) { printf("gethostbyname error\n"); return 0; } if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("socket() error \n"); return 0; } bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr); if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1) { printf("connetc() error\n"); return 0; } printf("connect success.\n"); printf("Please input your name:"); // scanf("%s",u.name); // printf("Please input your password:"); // scanf("%s",u.passwd); // send(sockfd,u.name,20,0); // send(sockfd,u.passwd,20,0); printf("Please input file's directory\n"); scanf("%s",dir); fd = fopen(dir,"rb"); while((num=fread(buf,1,MAXSIZE,fd))>0) { send(sockfd,buf,num,0); } fclose(fd); close(sockfd); return 1; } |
发表评论
要发表评论,您必须先登录。