-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
72 lines (58 loc) · 1.78 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
/**
* UDP Server.
*/
#include "common.h"
#define TIMEOUT (5)
char buffer[BUFFER_SIZE];
int main()
{
struct timespec start, end;
int32_t frames = 0;
size_t total = 0;
printf("Configure server...\n");
fd_set si_other;
fd_set original_stdin;
struct timeval timeout = {.tv_sec = TIMEOUT, .tv_usec = 0};
struct sockaddr_in si_me;
int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1)
{
printf("Error in socket(): %d\n", errno);
return -1;
}
FD_ZERO(&si_other);
FD_SET(s, &si_other);
bzero(&(si_me.sin_zero),8);
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = INADDR_ANY;
printf("Bind socket...\n");
if (bind(s,(struct sockaddr *)&si_me, sizeof(si_me)) == -1)
{
printf("Error in bind(): %d\n", errno);
return -1;
}
printf("Listening...\n");
do
{
struct sockaddr_storage addr;
size_t bytes = recvfrom(s, &buffer[total], UDP_FRAME, 0, NULL, 0);
if (!frames) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
}
struct Frame* frame = (struct Frame*)&buffer[total];
if (bytes == UDP_FRAME && memcmp(&frame->code, "MILA", 4) == 0) {
frames++;
total += bytes;
}
} while(frames < SUBIMAGES * FRAMES_PER_SUBIMAGES);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_nsec - start.tv_nsec) / 1000;
printf("Total bytes read %ld\n", total);
printf("Time to receive %ld subimages: %f[s]\n", total / FRAMES_PER_SUBIMAGES, delta_us / 1e6f);
printf("Estimated bandwidth %0.3f Mbits/s\n", (float)total / delta_us);
printf("Finished...\n");
close(s);
return 0;
}