Repository navigation
Expand file tree
/
Copy pathServer.cpp
More file actions
150 lines (103 loc) · 2.97 KB
/
Copy pathServer.cpp
File metadata and controls
150 lines (103 loc) · 2.97 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
146
147
148
149
#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.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 <cstring>
// concurrent server
void transact_with_client(char * backup_dir,int sock);
int main(int argc, char * argv[])
{
//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(argv[1],client_fd);
}
}
void transact_with_client(char * backup_dir, int sock)
{
char ch[100], direc[100];
int fd;
int n =read(sock,ch,sizeof(ch)); // Recieving "filename"
while(n!=0)
{
send(sock,"Handshaking Completed",21,0); //Send Confirmation
//**************************************************************
bzero(direc,sizeof(direc));
read(sock,direc,sizeof(direc)); //Reading folder name
send(sock,"Folder Recieved",15,0); //Send Confirmation
if (strcmp(ch,"filename")==0)
{
char fn[30];
bzero(fn,sizeof(fn));
n=read(sock,fn,sizeof(fn)); //fn = file name
cout << "File Name : " << fn << endl;
send(sock,"Received",8,0);
cout<<"-----------------------------------"<<endl;
string st = (char*)fn; // st contains file name
string ch = (char*)direc; // ch contains Folder name
string path = (char*)backup_dir;
string makedir=path+ch;
mkdir(makedir.c_str(),0777);
st=path+ch+"/"+st;
cout<<"full path of file to be backedup is : "<<st<<endl;
bzero(direc,sizeof(direc));
cout<<"-----------------------------------"<<endl;
strcpy(fn,st.c_str());
fd = open(fn,O_CREAT|O_WRONLY,0777);
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));
}
}