Add a simple receiver.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 28 May 2012 12:02:09 +0000 (14:02 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 28 May 2012 12:02:09 +0000 (14:02 +0200)
receiver.c [new file with mode: 0644]

diff --git a/receiver.c b/receiver.c
new file mode 100644 (file)
index 0000000..b4c0759
--- /dev/null
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <time.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define PACKET_SIZE 1200
+
+
+int main(int argc, char **argv)
+{
+       int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+       struct sockaddr_in addr;
+       addr.sin_family = AF_INET;
+       addr.sin_addr.s_addr = inet_addr(argv[1]);
+       addr.sin_port = htons(atoi(argv[2]));
+
+       bind(sock, (struct sockaddr *)&addr, sizeof(addr));
+       listen(sock, 255);
+
+       for ( ;; ) {
+               static char buf[PACKET_SIZE];
+               int seqno;
+               ssize_t len = recv(sock, buf, PACKET_SIZE, 0);
+               if (len == -1) {
+                       perror("recv");
+                       exit(1);
+               }
+               if (len < sizeof(int)) {
+                       fprintf(stderr, "short packet\n");
+                       continue;
+               }
+               memcpy(&seqno, buf, sizeof(int));
+               printf("%d\n", seqno);
+       }
+}