-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverB.cpp
More file actions
115 lines (98 loc) · 2.85 KB
/
serverB.cpp
File metadata and controls
115 lines (98 loc) · 2.85 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#define ServerA_UDP_PORT 21947
using namespace std;
int udp_sockfd;
struct sockaddr_in udp_server_addr, udp_client_addr;
char recv_buff[1024];
string resultData,sta_request;
//Create a UDP socket
void create_udp_socket()
{
udp_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
//test if create a socket successfully
if(udp_sockfd == -1){
perror("ServerA UDP socket");
exit(1);
}
memset(&udp_server_addr, 0, sizeof(udp_server_addr)); // make sure the struct is empty
udp_server_addr.sin_family = AF_INET;
udp_server_addr.sin_port = htons(ServerA_UDP_PORT); //port
udp_server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //set IP
//test if bind a socket successfully
if( bind(udp_sockfd, (struct sockaddr*) &udp_server_addr, sizeof(udp_server_addr)) == -1){
perror("ServerA UDP bind");
exit(1);
}
socklen_t udp_length = sizeof(udp_client_addr);
getsockname(udp_sockfd,(struct sockaddr*) &udp_client_addr , &udp_length);
int portnum = ntohs(udp_client_addr.sin_port);
printf("The Server A is up and running using UDP on port <%d>.\n",portnum);
}
void search (string target)
{
ifstream inFile("database_a.csv",ios::in);
string lineStr;
vector< vector<string> > strArray;
// save file in a vector
while (getline(inFile,lineStr))
{
stringstream ss(lineStr);
string str;
vector<string> lineArray;
while(getline(ss,str,','))
{
lineArray.push_back(str);
}
strArray.push_back(lineArray);
}
//search
sta_request = "0";
resultData = "";
for(int i = 0 ; i < strArray.size(); i++)
{
if(target == strArray[i][0])
{
sta_request = "1";
resultData = "," + strArray[i][0] + "," + strArray[i][1] + "," + strArray[i][2] + "," + strArray[i][3] + "," + strArray[i][4];
}
}
//print the result
printf("The server A has found <%s> match\n",sta_request.c_str());
}
int main()
{
create_udp_socket();
while(1)
{
socklen_t serverA_udp_len = sizeof(udp_client_addr);
if (recvfrom (udp_sockfd,recv_buff,1024,0,(struct sockaddr *) &udp_client_addr,&serverA_udp_len) == -1){
perror("serverA receive failed");
exit(1);
}
printf("The Server A received input <%s>\n", recv_buff);
search(recv_buff);
string message = sta_request + resultData;
if(-1 ==sendto(udp_sockfd,message.c_str(),1024,0,(struct sockaddr *) &udp_client_addr,sizeof(udp_client_addr)))
{
perror("serverA response failed");
exit(1);
}
printf("The Server A finished sending the output to AWS\n");
}
close(udp_sockfd);
return 0;
}