-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
91 lines (75 loc) · 1.93 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUFSZ 64
#define MOD 1000
void logexit(const char *str)
{
if(errno) perror(str);
else puts(str);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
uint32_t counter = 0;
char buffer[BUFSZ];
struct sockaddr_in clt, srv = {
.sin_family = AF_INET,
.sin_port = htons(51515),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK)
};
// Creating socket and binding
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) logexit("socket");
if (bind(sock, (struct sockaddr *) &srv, sizeof(srv)) < 0) logexit("binding");
// Forever looping. Waiting for clients.
while (1) {
int addrlen = sizeof(clt);
listen(sock, 1);
// New client. Accepting.
int newsock = accept(sock, (struct sockaddr *) &clt, &addrlen);
if (newsock == -1) logexit("accept");
// Receiving new data
if (1 != recv(newsock, buffer, 1, MSG_WAITALL)) logexit("recv");
int tmp = (int)counter;
if (buffer[0] == '+')
tmp++;
else if (buffer[0] == '-') {
tmp--;
if (tmp < 0) tmp += 1000;
}
else
logexit("invalid value");
tmp %= MOD;
// Sending counter value to confirm.
uint32_t value = htonl(tmp);
send(newsock, &value, 4, 0);
// Setting timer for 1s0ms.
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(newsock, &fdset);
struct timeval t = { .tv_sec = 1, .tv_usec = 0 };
setsockopt(newsock, SOL_SOCKET, SO_RCVTIMEO,
(struct timeval *)&t, sizeof(struct timeval));
if (0 == select(newsock + 1, &fdset, NULL, NULL, &t)) {
fprintf(stdout, "T\n");
}
else {
// Receiving counter confirmation.
if (3 != recv(newsock, buffer, 3, MSG_WAITALL)) logexit("recv");
// Checking value received and printing.
value = atoi(buffer);
if (tmp == value) {
counter = tmp;
fprintf(stdout, "%d\n", counter);
}
}
close(newsock);
}
close(sock);
exit(EXIT_SUCCESS);
}