Repository navigation
Expand file tree
/
Copy pathversionbackupserver.cpp
More file actions
146 lines (111 loc) · 2.99 KB
/
Copy pathversionbackupserver.cpp
File metadata and controls
146 lines (111 loc) · 2.99 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wait.h>
#include <signal.h>
#include <fcntl.h>
#include <sstream>
using namespace std;
// concurrent server
void transact_with_client(int sock);
int main()
{
//socket creation
int serv_fd = socket(AF_INET,SOCK_STREAM,0);
if (serv_fd == -1)
{
perror("socket creation error");
exit(1);
}
//initialize the sockaddr_in structure
struct sockaddr_in sock_addr_serv;// sockaddr_in for IPV4
sock_addr_serv.sin_family = AF_INET;
sock_addr_serv.sin_port = 9888;
sock_addr_serv.sin_addr.s_addr = inet_addr("127.0.0.1");
//bind the socket with ip address and port
if (bind(serv_fd,(struct sockaddr *)&sock_addr_serv,sizeof(sock_addr_serv))==-1)
{
perror("bind error");
exit(1);
}
// listening (waiting) for client request
if (listen(serv_fd,5)==-1)
{
perror("listen error");
exit(1);
}
cout << "server waiting for client to connect" << endl;
//accepting client request
struct sockaddr_in sock_addr_cli;
socklen_t cli_len = sizeof(sock_addr_cli);
int client_fd=accept(serv_fd,(struct sockaddr *)&sock_addr_cli,&cli_len);
if (client_fd==-1)
{
perror(" accept error");
exit(1);
}
while (1)
{
transact_with_client(client_fd);
}
}
void transact_with_client(int sock)
{
char ch[100], direc[100];
int fd;
int n =read(sock,ch,sizeof(ch)); // rec- "filename"
while(n!=0)
{
send(sock,"received",8,0); //send confirmation
//**************************************************************
bzero(direc,sizeof(direc));
read(sock,direc,sizeof(direc)); //read folder name
cout<<"rec directory name : "<<direc<<endl;
send(sock,"dir rec",7,0); //send msg
//****************************************************
if (strcmp(ch,"filename")==0)
{
char fn[30];
bzero(fn,sizeof(fn));
n=read(sock,fn,sizeof(fn)); //fn = file name
cout << "fn " << fn << endl;
send(sock,"received",8,0);
cout<<"****************************************"<<endl;
string st = (char*)fn;
string ch = (char*)direc;
time_t a,b;
b=time(&a);
cout<<b<<endl;
std::stringstream ss;
ss << a;
std::string ts=ss.str();
cout<<"string value of time"<<ts <<endl;
cout<<"ch : "<<ch<<":: "<<st<<endl;
cout<<"****************************************"<<endl;
st="/home/rc/bhargavi/masterbackup/"+ch+"/"+st+ts;
cout<<"full st is : "<<st<<endl;
cout<<"****************************************"<<endl;
bzero(direc,sizeof(direc));
cout<<"****************************************"<<endl;
strcpy(fn,st.c_str());
fd = open(fn,O_CREAT|O_WRONLY,0660);
bzero(fn,sizeof(fn));
}
else if (strcmp(ch,"end")==0)
{
close(fd);
}
else
{
write(fd,ch,n);
bzero(ch,sizeof(ch));
}
n =read(sock,ch,sizeof(ch));
}
}