-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
364 lines (335 loc) · 7.41 KB
/
server.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <vector>
#include <sys/dir.h>
#include <sys/param.h>
#include <fstream>
using namespace std;
string get_data_string(string file_name) //takes name of file as the input and returns a string that contains all the text
{
ifstream file(file_name.c_str());
string word;
char x ;
word.clear();
int count = 0;
while ((x = file.get())!= EOF)
{
word = word + x;
}
file.close();
return word;
}
vector<string> ls()
{
int count,i;
struct dirent **files;
int file_select(const struct dirent*);
char pathname[MAXPATHLEN];
if (getcwd(pathname, sizeof(pathname)) == NULL)
{
cout<<"Error getting path"<<endl;
}
count = scandir(pathname, &files, file_select, alphasort);
vector<string> filename(count);
if (count <= 0)
{
cout<<"No files in this directory"<<endl;
}
else
{
for (i=1;i<count+1;++i)
{
cout<<files[i-1]->d_name<<" ";
filename.at(i-1) = files[i-1]->d_name;
}
}
return filename;
}
int file_select(const struct dirent *entry)
{
if ((entry->d_name == ".") ||(entry->d_name == "..")){
return (0);
}
else
return (1);
}
string strtrim(string sentence)
{
int i = 0;
sentence = sentence.c_str();
int j = sentence.size()-1;
char x;
while(i < j)
{
if(sentence[0] != ' ')
{
i = 0;
break;
}
else if((sentence[i] == ' ') && (sentence[i+1] != ' '))
{
i = i+1;
break;
}
else
{
i++;
}
}
while(j>0)
{
if(sentence[sentence.size()-1] != ' ') break;
else if((sentence[j] == ' ') && (sentence[j-1]!=' '))
{
j = j-1;
break;
}
else j--;
}
string trm_str;
trm_str.clear();
for(int k = i; k<=j; k++)
{
x = sentence[k];
trm_str = trm_str + x;
}
return trm_str;
}
vector<int> get_white_spaces(string sentence)
{
vector<int> space_index(100);
char x;
int i = 0;
int j,k;
k = 0;
int diff = 0;
space_index.at(k) = 0;
k++;
while(i<sentence.size())
{
if(sentence[i] == ' ')
{
space_index.at(k) = i-1;
k++;
j = i;
while(j<sentence.size()-1)
{
if((sentence[j]==' ')&&(sentence[j+1]!=' '))
{
space_index.at(k) = j+1;
k++;
break;
}
else j++;
}
diff = j-i;
i = j+1;
}
else i++;
}
space_index.at(k) = sentence.size()-1;
return space_index;
}
vector<string> tokenizer(string sentence)
{
int p;
sentence = strtrim(sentence);
vector<string> tokens (100);
vector<int> index_vector = get_white_spaces(sentence);
int pre,post,length,i;
i= 0;
length = 0;
string temp;
while(i < index_vector.size())
{
if((i ==0)||(index_vector.at(i) !=0))
{
temp.clear();
pre = index_vector.at(i);
post = index_vector.at(i+1);
for(int j = pre;j<=post;j++)
{
temp = temp + sentence[j];
}
tokens.at(length) = temp;
length++;
}
i = i+2;
}
return tokens;
}
void str2char(string data, char* destination){
int size = data.size();
char strarray[size];
for(int i = 0;i < size;i++){
destination[i] = (char)data[i];
}
}
int send(int file_descriptor,string message,int count){
char buffer[1024];
str2char(message,buffer);
int n = -1;
n = write(file_descriptor,buffer,count);
if(n<0){
cout<<"Error writing to port"<<endl;
}
return n;
}
string recieve(int file_descriptor, int count){
char buffer[2*count];
bzero(buffer,2*count);
int n = read(file_descriptor,buffer,count);
if(n<0){
cout<<"ERROR reading from socket"<<endl;
}
string stri(buffer);
return stri;
}
void map_command(vector <string> command_args,int file_descriptor){
char buffer[1024];
int n = -1;
string reply;
vector<string> file_name;
if(command_args.at(0) == "ls"){
file_name = ls();
string msg = "";
for(int i=0;i<file_name.size();i++){
msg = msg + " " + file_name.at(i);
}
str2char(msg,buffer);
n = send(file_descriptor,buffer,msg.size());
if(n<0){
cout<<"Error sending file description"<<endl;
}
}
else if(command_args.at(0) == "get"){
file_name = ls();
int flag = 0;
for(int i =0;i<file_name.size();i++){
if(command_args[1] == file_name.at(i)){
flag = 1;
}
}
if(flag==0){
cout<<"No such files found"<<endl;
}
else{
string data = get_data_string(command_args.at(1));
cout<<data<<endl;
char temp_buffer [data.size()+10];
str2char(data,temp_buffer);
n = send(file_descriptor,temp_buffer,data.size());
if(n<0){
cout<<"error writing to ports"<<endl;
}
else{
cout<<"file sent successfully"<<endl;
}
}
}
else if(command_args.at(0) == "put"){
string incoming_file_name = command_args.at(1);
ofstream file;
file.open(incoming_file_name.c_str());
string content = recieve(file_descriptor,1024);
file << content;
file.close();
}
}
void ftpmode(int file_descriptor){
char array [1024];
string command;
while(1){
command = recieve(file_descriptor,256);
cout<<command<<" :exact command is: "<<endl;
vector<string> command_args = tokenizer(strtrim(command));
if(command_args.size() > 0){
cout<<"command from client is : "<<command_args.at(0)<<command_args.at(1)<<endl;
map_command(command_args,file_descriptor);
}
else{
send(file_descriptor,"invalid command",20);
}
}
}
void turn_on_server(string server_type){
int domain = AF_INET;
int type = SOCK_STREAM;
int protocol = 0;
int status = 0;
int file_descriptor = 0,new_file_descriptor =0;
int port_number = 5010;
struct sockaddr_in server_socket, client_socket;
socklen_t length_client;
int pid;
string buffer;
bzero((char *) &server_socket, sizeof(server_socket));
bzero((char *) &client_socket, sizeof(client_socket));
//initialising socket stucture
server_socket.sin_family = AF_INET;
server_socket.sin_addr.s_addr = inet_addr("127.1.0.1");
server_socket.sin_port = htons(port_number);
length_client = sizeof(client_socket);
cout<<"Welcome"<<server_socket.sin_addr.s_addr<<" "<<server_socket.sin_port<<endl;
file_descriptor = socket(domain,type,protocol);
if(file_descriptor < 0){
cout<<"Error, failed to create sockets"<<endl;
}
else{
status = bind(file_descriptor, (struct sockaddr *)&server_socket, sizeof(server_socket));
if(status < 0){
cout<<"Error, failed to bind to address"<<endl;
}
else{
status = listen(file_descriptor, 10);
if(status != 0){
cout<<"Error, failed to get to listen mode"<<endl;
}
else{
cout<<"Server has started....Please make requests to connect."<<endl;
while(1){
new_file_descriptor = accept(file_descriptor, (struct sockaddr *)&client_socket, &length_client);
if(file_descriptor<0){
cout<<"Error, failed to create dedicated sockets for client"<<endl;
}
else{
cout<<"HEY! CONNECTED SUCCESSFULLY"<<endl;
cout<<client_socket.sin_addr.s_addr<<endl;
pid = fork();
if(pid<0){
cout<<"Unable to create new process for new client..."<<endl;
}
if(pid==0){
close(file_descriptor);
if(server_type == "echo_server"){
getline(cin, buffer);
int n = send(new_file_descriptor,buffer,buffer.size());
if(n<0){
cout<<"Error sending the message"<<endl;
}
string reply = recieve(new_file_descriptor,256);
cout<<reply<<endl;
}
else if(server_type == "ftp_server"){
ftpmode(new_file_descriptor);
break;
}
}
else{
close(new_file_descriptor);
}
}
}
}
}
}
}
int main(){
//turn_on_server("echo_server");
turn_on_server("ftp_server");
}