-
Notifications
You must be signed in to change notification settings - Fork 0
/
5sec_server.c
executable file
·106 lines (93 loc) · 2.52 KB
/
5sec_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include "aw_queue.h"
const unsigned BUFFER_SIZE = 255;
const unsigned QUEUE_MAX_PACKETS = 255;
const char * BLINK_MSG = "CHIRP";
int main(int argc, char *argv[]){
if(argc != 2){
printf("usage: port\n");
exit(0);
}
int status;
int sockfd;
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
struct addrinfo *res, *p;
if ((status = getaddrinfo(NULL, argv[1], &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
for(p = res; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("listener: socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("listener: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "listener: failed to bind socket\n");
return 2;
}
struct sockaddr src_addr;
int src_len = sizeof src_addr;
char buff[BUFFER_SIZE];
int read_count;
int write_count;
int firsttime = 1;
bytequeue queue;
if (bytequeue_init(&queue, sizeof(buff), QUEUE_MAX_PACKETS) < 0) {
printf("Queue error.\n");
return 3;
}
struct timeval timeout;
while (1) {
timeout.tv_sec = 5;
timeout.tv_usec = 0;
time_t starttime = time(NULL);
time_t looptime = time(NULL);
while(timeout.tv_sec > 0) {
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
read_count = recvfrom(sockfd, &buff, BUFFER_SIZE, 0, &src_addr, &src_len);
if(read_count <= 0) {
/*printf("Socket timeout.\n");*/
}
else if(strcmp(buff, BLINK_MSG) == 0){
if (bytequeue_push(&queue, buff) < 0)
printf("Buffer overflow.\n");
}
else {
printf("Unknown packet received: %s\n", buff);
}
time_t currtime = time(NULL);
timeout.tv_sec -= difftime(currtime, starttime);
starttime = currtime;
}
if (bytequeue_pop(&queue, buff) < 0) {
printf("Buffer underflow.\n");
}
else {
printf(">>>>>BLINK: Delay is %f seconds.\n", difftime(time(NULL), looptime));
}
}
freeaddrinfo(res);
close(sockfd);
exit(0);
}