-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
65 lines (62 loc) · 1.63 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
60
61
62
63
64
65
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define EXPECTED_ARGS 2
#define QUEUE_SIZE 5
int main(int argc, char* argv[]) {
int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if(sock < 0) {
perror("socket");
return 1;
}
if(argc != EXPECTED_ARGS) {
printf("Usage: %s <addr>", argv[0]);
return 1;
}
struct sockaddr_un addr;
addr.sun_family = AF_LOCAL;
if(strlen(argv[1]) > sizeof(addr.sun_path) - 1) {
printf("Path too long");
return 1;
}
strcpy(addr.sun_path, argv[1]);
if(bind(sock, (const struct sockaddr *)&addr, sizeof(addr))) {
perror("bind");
return 1;
}
if(listen(sock, QUEUE_SIZE)) {
perror("listen");
return 1;
}
while(1) {
int clientFd = accept(sock, NULL, NULL);
if(clientFd < 0) {
perror("accept");
return 1;
}
printf("Client connected\n");
FILE* client = fdopen(clientFd, "r");
if(client == NULL) {
perror("fdopen");
return 1;
}
unsigned num;
while(fscanf(client, "%u ", &num) != EOF) {
printf("%u\n", num);
if(num == 418) {
printf("I'm a teapot\n");
printf("Code recieved, terminating\n");
goto end; // I know, I know, goto is bad, but it's the only way to break out of two loops
}
}
printf("Client disconnected\n");
}
end:
printf("Server terminated\n");
close(sock);
unlink(argv[1]);
return 0;
}