-
Notifications
You must be signed in to change notification settings - Fork 0
/
pusher.c
75 lines (58 loc) · 1.9 KB
/
pusher.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
#include "signals.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
const char* PRICE_SERVER_IP = "127.0.0.1";
const unsigned short PRICE_SERVER_PORT = 21123;
const char* PULLER_IP = "127.0.0.1";
const unsigned short PULLER_PORT = 21124;
void make_addr(const char* ip, const unsigned short port, struct sockaddr_in* out) {
out->sin_family = AF_INET;
out->sin_addr.s_addr = inet_addr(ip);
out->sin_port = htons(port);
}
int main() {
int reader, writer;
struct sockaddr_in price_server_addr;
struct sockaddr_in puller_addr;
unsigned price;
reader = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
writer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
make_addr(PRICE_SERVER_IP, PRICE_SERVER_PORT, &price_server_addr);
make_addr(PULLER_IP, PULLER_PORT, &puller_addr);
if (connect(reader, (struct sockaddr*)&price_server_addr, sizeof(price_server_addr)) == -1) {
perror("connect");
return 1;
}
if (setup_signal_handlers() != 0) {
fprintf(stderr, "Error setting up signal handlers");
return EXIT_FAILURE;
}
while(!shutting_down) {
int nrecv = recv(reader, &price, sizeof(price), 0);
if (nrecv < 0) {
perror("recv");
break;
}
if (nrecv == 0) {
printf("Received 0 bytes.\n");
break;
}
int nsend = sendto(writer, &price, sizeof(price), 0, (const struct sockaddr *) &puller_addr, sizeof(puller_addr));
if (nsend < 0) {
perror("send");
break;
}
if (nsend == 0) {
printf("Sent 0 bytes.\n");
break;
}
printf("Relayed price: %u\n", price);
}
close(writer);
close(reader);
return EXIT_SUCCESS;
}