-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
59 lines (50 loc) · 1.46 KB
/
server.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#define FIFO_REQ "/tmp/fifo_request"
#define FIFO_RES "/tmp/fifo_response"
int evaluate_expression(char *expression) {
int num1, num2;
char operator;
sscanf(expression, "%d %c %d", &num1, &operator, &num2);
switch (operator) {
case '+': return num1 + num2;
case '-': return num1 - num2;
case '*': return num1 * num2;
case '/': return num1 / num2;
default: return 0;
}
}
int main() {
char expression[256];
char result[256];
int fd_req, fd_res;
// Create the FIFO pipes if they don't exist
mkfifo(FIFO_REQ, 0666);
mkfifo(FIFO_RES, 0666);
while (1) {
// Read the expression from the client
fd_req = open(FIFO_REQ, O_RDONLY);
if (fd_req < 0) {
perror("Error opening request FIFO");
exit(EXIT_FAILURE);
}
read(fd_req, expression, sizeof(expression));
close(fd_req);
// Evaluate the expression
int res = evaluate_expression(expression);
snprintf(result, sizeof(result), "%d", res);
// Send the result back to the client
fd_res = open(FIFO_RES, O_WRONLY);
if (fd_res < 0) {
perror("Error opening response FIFO");
exit(EXIT_FAILURE);
}
write(fd_res, result, strlen(result) + 1);
close(fd_res);
}
return 0;
}