-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
46 lines (38 loc) · 861 Bytes
/
server.c
File metadata and controls
46 lines (38 loc) · 861 Bytes
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 <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) {
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);
target = open(req.target, O_WRONLY);
write(target, &req, sizeof(struct message));
close(target);
}
close(server);
close(dummyfd);
return 0;
}