Repository navigation
Expand file tree
/
Copy pathdbServer.cpp
More file actions
126 lines (98 loc) · 2.97 KB
/
Copy pathdbServer.cpp
File metadata and controls
126 lines (98 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
#define MYPORT 23153
using namespace std;
int main(){
int client_fd;
int ID;
int len, sin_size;
struct sockaddr_in main_addr;
struct sockaddr_in db_addr;
char message[1024] = {0};
string buff;
memset(&main_addr,0,sizeof(main_addr));
main_addr.sin_family=AF_INET;
main_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
main_addr.sin_port=htons(22153);
db_addr.sin_family = AF_INET;
db_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
db_addr.sin_port = htons(23153);
client_fd = socket(AF_INET,SOCK_DGRAM,0);
if (client_fd == -1) {
perror("socket error\n");
return 1;
}
sin_size = sizeof(main_addr);
if(bind(client_fd,(struct sockaddr *)&db_addr,sizeof(main_addr))<0)
{
perror("bind error\n");
return 1;
}
while(1){
cout << "The Database Server is up and running" << endl;
buff = "No";
strcpy(message, buff.c_str());
recvfrom(client_fd, message, 1024, MSG_WAITALL, (struct sockaddr*)&main_addr, (socklen_t*)&sin_size);
cout << "Receive request from Main Server." << endl;
ID = atoi(message);
//k is the total lines of the txt file
int k=0;
string line;
ifstream original("database.txt");
for (k=0;getline(original, line);++k);
original.close();
int code[100];
int generator[100];
int generator1[100];
int generator2[100];
ifstream input;
int buf;
input.open("database.txt");
int j = 0;
int counter=0;
string oneLine;
for(int i=0; i<k; i++){
getline(input,oneLine);
stringstream ss;
ss.str(oneLine);
ss>>code[counter]>>generator[counter]>>generator1[counter]>>generator2[counter];
if(code[counter] == ID)
{
buff="Yes";
break;
}
counter=counter+1;
}
input.close();
if (buff=="No"){
cout << "No match found" << endl;
buff = "Recieve no match found";
strcpy(message, buff.c_str());
}
else{
cout << "Send link " << code[counter] << ", capacity " << generator[counter] << "Mbps, link length" << generator1[counter] << "km, propagation velocity " << generator2[counter] << "km/s." << endl;
buff = to_string(generator[counter])+ " " + to_string(generator1[counter]) + " " + to_string(generator2[counter]);
strcpy(message, buff.c_str());
}
sendto(client_fd, message, 1024, 0, (struct sockaddr*)&main_addr, sin_size);
buff="restart";
}
close(client_fd);
return 0;
}