From 77619ebfbb5ec81963d0cfc2506b8c59d2523abe Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 28 May 2012 14:02:09 +0200 Subject: [PATCH] Add a simple receiver. --- receiver.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 receiver.c diff --git a/receiver.c b/receiver.c new file mode 100644 index 0000000..b4c0759 --- /dev/null +++ b/receiver.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); + } +} -- 2.39.2