Remote Backup Server for socket programming in Linux

Source: Internet
Author: User

Write a remote data backup server using TCP or UDP protocol, and run the client to back up local files to the remote server.

Server Function: accepts client requests and backs up client files (which can be backed up to a specified folder ).

Client function: connect to the remote server and send local files to the remote backup server after the connection.

Development Environment: fedora13, Vim, GCC

Copyserver. c file

#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 32struct 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;        }elseprintf("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 file

#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 32struct 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;}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.