-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_and.c
139 lines (111 loc) · 3.42 KB
/
server_and.c
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<errno.h>
#include<string.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdbool.h>
int main() {
int sock, length, n;
struct sockaddr_in serverAddress, clientAddress;
char buf[40];
bzero(buf, 40);
bool printCompFlag = true;
int numOfAndOperations = 0;
bool isFirstTransmision = true;
char sizeOfAndOperationsBuffer[10];
int numOfAndOperationsFromBuffer = 0;
bzero(sizeOfAndOperationsBuffer, 10);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) {
perror("Socket");
exit(-1);
}
length = sizeof(serverAddress);
bzero(&serverAddress, length);
// init adddress
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(22355);
inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr);
bzero(&serverAddress.sin_zero, 8);
if(bind(sock, (struct sockaddr *)&serverAddress, length) < 0) {
perror("Binding");
exit(-1);
}
printf("The Server AND is up and running using UDP on port 22355\n");
unsigned int fromLength = sizeof(clientAddress);
while(true) {
bzero(buf, 40);
n = recvfrom(sock, buf, 40, 0, (struct sockaddr *)&clientAddress, &fromLength);
if(n < 0) {
//perror("Received from");
} else {
// splitting string based on , here
if (printCompFlag) {
printf("The server AND started receiving lines from the edge server for AND computation. The computation results are:\n");
printCompFlag = false;
}
int j=0;
char buffer[40];
bzero(buffer, 40);
strncpy(buffer, buf, strlen(buf));
buffer[strlen(buf)] = '\0';
// checking if it's the first reception, so as to get the count
if (isFirstTransmision) {
strncpy(sizeOfAndOperationsBuffer, buffer, strlen(buffer));
numOfAndOperationsFromBuffer = atoi(sizeOfAndOperationsBuffer);
isFirstTransmision = false;
} else {
numOfAndOperations += 1;
char *token;
char *rest = buffer;
char *result;
int count = 0;
char *numOne;
char *numTwo;
int indexN;
// re-used this chunk of code from stack-overflow
while ((token = strtok_r(rest, ",", &rest))) {
if (count == 1) {
char *numOneTemp = token;
numOne = numOneTemp;
printf("%s and ",token);
} else if (count == 2) {
char *numTwoTemp = token;
numTwo = numTwoTemp;
printf("%s = ", token);
} else if (count == 3){
indexN = atoi(token);
}
count++;
}
char tempBuffer[41];
bzero(tempBuffer, 41);
int i=0;
while(i < 10) {
tempBuffer[i] = (((*numOne++)-'0') & ((*numTwo++)-'0')) + '0';
i++;
}
int firstDigit = indexN / 10;
int lastDigit = indexN % 10;
char c1 = '0' + firstDigit;
char c2 = '0' + lastDigit;
puts(tempBuffer);
tempBuffer[10] = ',';
tempBuffer[11] = c1;
tempBuffer[12] = c2;
tempBuffer[13] = '\0';
//puts(tempBuffer);
sendto(sock, tempBuffer, strlen(tempBuffer), 0, (struct sockaddr *)&clientAddress, fromLength);
// print all sent if the numOfAndOperations = sizeOfAndOperations
if (numOfAndOperations == numOfAndOperationsFromBuffer) {
printf("The Server AND has successfully received %d lines from the edge server and finished all AND computations.\n",numOfAndOperationsFromBuffer);
printf("The Server AND has successfully finished sending all computation results to the edge server.\n");
}
}
}
}
}