-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
56 lines (43 loc) · 1.02 KB
/
server.c
File metadata and controls
56 lines (43 loc) · 1.02 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
struct message {
char source[50];
char target[50];
char msg[200]; // message body
};
void terminate(int sig) {
printf("Exiting....\n");
fflush(stdout);
exit(0);
}
int main() {
int server;
int target;
int dummyfd;
struct message req;
signal(SIGPIPE,SIG_IGN);
signal(SIGINT,terminate);
server = open("serverFIFO",O_RDONLY);
dummyfd = open("serverFIFO",O_WRONLY);
while (1) {
// TODO:
// read requests from serverFIFO
if (read(server,&req,sizeof(struct message))!= sizeof(struct message)) {
continue;
}
printf("Received a request from %s to send the message %s to %s.\n",req.source,req.msg,req.target);
// TODO:
// open target FIFO and write the whole message struct to the target FIFO
// close target FIFO after writing the message
target=open(req.target,O_WRONLY);
write(target,&req,sizeof(struct message));
close(target);
}
close(server);
close(dummyfd);
return 0;
}