]> git.sesse.net Git - bursty/blob - receiver.c
Add a simple receiver.
[bursty] / receiver.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <time.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10
11 #define PACKET_SIZE 1200
12
13
14 int main(int argc, char **argv)
15 {
16         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
17         struct sockaddr_in addr;
18         addr.sin_family = AF_INET;
19         addr.sin_addr.s_addr = inet_addr(argv[1]);
20         addr.sin_port = htons(atoi(argv[2]));
21
22         bind(sock, (struct sockaddr *)&addr, sizeof(addr));
23         listen(sock, 255);
24
25         for ( ;; ) {
26                 static char buf[PACKET_SIZE];
27                 int seqno;
28                 ssize_t len = recv(sock, buf, PACKET_SIZE, 0);
29                 if (len == -1) {
30                         perror("recv");
31                         exit(1);
32                 }
33                 if (len < sizeof(int)) {
34                         fprintf(stderr, "short packet\n");
35                         continue;
36                 }
37                 memcpy(&seqno, buf, sizeof(int));
38                 printf("%d\n", seqno);
39         }
40 }