Repository navigation
Expand file tree
/
Copy pathdhtserver3.cpp
More file actions
393 lines (344 loc) · 9.13 KB
/
Copy pathdhtserver3.cpp
File metadata and controls
393 lines (344 loc) · 9.13 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// ************************************************
// filename: dhtserver3.cpp
//
// creator:Sangamesh Patil
//
// Description: This is a server to contact other servers on TCP protocol and accept request from client.
// It is inspired by earlier implementation of TCP classes done by myself as software engineer
//
// This computer program is protected.
// Recipient is to retain the program in confidence, and
// is not permitted to copy, use, distribute, modify or
// translate the program without authorization from me.
//
// History
//
// date name description
// 09Mar2015 Sangamesh Patil initial creation and file reading
// 25Mar2015 Sangamesh Patil socket server and TCP Client implementation
// 17Apr2015 Sangamesh Patil Final Fix
// ***************************************************
#include <cstdlib>
#include<list>
#include<string>
#include <string.h>
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<fstream>
#include <errno.h>
#include <netdb.h>
#include <vector>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define TRUE 0;
#define TCP 23851
#define FALSE 1;
using namespace std;
std ::ifstream input;
string key[20],value[20];
// define the hostname
const string HOSTNAME = "nunki.usc.edu";
// *********************************
//class for tcp data transfer
// *********************************
class tcp_data
{
private:
int bufferSize;
public:
int dataSocket;
string recvString;
tcp_data(int);
int recvData();
int sendData(string);
void closeDataSocket();
void showErr(const char *);
char peerSocketIP[32];
int peerSocketPort;
char peerUserName[32];
};
// *********************************
// function to show error
// *********************************
void showErr(const char *err)
{
perror(err);
exit(1);
}
// *********************************
// class for tcp server listen
// *********************************
class tcp_server
{
private:
string hostname;
int portNum;
struct sockaddr_in server_address, client_address;
public:
int listenSocket;
tcp_server(string, int);
void closeListenSocket();
int acceptClient();
int startListen();
void showErr(const char *);
};
// *********************************
// Implement of tcp data class
// *********************************
// *********************************
//function to set buffer size
// *********************************
tcp_data::tcp_data(int sock)
{
this->bufferSize = 128;
this->dataSocket = sock;
}
// *********************************
// function to recieve data from server2.
// *********************************
int tcp_data::recvData()
{
int n;
int recvLength = 0;
n = read(this->dataSocket, &recvLength, sizeof(recvLength));
recvLength = ntohl(recvLength);
//cout << "recvLength is " << recvLength << endl;
char buffer[this->bufferSize];
memset(buffer, 0, this->bufferSize);
n = read(this->dataSocket, buffer, recvLength);
//cout << "receive length from buffer: " << n << endl;
recvString = buffer;
printf("The Server3 has received a request with %s from the Server2 with port number %d and IP address %s. \n",recvString.c_str(),this->peerSocketPort,this->peerSocketIP);
return n;
}
// *********************************
// function to send data to server2.
// *********************************
int tcp_data::sendData(string str)
{
int sendLength = htonl(str.size());
if ((write(this->dataSocket, &sendLength, sizeof(sendLength))) < 0)
{
this->showErr("Send Error");
return -1;
}
char buffer[this->bufferSize];
memset(buffer, 0, this->bufferSize);
strncpy(buffer, str.c_str(), sizeof(str));
buffer[sizeof(buffer) - 1] = 0;
if ((write(this->dataSocket, str.c_str(), str.size())) < 0)
{
this->showErr("Send Error");
return -1;
}
printf("The Server3 sends the reply %s to Server2 with port number %d and IP address %s. \n",str.c_str(),this->peerSocketPort,this->peerSocketIP);
return 1;
}
// *********************************
// function to close the socket
// *********************************
void tcp_data::closeDataSocket()
{
close(this->dataSocket);
}
// *********************************
// function to show error
// *********************************
void tcp_data::showErr(const char *err)
{
perror(err);
exit(1);
}
// **********************************
// Implement of tcp server class
// **********************************
// *********************************
// function to create server
// *********************************
tcp_server::tcp_server(string s, int p)
{
this->listenSocket = -1;
this->hostname = s;
this->portNum = p;
}
// *********************************
// function to start listening
// *********************************
int tcp_server::startListen()
{
struct hostent *hostInfo;
hostInfo = gethostbyname(this->hostname.c_str());
this->listenSocket = socket(AF_INET, SOCK_STREAM, 0);
memset((char*) &server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
memcpy(&server_address.sin_addr, hostInfo->h_addr, hostInfo->h_length);
//server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(this->portNum);
if (bind(this->listenSocket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
{
this->showErr("Bind Error");
return -1;
}
if (listen(this->listenSocket, 5) < 0)
{
this->showErr("Listen Error");
return -1;
}
// OUTPUT
printf("The Server3 has TCP port number has port number %d and IP address %s.\n", this->portNum, inet_ntoa(server_address.sin_addr));
return 1;
}
// *********************************
// function to close the socket.
// *********************************
void tcp_server::closeListenSocket()
{
close(this->listenSocket);
}
// *********************************
// function to accept client
// *********************************
int tcp_server::acceptClient()
{
int r;
socklen_t client_addr_length = sizeof(client_address);
r = accept(listenSocket, (struct sockaddr *) &client_address, &client_addr_length);
return r;
}
// *********************************
// function to show error.
// *********************************
void tcp_server::showErr(const char *err)
{
perror(err);
exit(1);
}
// ***********************************
// read the file and store the values in the string
// *************************************
void fileread()
{
if (input.is_open())
{
for(int i=0;i<20;i++)
{
input>>key[i]>>value[i];
}
}
}
// *********************************
// main
// *********************************
main()
{
input.open("server3.txt");
fileread();
tcp_server server(HOSTNAME, TCP);
server.startListen();
fd_set all_fd;
fd_set read_fd;
int fdmax;
FD_ZERO(&all_fd);
FD_ZERO(&read_fd);
// add listen socket into all_fd
FD_SET(server.listenSocket, &all_fd);
fdmax = server.listenSocket;
int socketReady;
int socketAccept;
int recvResult;
string receive;
string foundvalue,val;
string indexChoice;
// save all the data socket
vector<tcp_data> savedDataSocket;
vector<tcp_data>::iterator cuerrentSocket;
while(1)
{
bool foundkey=false;
// make read_fd as all_fd then select active socket
read_fd = all_fd;
socketReady = select(fdmax+1, &read_fd, NULL, NULL, NULL);
if (socketReady == -1)
{
showErr("select error");
}
// go through all sockets less than fdmax+1
for (int i = 0; i <= fdmax; ++i)
{
// find a active socket
if (FD_ISSET(i, &read_fd))
{
// accept a new connection if the listen socket active
if (i == server.listenSocket)
{
socketAccept = server.acceptClient();
if (socketAccept > fdmax)
{
fdmax = socketAccept;
}
// add new connection socket to all_fd
FD_SET(socketAccept, &all_fd);
// add new socket to vector
tcp_data temp(socketAccept);
// get the peer socket IP and port
struct sockaddr_in peerInfo;
socklen_t peerInfoLength = sizeof(peerInfo);
getpeername(socketAccept, (struct sockaddr*) &peerInfo, &peerInfoLength);
strcpy(temp.peerSocketIP, inet_ntoa(peerInfo.sin_addr));
temp.peerSocketPort = ntohs(peerInfo.sin_port);
savedDataSocket.push_back(temp);
}
else
{
// find the saved socket, save the iterator to the savedDataSocket
for (cuerrentSocket = savedDataSocket.begin(); cuerrentSocket != savedDataSocket.end(); cuerrentSocket++)
{
if (i == cuerrentSocket->dataSocket)
{
break;
}
}
int recvResult = cuerrentSocket->recvData();
// the client close the connection
if (recvResult <= 0)
{
// peer close the connection
cuerrentSocket->closeDataSocket();
FD_CLR(i, &all_fd);
// delete the socket from the vector
savedDataSocket.erase(cuerrentSocket);
}
else
{
// get the key that client sent
receive = cuerrentSocket->recvString;
receive=receive.erase(0,4);
for(int i=0;i<20;i++)
{
//cout<<key[i];
if(key[i]==receive)
{
foundkey=true;
foundvalue=value[i];
}
}
if(foundkey==true)
{
//cout<<foundvalue;
val="POST "+foundvalue;
cuerrentSocket->sendData(val);
}
}
}
}
}
}
server.closeListenSocket();
input.close();
return 0;
}