-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsender.c
74 lines (64 loc) · 1.69 KB
/
sender.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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUFLEN 512
#define DATALEN 150
#define HEADERLEN 10
#define MSGLEN (DATALEN+HEADERLEN)
#define NPACK 10
#define PORT 9988
#define NUM_GLOBES 50
//#define SRV_IP "127.0.0.1"
void diep(char *s)
{
perror(s);
exit(1);
}
int main(int argc, char** argv)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char* ip_addr;
uint8_t r, g, b;
int j, k;
// Read in the IP address of the target
if (argc < 5) {
printf("Usage: sender ip-address-of-receiver rr gg bb\n");
exit(1);
} else {
ip_addr = argv[1];
r = atoi(argv[2]);
g = atoi(argv[3]);
b = atoi(argv[4]);
}
printf("Red %d Green %d Blue %d\n", r, g, b);
// Fill up the first 150 bytes of buf with the colours
for (j=0, k=0; j < NUM_GLOBES; j++) {
buf[HEADERLEN + k++] = r;
buf[HEADERLEN + k++] = g;
buf[HEADERLEN + k++] = b;
}
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((void *) &si_other, 0, (size_t) sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(ip_addr, &si_other.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
/*for (i=0; i<NPACK; i++) {
printf("Sending packet %d\n", i);*/
//sprintf(buf, "This is packet %d\n", i);
if (sendto(s, buf, MSGLEN, 0, (struct sockaddr*) &si_other, slen)==-1)
diep("sendto()");
/*}*/
close(s);
return 0;
}