-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
45 lines (38 loc) · 1.13 KB
/
client.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
#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 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) {
printf("Enter a mathematical expression (e.g., 5 + 3): ");
fgets(expression, sizeof(expression), stdin);
// Send the expression to the server
fd_req = open(FIFO_REQ, O_WRONLY);
if (fd_req < 0) {
perror("Error opening request FIFO");
exit(EXIT_FAILURE);
}
write(fd_req, expression, strlen(expression) + 1);
close(fd_req);
// Read the result from the server
fd_res = open(FIFO_RES, O_RDONLY);
if (fd_res < 0) {
perror("Error opening response FIFO");
exit(EXIT_FAILURE);
}
read(fd_res, result, sizeof(result));
printf("Result: %s\n", result);
close(fd_res);
}
return 0;
}