分别采用TCP或UDP协议编写一个远程数据备份服务器,运行客户端将本地文件备份到远程的服务器中。
服务器的功能:接受客户端请求,把客户端的文件进行备份(可以备份到指定的文件夹)。
客户端的功能:与远程服务器进行连接,在连接后把本地的文件发送给远程备份服务器。
开发环境:fedora13,vim,gcc
copyserver.c文件
#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文件
#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;
}
运行结果:
今天的文章linux下socket编程之远程备份服务器分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/6515.html